This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java primitive problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


output:
115
115
s
ssks

why is the second print statement displaying 115 ?
shouldn't be the output s as in case of third print statement.
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The + before c in that expression represents the unary operator + which makes its operand c an int...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohitkumar gupta wrote:

output:
115
115
s
ssks
why is the second print statement displaying 115 ?
shouldn't be the output s as in case of third print statement.


Numeric promotion with unary operators(+,-,and ~)

The evaluation of the expression +c will first promote the value c to int and then apply the operator + resulting in an int value. This will also apply to char, short, byte...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic