File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Primitive Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Primitive Casting" Watch "Primitive Casting" New topic
Author

Primitive Casting

Edward Seager
Greenhorn

Joined: Jan 02, 2010
Posts: 9
Hi,

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


Whereas this works fine



Thanks,


Eddy Seager
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Implicit downcast doesn't work in method calls i.e. parameters passed to methods...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Tapio Niemela
Ranch Hand

Joined: Jan 06, 2006
Posts: 77
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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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

Joined: Nov 04, 2009
Posts: 2066

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......

.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Oct 30, 2009
Posts: 504
yes thats correct.


SCJP 1.6 96%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Nega Daga....

After long time you are here......... Happy to see you again here........ Thanks.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
yeah....was busy with some applications.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Primitive Casting