This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

data type short  RSS feed

 
ramchander yshetti
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai,
would your please clear my problem
i.e
when i am using


short a=10;
short b=20;
short c=a+b;
System.out.println(c);


->it is giving compilation error that loss of precission..
please clear this.
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


By default, java converts all shorts/bytes to an int in an expression for evaluation. In the 3rd line, both a and b are converted implicitly into integers and then evaluated as an int. And assigning an int to a short requires type cast.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, cowboys,

for a longer answer, see this thread.

It's about the same problem, just with byte instead of short.


Yours,
Bu.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!