• 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

Type Conversion doubt

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
short sh1 = (short)1 - (short)2;//1
Short sh = new Short((short)1 - (short)2);//2

I am getting compiler error in line 2 saying (constructor Short(int) is undefined)
if the o/p of the expression ((short)1 - (short)2) is integer.
Then how come the integer value is getting assigned to sh1 in line 1, when implicit norrowing is not allowed.

Please help.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It says that there is no constructor in the Short class that takes (short)1-(short)2 as an argument. Only it can either take the primitive value or a String representation and not like what you have declared.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
short sh1 = (short)1 - (short)2;//1
Short sh = new Short((short)1 - (short)2);//2

(short)1 - (short)2 returns an int type, return value will be implecitly cast to short if it is enough to hold so line 1 compiled correctly.
But in line 2 there is no constructor for new Short(int) so compiler error given
 
Manvinder Kalsi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
however the exp is evaluated to an integer which needs type conversion before invoking the short constructor (explicit conversion)therefore an error.
which is not the case in line 1.
If the expression evaluated to an integer then how is integer value is assigned to a short in line 1 without type casting?
 
Nirodha Abeywardana
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"=" operator implecitly cast it if it can hold
 
Manvinder Kalsi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks i got it.

Summary:
Type conversion for primitives is needed only when the value is falling out of the range and for wrappers its mandatory always.

Please correct me if i am wrong.
 
Nirodha Abeywardana
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte b = 27;

but only because the compiler automatically narrows the literal value to a byte if it is a compile time constant.The preceding code is identical to the following:

byte b = (byte) 27;

byte b = 3; // No problem, 3 fits in a byte
byte c = 8; // No problem, 8 fits in a byte
byte d = b + c; // Should be no problem, sum of the two bytes fits in a byte

But this gives a compiler error since "b+c" is not a compiler time const.
In this case you have to exlicitly cast as
byte c = (byte) (a + b);

byte d = 3 + 8; // This compiles successfully, since 3+8 is a compiler time const

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What no one (except Nirodha) has realised is that the expression (short)1 - (short)2 is a compile-time computation, and the result -1 is in the range of a short.
[ December 19, 2006: Message edited by: Barry Gaunt ]
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic