Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KB 187

 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI All

Please explain me what the athor is trying to explain here.

byte b = 3;
b = (byte) (b + 7); // Won't compile without the
// cast, since b + 7 results in an int

Thnaks in advance
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The author is trying to tell that the addition operator takes int type of operands, so if you dont do the (byte) cast, you'll get compiler error.
Since narrowing requires an explicit cast.


Hope this helps
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please UseAMeaningfulSubjectLine.

KB 187 hardly makes any sense.
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte hi = 1;
hi = hi+1; // wan`t compile
hi = (byte) hi+1; // wan`t compile
hi = (byte) hi + (byte) 1; // wan`t compile
hi = (byte) (hi+1); // compile

It very simple case of type promotion. When you do byte + byte = int.

so if you try to assign int result to any byte type variable. it`s simply give you compile time exception for casting.

so, you have to explicitly case result to byte it indicate to compiler that you are knowing that you are assigning int to byte.
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

run this code and try to understand all cases:


if you have doubt in any of these cases, ask.

Raphael Rabadan.
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all

@kashik

why byte + byte = int. ?? IS their any reason..........

@ Amit

I will do meaning ful heading from next time.

@Raphadel

Why casing is not reqired for the constants final int x3 = 30;short s4 = x3 + 10;

Thanks again
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why casing is not reqired for the constants final int x3 = 30; short s4 = x3 + 10;

when you declare a variable final compiler knew that value of that variable will not change.
here the value of x3 is 10 which can fit in short type(x3+10 also) so here cast is not necessary.
In below example the value of i is 32768 which is bigger than short range so cast is required here


final int i = 32768;
short s = (short) i;

why byte + byte = int. ?? IS their any reason..........



any numerical operation that involves types smaller than int will result in int.

i am not sure about reason behind it.
 
Maya Karthik
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Since the result of arithmetic operation is an int, here also even though the operands are of byte type it gives an int result. So we have to explicitly type cast.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why byte + byte = int. ?? IS their any reason..........


because the addition operation produces int result which is similar to the division operator producing a double result.
So the bytes are widened to int before the addition takes place

hope this helps
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all of you.It helped a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic