Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper classes?

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The statements below,

Boolean b1 = Boolean.valueOf("True");
Boolean b2 = Boolean.valueOf("True");
System.out.println(b1 == b2); //prints true

Double d1 = Double.valueOf("10");
Double d2 = Double.valueOf("10");
System.out.println(d1 == d2);//prints false

Any reasons??
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have already asked this question here: http://www.coderanch.com/t/260776/java-programmer-SCJP/certification/Boolean-Wrappers

Also see http://www.coderanch.com/t/260471/java-programmer-SCJP/certification/Wrapper
[ December 21, 2006: Message edited by: Aniket Patil ]
 
hemadri raju
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to save memory, two instances of the
following wrapper objects will always be == when their primitive values are the same:
Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127

hope that will help.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about this?

Long l = -128L;
Long m = -128L;
System.out.println(l==m);


Yours,
Bu
 
ramya ray
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is This applicable for 1.4 , as in my case below code is giving false...


Integer i = Integer.valueOf("1");
Integer j = Integer.valueOf("1");
System.out.println(i==j);

 
Kalpesh Jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And what about this?

Long l = -128L;
Long m = -128L;
System.out.println(l==m);


its clearly written at SCJP FAQs on javaranch

Note: The Java Language Specification does not explicitly guarantee this behavior for long values within the range of a byte.
[ December 21, 2006: Message edited by: Kalpesh Jain ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[hemadri raju]: In order to save memory, two instances of the
following wrapper objects will always be == when their primitive values are the same:


Always?
 
Maneeesh Saxena
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
can you please mention the source from where to study above all.
is it part of SCJP1.4???
I've read K&B (SCJP1.4) but this is not covered there so please tell me the source for the same.
Regards
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For SCJP 1.4 exam, whatever there in K&B book is fine.

If you want learn about wrapper classes. Lot of links available in SCJP FAQ
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic