• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About short

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
short h = 40; //OK: int is coverted to short
h = h+2; //Error: cannot assign an int to short

In the above i am not understand that short is limit
-2 (power) 15 to + 2 (power) 15 -1.
The result(42) of above is also within the limit then why it is an error(I found in Mughals book).Pl clear my doubt!
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, this is a puzzling thing.
short h = 40; //Since h is in the range for short it's fine
h = h + 2;
This is what I understand to happen in a nutshell:
h = (int)h + int(2);
h which is short = int result of (h + 2);

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler allows the constant 40 to be assigned to h because it falls within the valid range of values for a short. The compiler does not take the first statement into consideration when it processes the statement "h = h + 2;" It just follows the standard rules of conversion: h + 2 results in an int which cannot be assigned to a short.
Junilu
 
Anupama Kota
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Charlie,Junilu
Thanks for U'r responce.Form this i understand that when the operation is done then the result is converted to heighest precidence(For ex. int,byte then it is coverted to int).
But initial assignment not change.
In the above is there any wrong then pl correct me.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first assignment is ok because it is one of the few implicit narrowing conversions. The following are required for this to work:
1) The source must be an int. So int x = 10L; would result in a compile-time error.
2) The value must be in the allowable range for the destination.
3) The compiler must be able to determine 2) at compile-time.
The second assignment violates 3.
See Mughal p. 47 (Additionally...)
 
Anupama Kota
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tod
B4 Ur answer i understand that B'coz of int is 4 bytes and short is 2 bytes so that it does't fit.So for this cast is needed.
like h = short (h+2);
But I didn't understand Ur 3) point.pl explain me.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when we write
short h = 40; //Since h is in the range for short it's fine
compiler knows at runtime that value is within range, therefore it accepts it.
h = h + 2;
compiler does not know at compile time that value will be integer but does not know whether it will fall within range, therefore error.
Note the value of an expression is available at the runtime.
Therefore
short i = 2;// no error
int a = 1;
short i = a + 1//error
This is what I understand to happen in a nutshell:
h = (int)h + int(2);
h which is short = int result of (h + 2);
 
Anupama Kota
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi prakshi
I don't understand range,Is it -2 (power)15 to +2 (power)15 - 1 then 42 is in the range na!! If 42 is not in the range then what is the Ex. short.Please provide the Ex. of short.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
42 represented as an int in binary is

42 represented as a short in binary is

so do u see the difference now ...
short h = 42 is fine
but when u say
int i=1;
short h = i+41; //error because now 42 is represented as 00000000 00000000 00000000 00101010
hope that helps
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
 
Anupama Kota
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Samith
Now i understand the difference.Thank U.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anupama Kota:
when the operation is done then the result is converted to heighest precidence(For ex. int,byte then it is coverted to int).
But initial assignment not change.
not really, if you have short + byte, result is int, NOT short. But assignment op always work, like byte b = 40; b += 2;

 
Haining Mu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anupama Kota:
when the operation is done then the result is converted to heighest precidence(For ex. int,byte then it is coverted to int).
But initial assignment not change.
not really, if you have short + byte, result is int, NOT short. But assignment op always work, like byte b = 40; b += 2;

 
Anupama Kota
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Haining
yes,u r right that i found in Mughals now in pg 53.
Thank U .
 
reply
    Bookmark Topic Watch Topic
  • New Topic