Java Notes

Strings

Creating Strings

Although String acts like a class, it is in fact considered a primitive variable type.

To create an empty string we can do:

String mystr;

To create a string with something in, we can do:

String mystr2 = new String("This is my text.");

 

~~~

 

Strings also have other useful abilities, like being able to be added:

String s1, s2, result;

s1 = "Hello";

s2 = " there.";

result = s1 + S2;

Meaning if we print result using System.out.println we would get:

Hello there.

Numbers can also be added in the same way:

String str, result2;

str = "Two: ";

result = str + 2;

Resulting in:

Two: 2

Stepping through Strings

Often we will want to step through a string a character at a time.  Let's say we have a string as follows:

String mystr = new String("abcd");

We can step through each character as so:

for (int i=0;i<mystr.length();i++)

{

 

}

NOTE.  We use () after the length because it is a method.

 

~~~

 

To get a character at position i we use:

mystr.charAt(i)

So if we want to print out each character in turn on its own line, we would do:

for (int i=0;i<mystr.length();i++)

{

 System.out.println("Char at " + i + " is " + mystr.charAt(i));

}

Which for mystr above, would show:

Char at 0 is a

Char at 1 is b

Char at 2 is c

Char at 3 is d

 

~~~

 

To step backwards through each character in the string, we would do:

for (int i=mystr.length()-1;i>=0;i--)

{

 System.out.println("Char at " + i + " is " + mystr.charAt(i));

}

Which for mystr above, would show:

Char at 3 is d

Char at 2 is c

Char at 1 is b

Char at 0 is a

 

Arrays

Creating Arrays

Arrays can be created of any size, but once you have created it, the size cannot change.

An integer array of 20 items can be created as follows:

int[] myintarray = new int[20];

This created items from 0 to 19.

 

~~~

 

To access an item as position 7 we can do the following:

myintarray[7]

Or to print it out do:

System.out.println("Number at 7 is: " + myintarray[7]);

To access the 7th number we do the following:

myintarray[6]

 

~~~

 

Arrays can be created of any type, either primitive (int, double, String, etc) or classes (Date, Object, etc).

Examples:

double[] doublearray = new double[12];

String[] stringarray = new String[17];

Point[] pointarray = new Point[18000];

Date[] mydates = new Date[100];

They always have the same pattern:

Type[] VarName = new Type[Size];

 

Stepping through Arrays

Let's say we have an array as follows:

int[] myintarray = new int[10];

 

~~~

 

To step through the array one item at a time we can do:

for (int i=0;i<myintarray.length;i++)

{

 

}

 

~~~

 

To get the item at position i, we do:

myintarray[i]

~~~

 

So if we want to print out each item in our array, we do:

for (int i=0;i<myintarray.length;i++)

{

 System.out.println("Num as position " + i + " is " + myintarray[i]);

}

Things to note:

1. The for loops above step FORWARD through the array, going from item 0, to 1, then 2, 3, etc, up to the length of the array.

2. myintarray.length does NOT have () after length.  This is because length is NOT a method.

 

~~~

 

To step backwards through the array using a similar for loop, we can do:

for (int i=myintarray.length-1;i>=0;i--)

{

 System.out.println("Num as position " + i + " is " + myintarray[i]);

}

This would go from item 9, to 8, then 7, 6, etc, to 0.