Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about String

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why result is false? (i mean output off program)
 
Maneesh Godbole
Saloon Keeper
Posts: 11327
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because +null and nullhello are not equal
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to achieve/prove here?

First of all you are comparing 2 different strings (+null and nullhello), because the concatenation operator (+) has a higher precedence than the equal operator (==).

But even if you would add some parantheses to your last statement, to execute the equal operator first, false will be printed because you are comparing 2 different strings (a string literal "null" and null)


And what's the output of this little code snippet?


Hope it helps!
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because the concatenation operator (+) has a higher precedence than the equal operator (==).

forget this thanks
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
And what's the output of this little code snippet?


Hope it helps!


Hi, could you please explain this code snippet? I don't really understand it. Its output is 'false' for 's1 == s2' and 'true' for 's1.equals(s2)'. But why the former statement results into 'false'? I thought that two references 's1' and 's2' referenced the same object in the String constant pool... Moreover the output for the following code will be "+null +null":



And the following code will result in 'true' for boths statements:

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this is constant expression (from jls 7
Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.

in example

String s2 not Compile-time constant expressions
i interesting why

result is false?
is this because?:
Literals of primitive type and literals of type String

but null is literal for String.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergey Zhylinsky wrote:could you please explain this code snippet?

I'll give it a shot Sit tight, fasten your seatbelts, here we go!

Sergey Zhylinsky wrote:Its output is 'false' for 's1 == s2' and 'true' for 's1.equals(s2)'. But why the former statement results into 'false'?

That's one of the reasons why you should compare strings (and other objects) always using the equals method and not the == operator. But this good practice won't help you on the exam, because you'll get questions using the == operator to compare strings Read on!

Sergey Zhylinsky wrote:I thought that two references 's1' and 's2' referenced the same object in the String constant pool...

That's true, but there is one important thing you need to know: Strings created at run-time will always be distinct from those created from String Literals. A very good article about strings and the string constant pool can be found here. Disclaimer: probably a little too advanced for OCAJP7

So let's have a few examples to illustrate this.

The string s1 is created from 2 String literals ("+" and "abc"), but string s2 is created from 1 String literal ("+") and a String reference variable s0). So that's why s1 and s2 are distinct and not referring to the same String.

Now let's add the keyword final to s0 and see what happens:

Because s0 is final, it's considered as a String literal. Therefore both s1 and s2 refer to the same String.

Disclaimer: this is here for completeness, it's not needed for the OCAJP7 exam (nor for the OCPJP7 exam). As the link I mentioned describes, you can use the intern method too.

Why does the 2nd line returns true? Because that's how the intern method works. From its javadoc: When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned.

Hope it helps!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergej Smoljanov wrote:result is false?
is this because?:
Literals of primitive type and literals of type String

but null is literal for String.

Probably because "null" is still another literal than null.


Using the intern method yourself, prints true (because both strings are equal according to the equals method):


Hope it helps!
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Sergey Zhylinsky wrote:could you please explain this code snippet?

I'll give it a shot Sit tight, fasten your seatbelts, here we go!


Thanks for you patience, provided answer and additonal reference to the external article. I will use all of this to fill my gap in this field.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergei Zhylinski wrote:Thanks for you patience, provided answer and additonal reference to the external article.

Nice to know: instead of making a "thank you" post, you could also +1 (using ) the post(s) which you liked. It's easier, faster and other ranchers will see immediately which are the "starred" posts.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic