Strings (Part-1) in Java

Strings are the sequence of characters.

Unlike primitive data types like int, float or Boolean, non-primitive data types are not defined by the language, but are constructed by the programmer.

These data types refer to a memory’s location which keeps the value or data so they are also called “reference type,” or “object type”.

String is also one of the non- primitive data type.

String Class

Java platform provides predefined class known as STRING.

String variables or String objects are the objects or instances of class java.lang.String.

This String class show character strings like “Hello”. All these strings are defined as objects of this class.

String Object

String variables are the object or instance of Class String. There are two method for create string instances.
1. String Literal: String is created using double quotes. For example: String str1= “Hello”;

2. Using New keyword:
For example: S tring str2= new String(“Hello”);

str1, str2 are objects of Class String containing character String “Hello”. But the difference is that using String literal str1 will refer as an interned object and “Hello” will be stored in “String Constant pool” which means JVM will first check in “String Constant pool” whenever string literal is created if string already exist in pool it will not be created again and will use the reference of pool object. While str2 is guaranteed to create new string object with reference variable each time it is created.

Java is an advanced programming language, Class String is built-in data type It means strings can be defined directly instead of defining them as array of characters. For String object as an instance of class ‘String ‘means that it has methods like length() that returns the number of characters in that String object, contains() which checks whether certain string is part of that String Object value or not, equals(String str) that compares the characters of that String Object Value with the characters of string passed as an argument.

Storage of Strings in memory

In java, Heap data structure is used for storage purpose. A part from heap memory is distributed for String Literals as ‘String Constant pool’.

As discussed above, String objects created using String Literals are stored in ‘String Constant pool’. While in case of using new keyword, new string objects with its reference variables and placed in heap.

For example:

String str1= “program”;

String str1= “code”;

String str3= new String(“Java”);

String str4= new String(“coding”);

All the strings will refer to string their corresponding objects in memory.

Strings are immutable

Strings are immutable which means when string object is declared and initialized its value cannot be modified. If we try to modify the string object it will produce a new object of string that will refer to same variable but previous string object will remain same in the pool. This is known as immutability of String.
Lets take an example

String str1= “Hello”

str1.concat (“World”)

Let us take one more example to take bit more understanding that why strings are immutable String
String str1=”hello”;

String str2= “hello”;
From above, only one object will be stored in pool which will refer to two reference variable in the memory.

If we change string from one reference variable, it will cause wrong value for second reference variable as placement of objects in memory work like above diagram. That’s why Strings are immutable that means once we have created object it cannot be modified.

“==” Operator

It is an equality check operator in java which is used to check equality of two object by comparison method.

It compares the reference to which both objects are located in the memory.

After this comparison this operator return true or false.

Example:
String str1= “Hello”;

String str2=”Hello”;

System.out.println(str1==str2);

It will return false as both reference variables point to different address in the memory location.

equals()

equals(object obj ) is a method of String Class which is used to check the if values of String Objects are equal or not.

It compares two strings by comparing their values and returns true or false.
Example:
String str1= “Hello”;

String str2=”Hello”;

System.out.println(str1.equals(str2));
It will return true as str1 and str2 have same values.

Leave a Reply