File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes One more on wrapper classes from Dan's mock exam.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "One more on wrapper classes from Dan Watch "One more on wrapper classes from Dan New topic
Author

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

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
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 ...


Narasimha
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
That code looks fine to me. What is the compiler error you're getting?


SCJP Tipline, etc.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
GM Shobha
Greenhorn

Joined: Apr 22, 2003
Posts: 10
System.out.println(b3.equals(b4));
Please explain,why is this condition return true?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Aug 26, 2002
Posts: 205
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

Joined: May 15, 2003
Posts: 6
i think b1==b2 should by false,and b1.equals(b2) should be true


i love java and every one in the java ranch
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Jul 02, 2002
Posts: 1865
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: One more on wrapper classes from Dan's mock exam..