aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assignment doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assignment doubt" Watch "Assignment doubt" New topic
Author

Assignment doubt

Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi guys,

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

I'm a little bit confused.



"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 19, 2006
Posts: 584
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

Joined: Feb 07, 2005
Posts: 2367
On the method call, you are sending a byte as parameter.
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 31, 2004
Posts: 11343

See JLS, 5.3 Method Invocation Conversion.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment doubt
 
Similar Threads
Operators and Assignment
How to pass a long in ...Long ll = Long.valueOf(3L);//gives
final int/byte
static variable vs final variable!
Narrowing conversions doubt