File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Litteral Conversion Auto Cast

 
Rafi Fareen
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

anyone can explain that:


i don't need to put caste for the above, because the compiler does that for me.

what about here ...



by why do i get compiler error when i assign b+c to c?
does that mean compiler puts caste automatically/implicitly only for literal assignments, not for expression assignments?

regards,

Rafi
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if the value of c exceeds 127 ?
Thats why compiler give an error during compilation because once it passes the compilation part Runtime cant do anything and this could be the disastrous situation.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While assiging a value, You don't have to put a cast if the value you are assigning in within the limit of variable like



But you have to put a cast if limit exceeds the limit



While eveluating the expresion the outcome of the expression is always auto promoted to wider type of the element being used in the expression.

and byte, short are always promoted to int.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rafi Fareen wrote:hi guys,

anyone can explain that:


i don't need to put caste for the above, because the compiler does that for me.

what about here ...



by why do i get compiler error when i assign b+c to c?
does that mean compiler puts caste automatically/implicitly only for literal assignments, not for expression assignments?

regards,

Rafi


This is called as Arithmetic promotion for binary operators....
 
Jehaan Butt
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bedrock here is this - whenever any arithmetic operation involving two different variables is carried out, the answer is always at least an int, even if the two operands are bytes or shorts.
 
Mark Moge
Ranch Hand
Posts: 100
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in addition
You can do sth like this

but you cannot do

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic