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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How does this work" Watch "How does this work" New topic
Author

How does this work

babudev Yam
Greenhorn

Joined: Jul 27, 2006
Posts: 8
I am currently preparing for the SCJP exam. And I am having real confusing time understanding primitive casting !
The below I have taken from the http://www.danchisholm.net/

class GFC100 {
public static void main(String[] args) {
final short s1 = 1; // 1
final char c1 = 1; // 2
byte b1 = s1; // 3
byte b2 = c1; // 4
byte b3 = 1; // 5
byte b4 = 1L; // 6
byte b5 = 1.0; // 7
byte b6 = 1.0d; // 8
}}

Question is which will generate compile time error :Answer is 6,7,8

I cant understand how 3 and 4 will work.

I have read that char is treated separately when it comes to casting and it cannot be used with byte and short for narrowing or widening(but can be used with int,long,float ,double for widening)

4 is a narrowing expression short to byte is allowed without the cast?

Can someone help me undertand how the primitive casting really works?

Thanks for your time!

Rajesh Kadle
Greenhorn

Joined: Sep 06, 2004
Posts: 26
I have read that char is treated separately when it comes to casting and it cannot be used with byte and short for narrowing or widening(but can be used with int,long,float ,double for widening)


You are correct, conversion between char and byte/short is norrowing conversion. If you see the code, s1 and c1 are declared final, compiler allows these conversions (char->byte/short) when the variables are delcared as final.

You remove the final keyword and you will see errors at line 3 and 4 also.


-Raj
Ritesh Srivastava
Greenhorn

Joined: Jul 06, 2005
Posts: 26
Also note compilation fails(at line 4/5) when the value of variables s1 or c1 is changed to 128 even though these variables are declared final.
babudev Yam
Greenhorn

Joined: Jul 27, 2006
Posts: 8
How is final keyword making the difference?
babudev Yam
Greenhorn

Joined: Jul 27, 2006
Posts: 8
How is final keyword making the difference?
Jonathan Jensen
Greenhorn

Joined: Aug 31, 2006
Posts: 6
How is final keyword making the difference?

----------------------------


Once a final variable has been assigned, it always contains the same value.
You are giving assurance to the compiler that the values (final short s1 =1;
final char c1=1 will never change. That is the reason you are not getting error.

Hope this is clear.
babudev Yam
Greenhorn

Joined: Jul 27, 2006
Posts: 8
Ok I get it !.

Thanks for the reply.

Every step gets confusing
Karthigeyan Kannabiran
Greenhorn

Joined: Sep 08, 2006
Posts: 5
When ever you declare the variable is final the compiler not only knows the type also the value for that variable. Since in our case the variable is declared final and the short value is compatible with byte so no compilation error.
Since the value is constant the compiler checks the value too...


Thanks & Regards<br />Karthik
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Babudev,

Language specification says:


5.2 Assignment Conversion
(...)
In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:

The expression is a constant expression of type byte, short, char or int.

The type of the variable is byte, short, or char.

The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.



If the variables in line 3 and 4 weren't final, you'd need an explicit cast.


Yours,
Bu.
[ October 10, 2006: Message edited by: Burkhard Hassel ]

all events occur in real time
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
just one reminder:
final primitive type variable is considered as constant, given that the declaration and assignment are done in one line. Otherwise it's NOT considered as constant. e.g.

1. final int i1=1;
2. byte b1=i1;
it works fine because i1 is considered constant. but...

1. final int i1;
2. i1=1;
3. byte b1=i1;
compiling will fail on line 3 complaining conversion error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does this work