In this article we will see what are 'Java Strings', and how to define and use Strings in Java with example.

In Java Strings are objects of java.lang.String class, this class has many unique and interesting prperties that we will discuss in this article.

1) Creation of a String

In Java string can be created in many ways, most popuar are using a "literal" or using "new" keyword.
// Using literal
String s1 = "java";

// Using new keyword
String s2 = new String("java");

System.out.println(s1.equals(s2)); // returns true
System.out.println(s1 == s2); // return false


But these two statement saves strings in memory in different ways, in first statement when string is created using literal, compiler will look into the string constant pool (region inside java heap), if same string i.e. 'java' is already there no new string will be created and s1 will point to already created string. If "java" is not present in the pool a new string will be created.

In another statement when string is created using new, compiler will look into the string constant pool (region inside java heap), if same string i.e. 'java' is already there no new string will be created, If "java" is not present in the pool a new string will be created there. Additionaly a string object with value "java" will be created in heap outside the constant pool and s2 will point to this heap object.

Hence 's1.equals(s2)' will return 'true' while 's1 == s2' will return false, because equals checks the value that is same but '==' checks references that is different, because s1 is pointing to one in constant pool while s2 pointing to one outside pool.

2) Strings are immutable in nature

An immutable object is one whose value can not be changed once assigned, look at the following line of code:
String s1 = "java";
String s2 = s1 + "tuts";

System.out.println(s1.equals(s2)); // returns false
Here we have created a string s1 with "java", and added "tuts" to s1, the equals statement will return false because, java strings are immutable; in second line even if we added s1 + "tuts", this will not change value of s1, this will create a new string "javatuts" and s2 will point to it while value of s1 i.e. "java" will be unchanged.

3) String class methods

String class comes with a pre defined method, these methods can be used to perform a number of operations on strings as shown below:

3.1) Length of a string

length() method can be used to determine length of a string:
		String s1 = "java";
		// return length of a string
		System.out.println(s1.length()); // 4

3.2) Character at an index in string

charAt() method can be used to return character at an index of a string:
		String s1 = "java";
		// return character at an index
		System.out.println(s1.charAt(3)); // a

3.3) Substring of a string

substring() method can be used to return substring of a string:
		String s1 = "java";
		// return substring of a string
		System.out.println(s1.substring(1,3)); // av

3.4) Concating a string

concat() method can be used to concat a string:
		String s1 = "java";
		// return concat of a string
		System.out.println(s1.concat("tuts")); // javatuts

3.5) Changing case of a string

toUpperCase() and toLowerCase() method can be used for changing case of a string:
		String s1 = "jAvA";
		// Changing case of a string
		System.out.println(s1.toUpperCase()); // JAVA
		System.out.println(s1.toLowerCase()); // java

3.6) Replacing a part of a string

replace() method can be used for replace a part of a string:
		String s1 = "jAvA";
		// replace a part of a string
		System.out.println(s1.replace("jA", "KB")); // KBvA
There are much more rich set of methods availabe in Java Strings, these methods make String class very usefull and easy to use.

In this article we have seen what are 'Java Strings', and how to define and use Strings in Java with example. In upcoming articles we will see more about Java.
  • By Techburps.com
  • Nov 7, 2017
  • Core Java