• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Byte primitive assignment

 
Greenhorn
Posts: 7
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
She'll be back. I'm just gonna wait here. With this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic