DEV Community

Pramod Bablad
Pramod Bablad

Posted on

20 Things Every Java Beginner Should Know About Java Strings

1) In Java, you can create string objects in two ways. One is using new operator and another one is using string literals.

2) String objects created using string literals are stored in String Constant Pool and string objects created using new operator are stored in the heap memory.

3) What Is String Constant Pool?

String objects are most used data objects in Java. Hence, Java has a special arrangement to store the string objects. String Constant Pool is one such arrangement. String Constant Pool is the memory space in the heap memory specially allocated to store the string objects created using string literals. In String Constant Pool, there will be no two string objects having the same content.

Whenever you create a string object using string literal, JVM first checks the content of the object to be created. If there exist an object in the string constant pool with the same content, then it returns the reference of that object. It doesn’t create a new object. If the content is different from the existing objects then only it creates new object.

4) String is a derived type, not a primitive type like int, double etc. Strings are objects in Java.

5) String objects in Java are immutable. That means, once you create String objects, you can’t modify them. If you try to modify them, a new object will be created with modifications.

6) To overcome the immutability of String objects, two more classes are introduced in Java. They are StringBuffer and StringBuilder classes. Objects of StringBuffer and StringBuilder class are mutable.

7) All three classes – String, StringBuffer and StringBuilder are final. That means you can’t extend them. All three classes are members of java.lang package.

8) In all three classes – String, StringBuffer and StringBuilder, toString() method is overridden. That means, whenever you use references to objects of these classes, actual content of those objects will be retrieved.

9) equals() and hashCode() methods are overridden in String class but they are not overridden in StringBuffer and StringBuilder classes.

10) String and StringBuffer objects are thread safety where as StringBuilder objects are not thread safety.

11) Using “==“, equals() and hashCode() on String objects.

All three – “==”, equals() and hashCode() are used to check the equality of two string objects. If you want to check the equality of two string objects based on their physical address, then use “==” operator. If you want to check the equality of two string objects based on their content, then use equals() method. It is recommended not to use hashCode() method to compare the string objects. You may get unexpected results.

12) Strings in Java are backed by character array. You can retrieve this array using toCharArray() method of String class.

13) If you are performing lots of string concatenation in your code, then use either StringBuffer or StringBuilder classes. These two classes give better performance than String class.

14) Java doesn’t support operator overloading except ‘+‘ operator. ‘+‘ can be used for number addition as well as to concatenate two string objects. This is the special treatment given by the Java to string objects.

15) Java provides 4 methods to compare the strings.

equals() – This method returns true if contents of two string objects are same.

equalsIgnoreCase() – This method compares two string objects but ignores the case of the characters when comparing.

compareTo() – This method compares one string with another and returns an integer if the string is smaller or equal or greater than the other string.

compareToIgnoreCase() – This method is same as compareTo() but ignores the case of the characters when comparing.

16) You need not to create objects to access the String class methods. You can do so using string literals also.

17) What Is String Intern?

String object in the string constant pool is called as String Intern. You can create an exact copy of heap memory string object in the string constant pool. This process of creating an exact copy of heap memory string object in string constant pool is called interning. intern() method is used for interning. Click here to see more about string intern in Java.

18) indexOf(), lastIndexOf() and matches(String regex) are the methods to perform search within a string.

19) Unlike in C and C++, Strings in Java are not terminated with null character. Strings are treated as objects in Java.

20) Java provides lots of in built methods to manipulate the string objects.

Source : https://javaconceptoftheday.com/tutorial-examples-strings-in-java/

Top comments (0)