• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

why compilation fails here

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it works this way

 
Divyya Joshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hama
 
Greenhorn
Posts: 3
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 115
11
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Saloon Keeper
Posts: 13076
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 115
11
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic