Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Byte Addition and Byte Range

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I experimented with the following code:-

public class Hello{
public static void main (String args[]) {
byte b = 126;
byte c = 3;
byte d = (byte) (b+c);
System.out.println (d);
}
}

And as you know, this is out of the byte range (-127 to 128) and it compiles fine and gives a negative output. Then I tried:-

public class Test {
public static void main (String [] args){
byte b = 127;
byte c = 128;
byte d = (byte) (b+c);
System.out.println (d);
}
}

and it gave me the following error: -

C:\Sun\AppServer\jdk\bin\Test.java:4: possible loss of precision
found : int
required: byte
byte c = 128;
^
1 error

Why did it give this error if 127+128 is within the (0-255) byte range?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers!

ok, first example:
In class Hello you have this line:
byte b = 126;

Looks quite easy, but a few things happen here:
The 126 is not a byte, it is a literal of type int.
But the compiler sees, that it "fits" into a byte, converts the int literal to a bit and assigns it to the variable.

In the next line similar action with the int literal 3.

By the way, there are no such things as byte literals. This only in between.

The last line before the output was
byte d = (byte) (b+c);


The part b+c is also not so trivial as it might look in first place.
You use the operator +

That means, that if the operands are type byte, char or short, all will be converted to int and the result of the operation is also an int.
The compiler cannot know at compile time if the result fits into a byte, so you have to cast explicitely.
With this manual cast, you just skip the first bits of the integer, remaining only the rightmost 8 bits of the 32 of the int.



In the second example (class Test) however you have a single line that does not compile:
byte c = 128;

Compiler recognized that the int literal is too big (one too big exactly) and so it does not compile.
The rest of the class would be ok.


Yours,
Bu.
 
reply
    Bookmark Topic Watch Topic
  • New Topic