Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization 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: 9509
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