Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A nice question

 
Roel De Nijs
Sheriff
Posts: 9780
101
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the output of the following program ?



[edit] if you want to know the solution (and explanation), just send me a PM, so the fun of other rangers won't be spoiled because solution is in the thread.
[ August 11, 2006: Message edited by: Roel De Nijs ]
 
Ivan Rebrov
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the output of the following program ?


Cool! Wow!

And jet more interesting the following:
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its really a nice one.

Bye & Regards,
Praveen
 
Justin Yao
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
It is really interesting.
[ August 11, 2006: Message edited by: Yao Gao ]
 
Rachil Chandran
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool Question!!
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So are you going to tell us why?
 
Ivan Rebrov
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amitabha Batranab:
So are you going to tell us why?


See this table of operator precedence in Java

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

operator "==" has lower precedence then �perator "+"
 
Roel De Nijs
Sheriff
Posts: 9780
101
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Full explanation

the first line outputs 'equals: true' (i think no explanation needed)
the second line outputs 'false', because the '+' operator has a higher priority then the '==' operator
==> so System.out.println("==: " + obj1==obj2); is equal to System.out.println(("==: " + obj1) == obj2); (pay attention to the braces)
so "==: SCJP" is compared with "SCJP" which returns false of cource

to get the expected output '==: true' you have to change it to
System.out.println("==: " + (obj1==obj2)); (again pay attention to the braces)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic