aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Type Conversion doubt 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 "Type Conversion doubt" Watch "Type Conversion doubt" New topic
Author

Type Conversion doubt

Manvinder Kalsi
Greenhorn

Joined: Dec 19, 2006
Posts: 3
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.


We are what we repeatedly do. Excellence, therefore, is not an act but a habit.....Aristotle
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

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.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Nirodha Abeywardana
Greenhorn

Joined: Dec 12, 2006
Posts: 7
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

Joined: Dec 19, 2006
Posts: 3
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

Joined: Dec 12, 2006
Posts: 7
"=" operator implecitly cast it if it can hold
Manvinder Kalsi
Greenhorn

Joined: Dec 19, 2006
Posts: 3
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

Joined: Dec 12, 2006
Posts: 7
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type Conversion doubt