Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assignment doubt

 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Can you please explain why the code in bold are illegal ?

I'm a little bit confused.

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from the Java Language Specification.

In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:

* The expression is a constant expression of type byte, short, char or int.
* The type of the variable is byte, short, or char.
* The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.

If the type of the expression cannot be converted to the type of the variable by a conversion permitted in an assignment context, then a compile-time error occurs.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith. Now I see the problem with the assignment byte b2 = l

But what about the method call ?
Such method expects a byte but I'm passing an int constant. I don't think the rules applied here are the same for variables. Could you please explain.
[ April 11, 2006: Message edited by: Edisandro Bessa ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the method call, you are sending a byte as parameter.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith,

The commented call // doSomething(i); doesn't compile. I'm trying to send a final int to a method which expects a byte.

Could you please explain why it's not allowed once it's allowed to assign a final int to a byte ?

Are method arguments assignment rules different from variable assignment rules ?
[ April 11, 2006: Message edited by: Edisandro Bessa ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the context of what I showed from the Java Language Specification was specifically for assignments of variables. I don't think it applies to method calls.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See JLS, 5.3 Method Invocation Conversion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic