It’s String things 😉
After reading OCP programming guide for Java SE 7, I think these are all small but important key points every Java developers should know about String class String is a class, so you can create a string object by new String(); In any String every character is a 16 bit Unicode. String is immutable, The term immutable here refers to the String object that is created and assigned to some reference variable.
/** * Here myString is a reference variable that holds the reference to * the actual String object created in memory. */ String myString = new String();
So, you can change value of myString reference variable whenever you want, as it’s not the actual object. When you create a String object it gets created inside Java heap memory and the reference variable just has link to that.
String x = "ab"; x = "cd"; print(x); //output will be cd
copyrights reserved to https://www.programcreek.com
So, after assigning new value cd the older value ab resides into memory, It’s called lost object. You can easily relate now why String is called immutable, as we can’t change the original object “ab”. Now think about these operations performed on String
String x= "Java"; x.concat(" Facts"); print(x); //Output : Java
In above code snippet when we perform concat operation there will be 2 objects in memory 1. Java 2. Java Facts but, as we have not any reference to newly created “Java Facts” it will become a lost object. and x still referring to Java. Now if you had already noticed We can create String objects these ways
String s = new String("myString"); or String s = new String(); s = "some other string" or even better String s = "more better string"
Java has string literal concept, here is example of literal
"Yes! I am a string literal"
JVM will sets aside a different memory space known as String constant pool and keep all the literals in pool and whenever it finds any String literal at a time of compilation it will check whether identical String literal already exists in the pool, if any matches found then no new literal will be created just a reference will be added to older literal.
So here s and s2 both refers to the same String literal “Hello”, That’s how in a big project if there are lots of identical Strings exists Memory will not complain.
But If you explicitly create new object by calling new String() it will create new object inside java heap despite of that identical string already exists in the pool or not.
So in general you should always use String literals so that compiler can optimize your code.
Now you can start to see why making String objects immutable is a good idea.If several reference variables refer to the same String without knowing it. It would be very bad if any of them can change String’s value.
Comparison in String
String first = "hello" String second = "hello" print(first == second) // true String first = "hello" String second = new String("hello") print(first == second) //false
As in first example both string literals are same hence == returns true, but in second example when we took new String() it returns false.
To compare String values rather than reference there is a method .equals() that compares actual values.
String first = new String("hello") String second = new String("hello") print(first.equals(second)) //true
That’s all small but important things about strings, Thanks 🙂