Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One more on wrapper classes from Dan's mock exam..

 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In one of his mock exam questions he says that below piece of code compiles fine and gives the output as "true". But my compiler is throwing the compile time error on the line numbers 1 and 2 marked below.
My system is Windows NT. Is it something that, this piece of code works fine in Windows 2000 and not in Windows NT.
Boolean b1 = new Boolean(true); //1
Boolean b2 = new Boolean(true); //2
System.out.print((b1==b2) + ",");
What a wounder full questions, in the mock exam.
Many thanks to Dan ...
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code looks fine to me. What is the compiler error you're getting?
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narasimha,
Is it possible that you intended to cut and paste the following question instead of the one that appears in your post?

If so, then it is because the method
public static Boolean valueOf(boolean b)
was introduced in version 1.4 so those that are using an earlier version will get a compiler error.
 
GM Shobha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(b3.equals(b4));
Please explain,why is this condition return true?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by GM Shobha:
System.out.println(b3.equals(b4));
Please explain,why is this condition return true?

b3 and b4 both have the value true. Therefore, they are equal. The capitalization of the String "true" doesn't matter in the constructor for Boolean.
 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan,
It is true that i cut the piece of code from the code you mentioned in your post. Version i am using is 1.3, i think because of which i am getting the compile time error. If the same type of question repeat in 1.2 exam, can we answer that, it will give compile time error. Because in 1.2 version it will not work.
 
wang weixin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think b1==b2 should by false,and b1.equals(b2) should be true
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by narasimha rao bandlamudi:
Hi Dan,
It is true that i cut the piece of code from the code you mentioned in your post. Version i am using is 1.3, i think because of which i am getting the compile time error. If the same type of question repeat in 1.2 exam, can we answer that, it will give compile time error. Because in 1.2 version it will not work.

The 1.2 exam won't try to trick you with code that will only compile with a 1.4 version compiler. If you are taking the 1.2 exam then you will not see any code that uses features that were introduced with the 1.4 version of java.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by wang weixin:
i think b1==b2 should by false,and b1.equals(b2) should be true

Your statement is correct if b1 and b2 are created using the Boolean constructor as shown in the code posted by narasimha.

If references b1 and b2 are assigned using the return values of the Boolean.valueOf method (see the code that I posted) then the expression b1==b2 evaluates to true.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic