This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why compilation fails here Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why compilation fails here" Watch "why compilation fails here" New topic
Author

why compilation fails here

Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
The following code is from K&B . And it explains that short needs to be cast.
I am not abe to understand this.


Kindly explain this to me
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

Divyya Joshi wrote:The following code is from K&B . And it explains that short needs to be cast.
I am not abe to understand this.


Kindly explain this to me


7 is which is bigger than , so it needs to be cast


``Worry does not empty tomorrow of its sorrow; it empties today of its strength.''
Hama Kamal
Ranch Hand

Joined: May 29, 2011
Posts: 144

it works this way

Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
Thanks Hama
Angelica Bunghez
Greenhorn

Joined: Mar 21, 2011
Posts: 3
The implicit downcasting from int to short will be done only at assignment operators, not at passing arguments in the methods
Angelica Bunghez
Greenhorn

Joined: Mar 21, 2011
Posts: 3
and the implicit downcasting at assignments will be done only for literals in range ( 7 is a int literal in range of short primitive type) -( this is our case) and for the constants.
Alex Theedom
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

May I also add that if the value is not within the range of the short (-32,768 to 32,767 (inclusive)) an explicit cast is required and that this value must be within the range of the int (-2,147,483,648 to 2,147,483,647 (inclusive)). Note that all literal values are integers UNLESS they are denoted as floats or doubles. See the following code snippet:



If you print s3, s5 and s6 the result will be -1.

Alex Theedom - Senior Java EE Developer (OCPJP/OCPJWCD)
Chandraprakash Sarathe
Greenhorn

Joined: Jan 21, 2012
Posts: 16
The implicit downcasting from int to short will be done only at assignment operators, not at passing arguments in the methods




Here it fails to compile at line 2 which requires explicit downcasting.Means sum of two short values results an integer ..dont know why ?

Chandraprakash Sarathe
http://javaved.blogspot.com
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3401
    
    9
Because the + operator always returns an int, unless one of the operands is a long, float, double or String. That's just how it works.
Alex Theedom
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

It is true that the + operator returns integers even if the arguments are shorts and that you must cast the result to a short. However this only applies when the operation includes a variable. Like this:

However in the following two circumstances it is not required to cast to short:

Applies equally for all arithmetic operators (+, -, *, %, /) and byte, char and integers, but not floats or doubles.


Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I have a question about implicit downcasting from long to int.
int a = 9 L //won't compile and implicit downcasting won't work
int a = (int) 9 L //compile
We need to do explicit downcasting.

But short a = 9 // implicity downcasting works.

Why implicit downcasting works for short, not for long?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why compilation fails here
 
Similar Threads
scjp5.0 question
Assingment and casting
Quick question about casting
range of values
Cast compiler error