This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

== operator

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I am having trouble applying the concept of == double equal ooperator in below scenarios. Could someone please help

Case1: In First case output is true

String s1="hello";
System.out.println(s1=="hello");


Case2: In Second case output is false

String a = "";
       a += 2;
       a += 'c';
       a += false;
System.out.println(a == "2cfalse");
 
Marshal
Posts: 65806
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please tell us where that code comes from, to reduce the risk of copyright problems. Please use the code button; if you used it, your code would look much better, like this:-As you know, unlike in C#, the == operator is not overloaded. It tells you whether its two operands represent the exact same object or not. Corey McGlone's article might be old, but what it tells you is still valid. I gave somebody an explanation here, so I can repeat myself. Briefly, the two String literals in the first code block (or, for that matter, any compile time constant of type String (officially called, “a constant expression”) are interned by whichever class loader is used. That means when you get to line 2, the String "hello" already exists in memory and that object is reused; youi therefore the same object twice, so == will return true.
Because a is not declared final in the second code block, none of the expressions in lines 2‑4 is a compile time constant; the String literal in line 5 does not therefore point to the same object. I do not think that += is allowed to create a constant expression, either.
 
rohit viezu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell for the response.

Could you please help me with the explanation for the below output?
How String objects are referred at run time?







 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It follows the same principles. The two String literals are compile time constants, but you need to check the API documentation about those two methods. The method call in line 4 appears to return the same String. It says here,

Returns:
   the String, converted to lowercase.

In the case of a String already all lower‑case and requiring no conversions it appears to return the same String object. But, unlike here, the documentation doesn't specifically say that; such behaviour should therefore be regarded as undefined and subject to change from version to version.
 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rohit viezu wrote:. . .

Interesting problem; I have never noticed that before.
 
rohit viezu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Further below confuses me more.

 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In view of what you have already posted, please explain what is new about that last code block. The bit about toLowerCase() will be the same as in your second post, and the result of the toUpperCase() call should be obvious.
 
rohit viezu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the last code block, new operator is used to create String object and before that String literal. I am not sure if this changes the way String objects are referred.

Thanks!
 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I missed the new. That means that s1 doesn't refer to the same object as the String literal, but thereafter it is (again) all down to the behaviour of toUpperCase() and toLowerCase(). As I said before, you cannot rely on such behaviour being defined anywhere. In this instance, the output is independent of the new.
By the way: you doubtless already know it is not normal practice to write new String() nor new String("anything"). (The two are different.)u
 
rubbery bacon. crispy tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!