• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Doubt with "System.out.println"...

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the following code sample:



The output is:


My doubt is why am I getting the output as above. Why "a == b is " and "Gitesh" are not printed?

Please help.

Thanks,
Gitesh
 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gitesh

I think "Gitesh" gets appended to string a and then you compare the value with string b.

try
giving like this System.out.println("Gitesh:"+(a==b));

this shld print Gitesh: true

Srividhya
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Srividhya, it prints:



By why is that behavior of String so, that the concatenation is happening?

Is it something related to operator precedence?




Gitesh
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gitesh

Yes. '+' operator has more precendence than '==' operator. So it concantenated and then checks for equality. Ranchers correct me if I am wrong.

Srividhya
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println() is able to print an object but that method merely calls the object's toString() method

hope its clear your doubt.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srividhya Kiran:
Gitesh

Yes. '+' operator has more precendence than '==' operator. So it concantenated and then checks for equality. Ranchers correct me if I am wrong.

Srividhya



You're not wrong, this behavior is indeed down to operator precedence, whereby + preceeds ==, resulting in the comparison of two String object references: a reference to a String object with value "a == b is hello world" and a reference to a String object with value "hello world".
This comparison fails and System.out.println() prints the boolean value "false". Same for the other System.out.println() invocation.
[ March 27, 2008: Message edited by: Jelle Klap ]
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all.
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to info,

"hello world"
and
"hello" + "world"

are same as string literal concatenation is resolved at compile time and get the string from the pool of literals and not in the heap.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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
reply
    Bookmark Topic Watch Topic
  • New Topic