• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does this work

 
babudev Yam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ritesh Srivastava
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is final keyword making the difference?
 
babudev Yam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is final keyword making the difference?
 
Jonathan Jensen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I get it !.

Thanks for the reply.

Every step gets confusing
 
Karthigeyan Kannabiran
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
James Quinton
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic