| 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
Greenhorn
Joined: Jan 18, 2012
Posts: 22
|
|
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 Programmer.
|
 |
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: 2771
|
|
|
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
Greenhorn
Joined: Jan 18, 2012
Posts: 22
|
|
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: 319
|
|
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?
|
 |
 |
|
|
subject: why compilation fails here
|
|
|