wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Mock Question - Double Wrapper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's Mock Question - Double Wrapper

Timothy Toe
Ranch Hand

Joined: Oct 19, 2002
Posts: 156
http://www.danchisholm.net/nov04/comprehensive/exam3.html

Answer :

b Prints: 4.0 Double has two constructors. The first accepts a parameter of type primitive double. The second accepts a parameter of type String.

I do not understand why 1 and 2 are allowed since "Double has two constructors. The first accepts a parameter of type primitive double. The second accepts a parameter of type String." ? I compiled and code and to my amazement, it worked fine. I chose answer c:compiler error.
[ November 26, 2002: Message edited by: Ioow Gneb ]
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
primitive numbers are promoted - upcasted - to a higher - wider range - of other primitive numbers implicitly, meaning that if you assign a float to a double, the float is automatically promoted to double.
since byte, char, short, int, float have a narrower range than double, any of them can be assigned to double.


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Timothy Toe
Ranch Hand

Joined: Oct 19, 2002
Posts: 156
In the next question (question 5) on the same mock exam is a very similar question. The answer to that question is :

The designers of the Java programming language felt that implicit narrowing conversions for method and constructor parameters would add unnecessary complexities to the process of resolving overloaded method calls.

So, for method and constructor parameters, an implicit widening conversion (question 4) is allowed but not a narrowing conversion (question 5). Am I correct ?
Ben Ritchie
Ranch Hand

Joined: Nov 18, 2002
Posts: 98
Yes, primatives (excluding booleans) can be converted, but only if the conversion widens the data. Conversion can happen during assignment, a method call or arithmetic promotion.
They must be cast to a narrower type (they can also be cast to a wider type, but the explicit cast isn't necessary).


SCJP1.4, SCJD, SCEA (in progress)
Ram Sank,
Greenhorn

Joined: Nov 17, 2002
Posts: 9
I also answered C.Compile error to this question.
I got thru line 2 ( since char is treated as unsigned integer prim.) but failed in String .
But I bet only guys who know by heart about all the constructors for all the Wrapper classes can correclty answer this question.
Does this mean that we have to know all the Wrapper classes by heart for SCJP ?
Ben Ritchie
Ranch Hand

Joined: Nov 18, 2002
Posts: 98
Does this mean that we have to know all the Wrapper classes by heart for SCJP ?

Yes, I think so. It's not as bad as it seems - they are quite easy to remember.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Ram Sank,:

Does this mean that we have to know all the Wrapper classes by heart for SCJP ?

The constructors of the wrapper classes must be memorized and some of the methods must be memorized. For example, the methods inherited from the abstract Number class must be memorized: byteValue, shortValue, intValue, longValue, floatValue, and doubleValue. Each wrapper has a method such as Byte.parseByte, Short.parseShort, Integer.parseInt, etc. All of the parseXxx methods must be memorized. Each wrapper has a static valueOf method that returns an instance of the wrapper class. All of those must be memorized. You must also know how to use the toString and toHexString methods.
You will need to know what arguments are accepted by each of the above methods and you will need to know which of the methods are static and which are instance methods.
You do NOT need to memorize the decode of the integral wrapper types.
You must also remember that the wrapper classes override the equals and hashcode methods.
Remember that the boolean literals are true and false and not upper case TRUE and FALSE. Remember that Boolean.TRUE and Boolean.FALSE are not primitives but are instances of wrappers.
The next version of my exam will be uploaded in a few days. I have added a very large number of questions covering the above topics.
[ November 27, 2002: Message edited by: Dan Chisholm ]

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

Joined: Nov 26, 2002
Posts: 11
Originally posted by Dan Chisholm:

The Float wrapper is most likely to appear on the exam because it offers more opportunities for error due to the fact that floating point literals are of type double unless otherwise specified with the f or F suffix. A floating point literal such as 1.0 will cause a runtime error if passed to a Float constructor.

Sorry, Dan, but the Float-Wrapper has also a constructor for a double !
Look at Wrapper-Classes diagrams (Java 1.4.1)
Mike


SCJP 1.4, OCUPF (OMG Certified UML Professional Fundamental), SCEA - I
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Michael Taupitz:

Sorry, Dan, but the Float-Wrapper has also a constructor for a double ![/URL]
Mike

Opps. You are correct. I edited out the misleading statement above.
 
Consider Paul's rocket mass heater.
 
subject: Dan's Mock Question - Double Wrapper
 
Similar Threads
Wrapper
Logical error in creating a HashMap
Wrapper classes: Sense of "valueOf" method?
double behaviour
double call by reference?