Initializing Strings in Java
1. Direct Initialization(String Constant) : In this method, a String constant object will be created in String pooled area which is inside heap area in memory. As it is a constant, we can’t modify it, i.e. String class is immutable.
String str = "GeeksForGeeks"; str = "geeks"; // This statement will make str // point to new String constant("geeks") // rather than modifying the previous // String constant.
Note: If we again write str = “GeeksForGeeks” as next line, then it first check that if given String constant is present in String pooled area or not. If it present then str will point to it, otherwise creates a new String constant.
2. Object Initialization (Dynamic): In this method, a String object will be created in heap area (not inside String pooled area as in upper case). We can modify it.Also with same value, a String constant is also created in String pooled area, but the variable will point to String object in heap area only.
String str = new String("very"); str = "good";
Note: If we again write str = new String(“very”), then it will create a new object with value “very”, rather than pointing to the available objects in heap area with same value.But if we write str = “very”, then it will point to String constant object with value “very”, present in String pooled area.
Comparing Strings and their References
1. equals() method: It compares values of string for equality. Return type is boolean. In almost all the situation you can use useObjects.equals().
2. == operator: It compares references not values. Return type is boolean. == is used in rare situations where you know you’re dealing with interned strings.
3. compareTo() method: It compares values lexicographically and returns an integer value that describes if first string is less than, equal to or greater than second string.For example, if str1 and str2 are two string variables then if
str1 == str2 : return 0
str1 > str2 : return a positive value
str1 < str2 : return a negative value
Note: The positive and negative values returned by compareTo method is the difference of first unmatched character in the two strings.
Comparing strings with equals: true true false Comparing strings with ==: true false false true Comparing strings with compareto: 0 -1
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Compare two Strings in Java
- Compare two strings lexicographically in Java
- Initialize HashMap in Java
- Initialize an ArrayList in Java
- Initialize a static Map using Java 9 Map.of()
- Initialize a static Map using Stream in Java
- Initialize a static map in Java with Examples
- KeyPairGenerator initialize() method in Java with Examples
- Initialize a static Map in Java using Double Brace Initialization
- Initialize a list in a single line with a specified value using Java Stream
- Compare Dates in Java
- How to compare two arrays in Java?
- Short compare() method in Java
- How compare() method works in Java
- RuleBasedCollator compare() method in Java with Example
- Java Integer compare() method
- Byte compare() method in Java with examples
- Double compare() Method in Java with Examples
- Float compare() Method in Java with Examples
- Java | ==, equals(), compareTo(), equalsIgnoreCase() and compare()
Improved By : ShreyasWaghmare