Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Wrapper classes?

Joe Harry
Ranch Hand
Posts: 10127
3
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

Also see http://www.coderanch.com/t/260471/java-programmer-SCJP/certification/Wrapper
[ December 21, 2006: Message edited by: Aniket Patil ]

Greenhorn
Posts: 15
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

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

Yours,
Bu

ramya ray
Ranch Hand
Posts: 101
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

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
[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
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
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