jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes byte question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "byte question" Watch "byte question" New topic
Author

byte question

jaman tai
Ranch Hand

Joined: Sep 26, 2005
Posts: 37
byte b = 0;
b += 1; //1 no error
b = b + 1; //2 error

why //1 has no error?
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Every arithmetic operation evaluates atleast to an integer ( primitive int ). You have to do a explicit type cast in second line, but += does this implicitly.

HTH


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
jaman tai
Ranch Hand

Joined: Sep 26, 2005
Posts: 37
oh, thanks Srinivasa!
Andreas Sandberg
Ranch Hand

Joined: Sep 14, 2005
Posts: 31
Don't forget, there are also rules about automatic implcit narrowing promotion.

Fore example.

byte b = 3; //Ok,

int i = 3;
byte c = i; //Not ok, not resolved at compile time.


So for implicit automatic promotion the following apply:

a) The operand to the right of the assignment operator must be a byte, char, short, or int.
b) Operand on the right must be in the range of the operator on the left
c) Operator on the left must be a byte, short, or char.

Anything else required an implicit cast.

int x = 3L; //Compile error, need a cast

int x = (int) 3L; //OK.


"When the compilers not happy ain't nobody happy"
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,
int i=3;
byte c = i;



a) The operand to the right of the assignment operator must be a byte, char, short, or int.
b) Operand on the right must be in the range of the operator on the left c) Operator on the left must be a byte, short, or char.

Anything else required an implicit cast.




The above example...satisfies all three conditions...
Then why the compilation error..

You have missed the keyword "final"

put int i as

final int i=3..and then try..


Parthiban Murugesan
Greenhorn

Joined: Oct 04, 2005
Posts: 2
Originally posted by A Kumar:
Hi,
int i=3;
byte c = i;




The above example...satisfies all three conditions...
Then why the compilation error..

You have missed the keyword "final"

put int i as

final int i=3..and then try..


Parthiban Murugesan
Greenhorn

Joined: Oct 04, 2005
Posts: 2
Originally posted by Parthiban Murugesan:
[QB][/QB]


Hi the solution adding "final" may not work if

final int b =130 :-) as this would ask for type casting.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,


Hi the solution adding "final" may not work if final int b =130 :-) as this would ask for type casting.



b) Operand on the right must be in the range of the operator on the left


It is not in the range so...

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: byte question
 
Similar Threads
code...
Pre increment of byte and short variables
javaranch round up exam
why I cann't assign FINAL INT to Byte
Behavior of primitive types when incremented beyond their max limit