• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primitive Casting

 
Edward Seager
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Why does the following code not compile (implicit cast from int to short)?


Whereas this works fine



Thanks,
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implicit downcast doesn't work in method calls i.e. parameters passed to methods...
 
Tapio Niemela
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Implicit downcast doesn't work in method calls i.e. parameters passed to methods...


Could you please describe what is an implicit downcast?
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In both the above cases, the int value is implicitly downcasted to short. The literal 10 is an int literal, which is implicitly downcasted to short in line 1. On line 3, the final constant i is implicitly downcasted to short...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Ankit Garg,
Since the variable i is a compile time constant, we can assigned it s2(should be within the range of short). OK.

But what about final wrapper objects? coding is below......

.

 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you said yourself, since i was a compile time constant, so it was automatically downcasted to short. Compile time constants are either of primitive types or String, other types of final variables are not compile time constants (including wrappers). This is why implicit downcast doesn't work on wrapper objects...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote :

Compile time constants are either of primitive types or String, other types of final variables are not compile time constants (including wrappers)


Thanks a lot Ankit Garg for this valuable information.

Again Thanks to All.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again one more question Ankit Garg,

Since interface accepts only constants, We can add wrapper objects as well. So there should be a difference between compile time constants and just constants? If so, how the just constants work? (At runtime???).

Thanks in Advanced.......!!!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be a difference,


Here we should initialize b in every constructors. So a is a compile time constant and b is a just constant. Correct. Pleasse confirm!
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes thats correct.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nega Daga....

After long time you are here......... Happy to see you again here........ Thanks.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah....was busy with some applications.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic