Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Microservices Testing (Live Project)
this week in the
Spring
forum!
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
Tim Cooke
Ron McLeod
Jeanne Boyarsky
Paul Clapham
Sheriffs:
Liutauras Vilda
Henry Wong
Devaka Cooray
Saloon Keepers:
Tim Moores
Stephan van Hulst
Tim Holloway
Al Hobbs
Carey Brown
Bartenders:
Piet Souris
Mikalai Zaikin
Himai Minh
Forum:
Programmer Certification (OCPJP)
Boxing query
Pawanpreet Singh
Ranch Hand
Posts: 264
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Why the output is
/*
false
true
true
false
*/
for
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
boolean b3 =true;
Boolean b4=true;
System.out.println(b1==b2);
System.out.println(b1==b3);
System.out.println(b3==b4);
System.out.println(b1==b4);
Could anybody explain....
Leonardo Luiz
Ranch Hand
Posts: 51
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I think it is because:
(b1==b2) and (b1==b4) compares two reference variable pointing to two diferent Boolean instances. (true is boxed into b4);
(b1==b3) compares two primitives (b1 is unboxed).
(b3==b4) compares two primitives.
wise owen
Ranch Hand
Posts: 2023
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
15.21.1 Numerical Equality Operators == and !=
5.6.2 Binary Numeric Promotion
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Boolean doubt
conditional operator precedence
Boxing conversions question
Boolean ==
Please explain boxing conversion
More...