Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

byte type data converted to int?  RSS feed

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was coding and i found a compiling error in a section of my code where i tried adding two variables of byte type and stored the result in a variable of byte type.

section of my code in which error occurred
byte b=10;
byte c=20;
byte d=b+c;


the error as shown by compiler:
required:byte
found:int

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, a number defaults to an int type. If you want a byte, you have to cast it.

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
10 and 20 are int. You are trying to add int and store the result in byte which is not possible. You need to cast the result to byte to store it into the byte variable
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the same time similar and different to Knute's mentioned situation is, when you try to operate with float numbers.
Default fractional number is a type of double.

So, rather than using:need to use:
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just cant understand one thing byte can accept from -128 to 127
so why it do not accept 10
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:i just cant understand one thing byte can accept from -128 to 127
so why it do not accept 10

As Knute already mentioned, this number 10 is not a type of "byte", but int type (as it is a Java default for non fractional numbers), so indeed it can fit to byte in terms of how big number is, but it can't be assigned because of different data type.
So you need to explicitly cast it to a "byte" data type.

when you write:
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can it is said that its a int not byte.even though we define it to be byte
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Temp
{
public static void main (String...s)
{
byte b=10;
Temp1.view(b);
}
}
class Temp1
{
static void view (byte b)
{
System.out.println("I am Byte");
}
static void view (int b)
{
System.out.println("I am int");
}
}
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
execute this code
as per compilation binding at runtime ...
the output was
I am Byte
if it would have been int it would have given output as
I am int
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Temp
{
public static void main (String...s)
{
float b=10.0;
Temp1.view(b);
}
}
class Temp1
{
static void view (double b)
{
System.out.println("I am Double");
}
static void view (float b)
{
System.out.println("I am float");
}
}
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read about it in "assignment conversion paragraph":
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while executing the above program
i found a compilation error
required:float
found:double
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will happily accept 10 if the compiler knows it is being given 10. Your code looks like this:-Well, it might for all the compiler knows. You cannot expect the compiler to check the values of all variables through the whole program. You cannot expect it to “remember” whether line 3 does or does not exist. Anyway, the value of those bytes might not be known until runtime. If the compiler does not know that those values will not change, it must err on the side of caution and assume you are going to overflow the range of a byte.

Lesson: you use ints, longs, doubles, BigIntegers and BigDecimals for arithmetic. You can do very restricted arithmetic with chars. Full stop.
You don't even try to do arithmetic with bytes.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just cant understand then y my code is behaving unexpectedly
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's been a bit of confusion here, partly because you didn't highlight the line in your code giving the compilation error.

The initial assignments (as Campbell has pointed out) are fine.
It's the last bit:


The result of b + c is an int...that's just the way it is.
The compiler does not trace through the code to check the values of things (except to ensure that they have been initialised to something), so for the compiler the above may well be more than 127, so it cannot (unlike the constant assignments earlier) assume that it will be OK.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing unexpected about that code. The Java® Language Specification instructs that the result of every integer arithmetic operation be an int (or a long), so the javac tool must assume the result is in the range −2147483648...2147643647. You cannot fit that into a byte without an explicit narrowing conversion shown with the cast operator (byte).
Your 10 and 20 literals are compile‑time constants, so the compiler knows whether they fit into the range of a byte or not, and it can decide that at compile time. But only for compile‑time constants. The variables are variable, so the compiler cannot assume their values have not changed.

Yes, you can write a compiler which checks whether the values have changed. You can use that and have it spend twenty minutes compiling a 1000‑line application.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . you can write a compiler which checks whether the values have changed. . . .
Challenge. Go and do it. I'll give you ten years to do it
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me, it helps to think of 10 as a literal integer and 10.0 as a literal double. If you want a float, you write 10.0F. If you want a long integer, you write 10L.

It also helps to realize that the computer is very dumb. ;-)
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote: . . . It also helps to realize that the computer is very dumb. ;-)
But it is programmed to do what it is programmed to do. It would be very difficult to create a compiler which verifies the values of variables throughout the runtime. Even more difficult to verify that you are in the range −128...127 when your code includes things like
byte b = myScanner.nextByte();  
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, the computer is an idiot savant. ;-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!