aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes KB 187 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "KB 187" Watch "KB 187" New topic
Author

KB 187

sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
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

Joined: Jun 06, 2007
Posts: 2718
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2718
    
    6

And please UseAMeaningfulSubjectLine.

KB 187 hardly makes any sense.
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
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.


kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Hello,

run this code and try to understand all cases:


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

Raphael Rabadan.


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
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

Joined: Mar 15, 2008
Posts: 210


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.


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Maya Karthik
Greenhorn

Joined: Jul 18, 2008
Posts: 10
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

Joined: Jun 06, 2007
Posts: 2718
    
    6

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

Joined: Nov 17, 2006
Posts: 118
Thanks all of you.It helped a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KB 187