Friday 19 October 2018

Strings In Java (Part 1)




Hey guys,

Today we are going to start a new topic and this topic is very important for interview purpose as well as for job/doing coding.


" String is a CLASS in Java.."  String is a sequence of characters.

There are two ways to get string object in java

  1. using new keyword
  2. using literal constant


1 ) Using a new keyword. 

whenever we want to create an object, we must know about the constructor of it.
String class has 2 Constructors.
  1. no argument constructor [ String() ]
  2. one argument constructor [ String(String s) ]
Note - Both are same no difference, just a way of writing (Syntax) is different because of the constructor. 




2) literal Way



Question  -- what is the diffrence between String s = new String("Hello"); and String s = "Hello";  ?

Answer -

String s1="hello";

The string literal "hello" will be created and this would be placed in the String constant pool. (constant pool is a special memory for String literal. )

Hence one object is created with s1 as a reference variable to it.

Also, when any other reference variable is created in future with the same value as "hello", it would refer to the already created String literal present in the constant pool rather than creating a new one.


String s1=new String("hello");

In this case, two objects would be created.One would be the same string literal as above and would be placed in String constant pool. The second object would be created and placed in the normal(non pool) memory and s1 would refer to it. The second object would act as a normal object in a heap.



In short,

String S1 = new String("Hello");
This statement creates a String object in the Heap Memory.

String S1 = "Hello";
This statement creates a String literal with value "Hello" in the String Pool.

For more understanding, let's take 3 cases.

Case 1. String Object and Literal

String s1 = "Hello";
String s2 = new String("Hello");

Since s1 and s2 are reference variables, they'd be pointing at their respective memory locations right ?

s1 points to String Pool's location
and
s2 points to Heap Memory location.

Now testing if they're equal:
if(s1 == s2) 
would return false, as the reference variables are checked.

Whereas
s2.equals(s1) 
will return true as equals function checks the individual characters in both the reference variables.


An Interesting Fact:
"String Literal Pool" is a memory area that contains all the string literals used in the program.
Whenever a new literal is encountered, the JRE checks if it exists in the String pool or not. It does not contain duplicate literals.

If you create two reference variables with the same value "Hello", they'd both point to the same location.

Case 2: Two String Literals:

Eg:
String s1 = "Hello";
String s2 ="Hello";

if(s1==s2) 
will be true.

Case 3: One string object and anonymous literal

String s1 = new String("Hello");

if(s1 == "Hello")
will be false.

In this case, "Hello" creates a literal in the pool which has a different memory location than S1's heap memory.


Source  -- quora 






No comments:

Post a Comment