• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

casting from int to byte

 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading following lines from link
http://www.jchq.net/certkey/0501certkey.htm

did not understand it clearly.

>>>if you really want enough rope, Java uses the C/C++ convention of enclosing the data type with parenthesis i.e. (), thus the following code will compile and run

public class Mc{
public static void main(String argv[]){
byte b=0;
int i = 5000;
b = (byte) i;
System.out.println(b);
}
}
The output is

-120

I did bnot understand why output is -120 when i has 5000 value.
Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
 
Marshal
Posts: 65026
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write down the binary string value of 5000, as in your other thread, strike out all but the rightmost 8 bits, and work out what that means in two's complement, with values of -128 (+)64 32 16 8 4 2 and 1 for the different bits going from left to right.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!