Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Byte Primitive type

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am trying to understand how the primitive byte works.

Can someone explain to me why this code is not working.



I have a feeling that it has something to do with the statement aWindow.setSize(3);
Here is the error i get.


init:
deps-jar:
Compiling 1 source file to C:\documents and settings\702723344\My Documents\projects.workspace\Netbeans\Tutorials\Interfaces\JavaApplication2\build\classes
C:\documents and settings\702723344\My Documents\projects.workspace\Netbeans\Tutorials\Interfaces\JavaApplication2\src\tut\intf\Window.java:26: setSize(byte) in tut.intf.Window cannot be applied to (int)
aWindow.setSize(3);
1 error
BUILD FAILED (total time: 0 seconds)


[ September 28, 2007: Message edited by: Fred Rosenberger ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited your post to change the second set of 'code' tags to 'quote' tags. the 'code' tags forced your errors to print on one line, making it much wider than the screen. 'quote' tags allow wrapping to the next line, making your post easier to read.
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The literal 3 is an integer, not a byte.

It works when you directly assign it (byte b = 3; ) , but if you pass it as a parameter to method you have to explicitly cast it:


Similarly, every decimal literal is a double, and you have to cast it if you're passing it to a method that expects a float.
[ September 28, 2007: Message edited by: Rob Prime ]
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. Could you briefly explain why i have to cast it if its passed as a parameter but not if its directly assigned when declared.

And also, i read that a byte has a range of -128 to +127.
Does this mean that a byte can be any number from -127 to +127?

I am a bit confused why the maximum is not 255 since the maximum number in an 8bit binary is 255 i.e. 11111111.

Thanks
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is something else that does not make sense to me.

Take a look at the values of the following statement

aWindow.setSize((byte)100);
System.out.println(aWindow.size);

Depending on the value of the byte that is passed in, the output is different as shown below

aWindow.setSize((byte)100) = 100
aWindow.setSize((byte)101) = 101
aWindow.setSize((byte)102) = 102
aWindow.setSize((byte)103) = 103
aWindow.setSize((byte)120) = 120
aWindow.setSize((byte)121) = 121
aWindow.setSize((byte)125) = 125
aWindow.setSize((byte)126) = 126
aWindow.setSize((byte)127) = 127
aWindow.setSize((byte)128) = -128
aWindow.setSize((byte)129) = -127
aWindow.setSize((byte)130) = -126


Is this to do with the range for the byte? (i.e. -128 to 127). I tried the above expecting that the application will produce an error if i try 128 but it didnt. The number changed to negative values. Could you please clarify this for me thanks.
[ September 28, 2007: Message edited by: O. Ziggy ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java (and many other languages) use one of the bits to set the sign of the number - the 'left-most'. so, a number like 127 is stored as

(more leading 0's here) 00000000 01111111

128 is written as

(more leading 0's here) 00000000 10000000

when you scrunch that into a byte, you only get to use 8 bits, giving you
01111111 and 10000000 respectivly.

note that now the leftmost bit is a '1' on what was 128. in a byte, that makes it negative. 129 would be 10000001, still negative.

to learn how to tell WHAT negative number it is, search for the phrase "twos complement".
[ September 28, 2007: Message edited by: Fred Rosenberger ]
 
Tony Docherty
Bartender
Posts: 2960
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit confused why the maximum is not 255 since the maximum number in an 8bit binary is 255 i.e. 11111111.
Because the left most bit is used to denote the sign of the number and hence you only have 7 bits remaining to represent a number ie 0 to 127
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15288
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by O. Ziggy:
Thanks for your reply. Could you briefly explain why i have to cast it if its passed as a parameter but not if its directly assigned when declared.

And also, i read that a byte has a range of -128 to +127.
Does this mean that a byte can be any number from -127 to +127?

I am a bit confused why the maximum is not 255 since the maximum number in an 8bit binary is 255 i.e. 11111111.

Thanks

The signed integer data types: byte, short, int and long are stored in two's complement format in Java - that's why the range for byte is -128 to +127.

In two's complement, 11111111 means -1, not 255.
[ September 29, 2007: Message edited by: Jesper Young ]
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.. i read about twos complement and it now makes sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic