Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Explicit Cast 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 "Explicit Cast" Watch "Explicit Cast" New topic
Author

Explicit Cast

Rex Rodriguez
Greenhorn

Joined: Jan 16, 2002
Posts: 21
I have the following code. I get an error message in line number 6 (a=a*b) at the time of compilation.. The error message is as follows:
"Incompatible type for =. Explicit cast needed to convert int to short."
I have declared a as short why then I have to cast. Thanks in advance for explanation.
public class test{
public static void main(String args[])
{
short a,b;
a=b=2;
a=a*b;
System.out.println(a);
}
}
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Rex Rodriguez:
I get an error message in line number 6 (a=a*b) at the time of compilation.. The error message is as follows:
"Incompatible type for =. Explicit cast needed to convert int to short."
I have declared a as short why then I have to cast. Thanks in advance for explanation.
public class test{
public static void main(String args[])
{
short a,b;
a=b=2;
a=a*b;
System.out.println(a);
}
}

The expression a*b gets evaluated using the rules for binary operators which are summarized as:
If one of the operands is a double, convert the other to a double, else if one of the operands is a float, then convert the other to a float, else if one of the operands is a long, convert the other to a long, else convert both to int.
So a and b in the expression a*b get implicitly converted to ints, and the multiplication gets carried out on ints, resulting in an int value for the expression.
Then you try to assign this int value to a variable of type short, and the compiler complains.
You need an explicit cast to provide a narrowing conversion here:
a = (short)(a*b);
Rob
[ January 17, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Originally posted by Rex Rodriguez:
public class test{
public static void main(String args[])
{
short a,b;
a=b=2;
a=a*b;
System.out.println(a);
}
}

The reason it gives compiler error because the result of a*b will be integer. That we are assigning to short variable. so it ask for casting. Don't confuse between a*=b and a=a*b
a*=b doesnt need casting it compiles fine. Because internal casting is done. Whereas in the second case we have cast it explictly


Arathi<br />Sun Certified Java Programmer
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The problem is that a and b are of type short.
When you multiply (a*b), a and b get promoted to ints and the result of the multiplication is an int which is not assignable to a short. An explicit cast is needed.
The following line doesn't give a compile error.
a=(short)(a*b);
Read JLS 5.6.2 Binary Numeric Promotion for more information about numeric promotion.
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Explicit Cast