A string is immutable in Java. String class represent character strings, i.e. string class is implemented with the char array. We can create a string in two ways :
String str1 = "playjavatutorials"; |
String str2 = new String("playjavatutorials"); |
When a string is created using double quotes it checks for the same value in the string pool. If the value already exists in the string pool then it returns the reference else creates it's object and then places it in the string pool. In this way, JVM saves lots of memory by using the same string at many places and threads.
When a string is created with the new operator then it explicitly creates a string in the heap memory.
The string is a final class with its all fields as final except for 'private int hash'.
String overrides equals() and hashCode() method to verify if given two strings are same or not. The equals() method is case sensitive so if case-sensitive checks are not required then equalsIgnoreCase() method should be used.
☛ Next >> StringBuffer in Java
A StringBuffer is like String but it can be modified because of its m ...
☛ Next >> StringBuffer in Java
A StringBuffer is like String but it can be modified because of its m ...
No comments:
Post a Comment