• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why not in method call....?

 
Prashant Neginahal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,
Case 1:
byte b=5;
This is absolutely fine unless assigning value comes under byte range.

Case 2:
void Testing()
{
call(5);
}
void call(byte b)
{
....
}
Here it is giving error stating that it is not possible.Why it should..?
Will u pls anyone explain me what's going in these two cases...?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Prashant,
take a look at here
here is an excerpt from this page,
--------------------
Another example is:

class ColoredPoint {
int x, y;
byte color;
void setColor(byte color) { this.color = color; }
}
class Test {
public static void main(String[] args) {
ColoredPoint cp = new ColoredPoint();
byte color = 37;
cp.setColor(color);
cp.setColor(37);// compile-time error
}
}
Here, a compile-time error occurs for the second invocation of setColor, because no applicable method can be found at compile time. The type of the literal 37 is int, and int cannot be converted to byte by method invocation conversion. Assignment conversion, which is used in the initialization of the variable color, performs an implicit conversion of the constant from type int to byte, which is permitted because the value 37 is small enough to be represented in type byte; but such a conversion is not allowed for method invocation conversion.
--------------------
hope this helps.
regards
maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw,
if u call method call(byte) with say,
obj.call((byte)5) then it works for a method m() that has byte as an argument because here we are doing EXPLICITY conversion for the method call..
regards
maulin
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic