STRING - JAVA: Learn JAVA Quickly (2017)

JAVA: Learn JAVA Quickly (2017)

CHAPTER 3. STRING

String is an object that represent sequence of char values. There is a two was for creating String objects:

1. By String literal

String str = "New String";

2. By new keyword

String str1 = newString("New String");

Note: Difference will be deeper explained when we will discuss about memory in JVM.

Let`s now create a few Strings by literal that contain your name, surname and country where you live. So in your current project make new class called ClassStringwith main method in first.pack package.

For example:

String name = "Your name";//type your name

String surname = "Your surname";//type your surname

String country = "Your country";//type your country

Note: Additional information that you can see is called comment. Comments are used to type some massages about your code for better reviewing latter on or organization of code. Here is another type of comment:

/*

This is a comment.

*/

If one operand is String, whole expression is String. It will be explained in another lines of code:

inti = 1;

String a= "Value: " + i;

System.out.println(a);

expected: Value: 1 will be printed to console

Second operand has converted to String(his String representation has made).

METHODS OF CLASS STRING

· equals

We have two String literals that are needed to be compared if they are equal or not:

String a1= "This is some text";

String a2= "this is some text";

Based on progress that we made until now, it won`t be hard to recognize what this code works:

if(a1.equals(a2))

System.out.println("Strings a1 and a2 are similar");

else

System.out.println("Strings a1 and a2 are not similar");

if(a1.equalsIgnoreCase(a2))

System.out.println("Strings a1 and a2 are similar");

else

System.out.println("Strings a1 and a2 are not similar");

expected: Strings a1 and a2 are not similar will be printed to console

Strings a1 and a2 are similar

Try to make it different. Make another String literals. What if we have String in lowercase and want to convert it to uppercase? This method will help as:

System.out.println("this is text".toUpperCase());

expected: THIS IS TEXT will be printed to console

This could be done vice versa, with method .toLowerCase().

· substring/startsWith

String s = "This is some text";

String s1 = s.substring(0,5);

if(s.startsWith(s1))

System.out.println("Starts with character from s1");

else

System.out.println("Does not start");

expected: Starts with character from s1 will be printed to console

Let`s discuss about this example. Method substring(a, b) with two parameters will count from begging of String s, it counts from 0, until fifth character. Note that space is also counted. In this case result of this will be “This”.

Next is startsWith(a) method which checks if String s starts with String s1 (“This”). If it is true it will be print first sentence, else second one.

Note: method substring(a) could be with one parameter.

String s2 = "Some text";

System.out.println(s2.substring(5));

expected:text will be printed to console

· contains

if(s.contains("text"))

System.out.println("String s contains 'text'");

else

System.out.println("Does not contain");

expected: String s contains 'text' will be printed to console

· compareTo

String name1 = "Ara";

String name2 = "Ana";

if(name1.compareTo(name2)>0)

System.out.println("After");

elseif(name1.compareTo(name2)<0)

System.out.println("Before");

else

System.out.println("Same");

expected: After will be printed to console

This code will lexicographically compare String names.

· returns < 0 then the String calling the method is lexicographically first

· returns == 0 then the two strings are lexicographically equivalent

· returns> 0 then the parameter passed to the compareTo method is lexicographically first.

ARRAYS

Array can be treated as a container whose holds a fixed number of values. That values must be of same type. Array havefixed length when is created.

· One dimensional arrays

inta[];

a = newint[5];

inta[] = newint[5];//same as previous

int[] a = newint[5];//also same

Declaration and initiation:

inta[] = {1, 2, 3, 4, 5};

· Two dimensional arrays

int[][] a = {{1, 2, 3}, {4, 5, 6}};

What if we want to print all values that are stored in some array, one or two dimensional. This could be done by using for and for each loops:

intarray[] = {1, 3, 5 ,7, 9};

for(inti = 0;i<array.length;i++) {

System.out.print(array[i] + " ");

}

intarray2[] = {2, 4, 6, 8, 10};

for(inti : array2) {

System.out.print(i + " ");

}

int[][] a = { {1, 2, 3}, {4, 5, 6} };

for (inti = 0; i<a.length; i++) {

for (intj = 0; j<a[i].length; j++) {

System.out.println(a[i][j]);

}

System.out.println();

}

Loop go through each row and then again through each column in every row and print result.

STACK VS HEAP

As just you could see creating primitive types, arrays and String is very easy. But, as just in other programs, every operation need some memory to be allocated. There are two types of memory in JVM (Java Virtual Machine), Stackand Heap. For example:

In first example we created an empty int, which is primitive type. We also could do it with double, float etc. result will be same.

In second picture you can see that we created an object with keyword new. From this we can conclude that on stack are stored primitive types and references, on heap are stored objects.

In last one picture declared values are delegated to empty memory spaces on heap.

Similar thing happens with String. Reference of String object is stored on stack and object that is created is on heap. Here we have example of String pool, what we will explain next. For example, imagine that you created same String literal, like this:

String str1 = “stringObj”;

Note: Creating object with keyword new does not check String pool, it automatically creates a new String object. But, without keyword new, it creates new String in String pool and every other time it first check if there is String with same name. If that condition is true, it just return reference of existing object.