aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Byte primitive assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Byte primitive assignment" Watch "Byte primitive assignment" New topic
Author

Byte primitive assignment

chirag visavadia
Greenhorn

Joined: Dec 21, 2009
Posts: 6

Can you guys explain me below situation....

When we compile below code
It will give compile error like
"ByteTest.java:12: possible loss of precision"

But when we add explicit cast like

It compiles without any error.

But when we open compiled .class file it look like we are assigning Integer value to integer and needs (byte) casting...


This is my first question to Javaranch, I apologize if I posted question in wrong manner.


OCPJP6
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

in java, arithmetic calculation by default uses 32 bits(int) to store. so after + it become int, hence cast is required to fit in to byte(higher order bits disappear, called loss of precision)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

chirag visavadia wrote:
But when we open compiled .class file it look like we are assigning Integer value to integer and needs (byte) casting...


are you sure? byte type converted to int?.....
chirag visavadia
Greenhorn

Joined: Dec 21, 2009
Posts: 6

Seetharaman Venkatasamy wrote:
chirag visavadia wrote:
But when we open compiled .class file it look like we are assigning Integer value to integer and needs (byte) casting...


are you sure? byte type converted to int?.....


I am not sure regarding conversion, But when I use decompiler, I got this conversion, that is why I posted this question.
my source code


I have decompiled using JD-GUI tool.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

chirag visavadia wrote:
I am not sure regarding conversion, But when I use decompiler, I got this conversion, that is why I posted this question.

Ohh, Ok. Um.. then I have to see on this when ever i get time , thanks
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

you have to refer JLS section given at http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2

basically this has to do with what JLS calls Assignment conversion. assignment conversion occurs when the expression on right hand side is converted to type of variable on left hand side. assignment conversion uses one of the following context

1. identity conversion
2. widening primitive conversion
3. widening reference conversion
4. boxing conversion optionally followed by widening reference conversion
5. unboxing conversion optionally followed by widening primitive converion

if expression on rhs is a compile time constant of the type byte, short char, int a narrowing conversion will occur if

a) the variable is of type byte, short , char and the expression is assignable to the respective types

narrowing conversion followed by boxing will occur if the type of variable is of type Byte, Short , Char and the expression if assignable to the respective variables

when you did byte a =6; // 6 is an int literal but since it is a compile time constant(and within byte range) narrowing conversion occurs

again byte b = 4; // same reason

but byte c =a +b; now the expression viz a + b is NOT compile time constant expression. however if you do

final byte a = 6;
final byte b = 4;
byte c = a+ b; // now it wont give compile error since a+b is compile time constant expression
 
 
subject: Byte primitive assignment