• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

typecasting problem

 
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class TypeCast1{
public static void main(String args[]){
short s1=25;
short s2=12;
short s3=s1+s2;

short s4=s1+9;

System.out.println(s3);
System.out.println(s4);
}
}
This code gives me compile time error, but if I change all short to int, it compiles perfectly fine. Why?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code gives compiler error as it should be, because when you try to add short and literal 9, 9 is by default taken as int and adding int + short results in int which you are trying to assign to short variable. Hence you are getting compile time error.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes anuj is correct, in addition i would like to add that even if you try to add short to short it give you an int.

So s3=s1+s2; will also give an error
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
by default all arithmetic operations involving all numeric types less than int (byte, short, char )will be promoted to int value by the compiler.


so if the destination type is not an int we need to type cast it.
hope I have cleared your doubt
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vijaya saradhi:
by default all arithmetic operations involving all numeric types less than int (byte, short, char )will be promoted to int value by the compiler.


so if the destination type is not an int we need to type cast it.
hope I have cleared your doubt



This is also true for unary operators like +,-
 
Nancy Antony
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
short s3 = s1 + s2;

1) first s1 is evaluated as a int type variable and also s2.

2) total s1+s2 is evaluated as int type.

3) so we should type cast to short or take RHS variable of type as int.

Plese check ..urgently..now itself ..scjp by Khlid mugal.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic