File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes java.lang 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 "java.lang " Watch "java.lang " New topic


Puja S
Ranch Hand

Joined: Jan 06, 2005
Posts: 51

Which of the following class instance creation expressions would generate a compile-time error?

a. new Short(1)
b. new Short('1')
c. new Short('b' - 'a')
d. new Short((short)1 - (short)2)
e. new Short((byte)1)
f. new Short((short)1)

This question is from Dan.....I am able to understand that a,b,c,d are the answers,but I am not able to understand,why is e not an answer ?

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
why is e not an answer?

The compiler will provide an implicit narrowing cast for an assignment of a constant expression that will fit in the left-hand operand. This is not available for arguments of methods or constructors.

The available constructors for class Short require a short or a String.

With "new Short((byte)1)", the argument has been explicitly cast to a byte. Going from a byte to a short is a widening conversion, not a narrowing cast, and perfectly legal for an argument.
[ January 18, 2005: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
I agree. Here's the link:
subject: java.lang
It's not a secret anymore!