Strings in Java

In this class, We discuss Strings in Java.

The reader should have prior knowledge of arrays. Click Here.

String:

The StringString is a sequence of characters.

a = “hello”

We use the String class to define strings.

The class “String” is an inbuilt class.

The example below shows a string’s declaration using a String class object.

class test

{

public static void main(String args[])

{

String s1 = new String(“hello”);

System.out.println(s1);

}

}

We use double quotations to define strings.

The below example shows to declare strings using string constants.

class test

{

public static void main(String args[])

{

String s1 = “hello”;

System.out.println(s1);

}

}

The string constant “hello” is used here.

How is memory allocated to strings?

We understand the memory allocation with an example.

class test

{

public static void main(String args[])

{

String s1 = “java”;

String s2 = “java”;

System.out.println(s1==s2);

}

}

Separate space is allocated in the heap for string constants called string constant pools.

S1 = “java” The string “java” is stored in string constant pool.

S2 = “java” In this statement, the JVM will check the availability of the string “java” in the string pool.

The string “java” is available, so S2 is given reference to the existing string “java.”

Both S1 and S2 reference the same String, “java.”

S1 == S2 will return true.

Strings in Java are immutable:

We can not modify strings in Java.

class test

{

public static void main(String args[])

{

String s1 = “java”;

String s2 = “java”;

System.out.println(s1==s2);

s2 = “java is top”;

System.out.println(s1==s2);

s1 = “java is top”;

System.out.println(s1==s2);

}

}

Output:

true

false

true

s2 = “java is top”; a new string “java is top” is created in this statement.

The new string reference is assigned to s2.

s1 = “java is top”; In this statement existing string “java is top” is referenced to s1.

We get an output of true, false, and true.

Create strings using an object:

class test

{

public static void main(String args[])

{

String s1 = new String(“java”);

String s2 = new String(“java”);

System.out.println(s1==s2);

}

}

Output:

false

Even if the strings are the same separate memory is allocated.

If we define strings using an object, separate space for each String is allocated, so the output is false.

Few String class constructors:

The string class will take a character array in a constructor.

The below example shows the code.

class test

{

public static void main(String args[])

{

char[] a = {‘a’,’b’,’c’};

String s1 = new String(a);

System.out.println(s1);

}

}

Another constructor takes character array and index positions.

class test

{

public static void main(String args[])

{

char[] a = {‘a’,’b’,’c’};

String s1 = new String(a,0,1);

System.out.println(s1);

}

}

Zero is the starting index.

One is the ending index.

The above program takes the character “a” in the string s1.

The ending index is not considered.

String s1 = new String(a,0,2); will take characters at index 0,1.

s1 will have the string “ab.”