Strings In Java


There are much use of java strings in any java project ,it is very importent to make clear understanding for java string.

First thing, java strings are immutable or final, immutable or final means each time when we are creating or editing or performing any method to change string, one new string created in string pool.

As a Example See why we are taking java strings are immutable or final, may be there are multiiple user creating strings in an project and may be different user have reffernce for one string then if string will not immutable then one person string value change will effect other person string changes.

we can create new string as string literal or by create string object.

 

Create String literal :

 String exampleString = "java";
 

Each time when we will create string literal,New String object created in String pool .string value stored in string pool. String pool space is located inside heap. beacause of lot use of strings when new string create as string literal , java firstly check string pool for string value, if string value exist in string pool then return refference for that string value, if value not exist then new string will create inside string pool and return refference for new string value.

Create String Object :

   String exampleString = new String("java");
   

if you are creating a new String object by using new operator, then string pool check will not perform and string object will create in heap memory.

In this example showing how to string objects stored in String pool or Heap memory.

Example1:

 

 public class String_Example {

  public static void main(String[] args) {
     
	  // this line will create new String in String pool
	  String stringPool1 = "Java";
	  
	  // this line will create new String in String pool
      String stringPool2 = "Example";
      
      // Firstly check String pool Java is already there
      // Not create new string object 
      // Return refference for existing Java string object 
      String stringPool3 = "Java";
      
      // Not check String pool
      // Create a new object in Heap memory
      String stringObject = new String("Java");
            
  }

}

 

String pool String object storage heap

Compare two strings :

Now showing two way to compare strings.

A. Compare string by equals() method :

public boolean equals(Object) compares string to the other object.

if two strings refference point to same memory address or two strings have same values then equal() method return true.

Example :

Comparing string with equals() method.

  

public class String_Example {

	  public static void main(String[] args) {
	     
		  String str1 = "Java";
		  
	      String stringObject = new String("Java");
	      
	      String stringObject1 = new String("Java");
	     	      
	   // Comparing value of str1 and value of stringObject
	      if(str1.equals(stringObject))
		      System.out.println("str1 == stringObject Matched.");
	      else
	    	  System.out.println("str1 == stringObject Not Matched."); 
	      
	   // Comparing value of stringObject and value of stringObject1
	      if(stringObject.equals(stringObject1))
		      System.out.println("stringObject == stringObject1 Matched.");
	      else
	    	  System.out.println("stringObject == stringObject1 Not Matched.");   
	      
	  }

	}
	

Output:

	str1 == str1 Matched.
    str1 == stringObject Matched.
    stringObject == stringObject1 Matched.

B. Compare string by == :

== operator return true if two string redderence point to same object memory address. return false if two memory addresses values are same.

Example :

public class String_Example {

	  public static void main(String[] args) {
	     
		  String str1 = "Java";
		  
		  String str2 = "Java";
	      
	      String stringObject = new String("Java");
	      
	      String stringObject1 = new String("Java");
	     
	      // Comparing address of str1 and str2 not value
	      if(str1 == str2)
		      System.out.println("str1 == str2 Matched.");
	      
	   // Comparing address of str1 and address of stringObject
	      if(str1 == stringObject)
		      System.out.println("str1 == stringObject Matched.");
	      else
	    	  System.out.println("str1 == stringObject Not Matched."); 
	      
	   // Comparing address of stringObject and address of stringObject1
	      if(stringObject == stringObject1)
		      System.out.println("stringObject == stringObject1 Matched.");
	      else
	    	  System.out.println("stringObject == stringObject1 Not Matched.");   
	      
	  }

	}
	

Output:

	 str1 == str2 Matched.
     str1 == stringObject Not Matched.
     stringObject == stringObject1 Not Matched.
  

 

 

Share this: