• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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 ?

Thanks.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic