Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explicit Cast

 
Rex Rodriguez
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic