Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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 Head First Android this week in the Android 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
Author
Ranch Hand

Joined: Jan 18, 2012
Posts: 77
    
    5

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 Developer (OCPJP/OCPJWCD/Spring Web)
Co-author of Professional Java EE Design Patterns. Blogger at alextheedom.com. Twitter at @alextheedom
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: 3900
    
  18

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.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Alex Theedom
Author
Ranch Hand

Joined: Jan 18, 2012
Posts: 77
    
    5

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?
 
Consider Paul's rocket mass heater.
 
subject: why compilation fails here
 
jQuery in Action, 3rd edition