• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Question from Dan's exam.

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is a simple question from Dan's exam.
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
Boolean b3 = new Boolean("TrUe");
Boolean b4 = new Boolean("tRuE");
System.out.print((b1==b2) + ","); //1
System.out.print((b1.booleanValue()==b2.booleanValue()) + ",");//2
System.out.println(b3.equals(b4));
Ths ans given is : false,true,true
My doubt is in the line #2. It seems to be v..simple but i am getting confused. When ohjects are not equal(line #1), how can the value be the equal in (line#2) .
Pl. explain this.
-Sanjana
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi There,
It is simple
In line#1 u r comparing the objects themselves , since they r stored at diff. locations in memory it returns false , while in lines 2 and 3 you are comparing the values stored at these memory locations which returns true.
Hope it makes sense,
Good luck;
 
Don't MAKE me come back there with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic