• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Dan's Mock Question - Double Wrapper

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Timothy Toe
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic