• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type Conversion doubt

 
Manvinder Kalsi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Harry
Ranch Hand
Posts: 10082
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Nirodha Abeywardana
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
"=" operator implecitly cast it if it can hold
 
Manvinder Kalsi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic