File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why compilation fails here

 
Divyya Joshi
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hama Kamal
Ranch Hand
Posts: 144
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it works this way

 
Divyya Joshi
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Hama
 
Angelica Bunghez
Greenhorn
Posts: 3
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The implicit downcasting from int to short will be done only at assignment operators, not at passing arguments in the methods
 
Angelica Bunghez
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
5
Java Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandraprakash Sarathe
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Author
Ranch Hand
Posts: 77
5
Java Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic