comparing Strings

Oracle Certified Foundations Associate Java Certification Questions and Discussion
1Z0-811

Moderator: admin

Post Reply
nick12345
Posts: 17
Joined: Fri Aug 13, 2021 12:43 pm
Contact:

comparing Strings

Post by nick12345 »

Hello,

I have a question regarding comparing Strings.
Can you please help me to understand the results? I'm confused.

Thank you!

Code: Select all

     
     String s1 = "a";
     System.out.println(s1.concat("b") == "ab"); //false
     System.out.println("a".concat("b") == "ab"); // false

Code: Select all

  
  String s2 = "bbb";
  System.out.println(s2.substring(0, 3) == "bbb"); //true

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: comparing Strings

Post by admin »

You need to know about two things about Strings - 1. They are immutable and 2. They are interned.
s1.concat("b") and "a".concat("b") return a new uninterned String object. "ab" is a compile time constant and is therefore interned, which is why it is a different object and so, == returns false.

In case of s2.substring(0, 3), length of s2 is 3 and so, s2.substring(0, 3) doesn't need to make any new substring. It returns the same String object and so, == returns true.

See this for details: https://stackoverflow.com/questions/105 ... -interning
If you like our products and services, please help us by posting your review here.

nick12345
Posts: 17
Joined: Fri Aug 13, 2021 12:43 pm
Contact:

Re: comparing Strings

Post by nick12345 »

So if I take a compile time constant and I call one of String manipulation methods (concat(), trim(), toLowerCase(), toUpperCase(), replace() ) it will return a new uninterned String object, which will be stored in the heap memory?

So when the value of the expression can be computed only at runtime, so this new string will be stored in the heap memory?
So will String s21 be stored in the heap memory?

Like this example:

Code: Select all

String s19 = "a";
String s20 = "ab";
String s21 = s19 + "b";
System.out.println(s21 == s20); //false
It is a little bit confusing because I thought in the heap memory are stored only Strings object, which were created with a word "new".

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: comparing Strings

Post by admin »

Yes, I agree that it is a quite confusing. Just remember this basic rule:

Anytime a String is created dynamically by the JVM (i.e. when the compiler cannot determine the contents of the String at compile time) , it will be created by JVM on the heap as a new object different from all the rest. For example, if you do new String("123") twice, the JVM will create two different String objects containing 123 on the heap. But, of course, before runtime, the compiler sees the String "123" in the code as well. This is a compile time constant string and so, the compiler will include an instruction for the JVM to create a String containing "123" in the String pool as well.

The compiler cannot execute the code and so, it cannot determine what will be the result of new String("123"), and so this call executes at run time and a new String has to be computed by the JVM. That is why it will be created on the heap.

Thus, effectively, there will be three String objects containing "123" - one in the String pool and two in the heap.

Finally, you can call the intern method on any String, to get a String object from the String pool. So, (new String("123")).intern() == "123" will return true.

This is a big topic, and it is best if you read about it from a good book if you have trouble understanding because it is not possible to cover everything in this post.

HTH!
If you like our products and services, please help us by posting your review here.

nick12345
Posts: 17
Joined: Fri Aug 13, 2021 12:43 pm
Contact:

Re: comparing Strings

Post by nick12345 »

Thank you very much for your reply :)

It is much clearer now!

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests