Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

promote question  RSS feed

 
liao bi-ling liao bi-ing
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java,to declare the following is legal.
byte a=3;
but I have a question.
In java,(3) is defaulted int.
now a is a btye variable;the system promote int(3) to byte.
I don't understand why in this plase the system use the order(promote).
To transform int into byte,we should use the other order(cast).
Why in this plase we don't use the order(cast)?
Thank you for your reading,and hope you can supply me some answers or thoughts.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here the cast was necessary because of possible loss of precision. In your case the compiler already knew there was no loss of precision because '3' is a constant.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!