Problems on String – Set 1

      No Comments on Problems on String – Set 1




What will be the output of the below program?

package com.masterjavatutorial;
public class Test {
	public static void main(String[] args) {
 
		 String str = "helloworld";
		 String str1 = str.toUpperCase().toLowerCase();
		 System.out.println(str == str1); 
        		 
	}
}

Output :

false

String str = “helloworld”; // str object is formed in string pool memory area.
String str1 = str.toUpperCase().toLowerCase(); // str1 object is formed in heap area

In case of String , if the content is changed while performing dynamic operation on the string, then the resulting string is formed in Heap Area of memory.

Here, toLowerCase() is getting called on already changed content (str.toUpperCase()). Hence, it forms a new objwct in heap area and points to it.