• Post Reply Bookmark Topic Watch Topic
  • New Topic

negative character literal?  RSS feed

 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char c=(char)-65;

This is legal but how ?? what is the value actually being stored in c ?
The output is shown as ﾿.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try printing out the numeric value and see if you can work out what is happening. ie
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You actually have an integer literal wioth the value -65, and then you cast that to char.

An int is 32 bits, and a char is 16 bits. What happens is that the top 16 bits of the int are thrown away, and then the bottom 16 bits are put into the char.

-65 in binary is: 1111 1111 1111 1111 1111 1111 1011 1111

Chop off the top 16 bits leaves you with: 1111 1111 1011 1111, which is in hexadecimal: FFBF

So you get the Unicode character U+FFBF
Which happens to be an undefined code, see this chart.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jesper has said, you have an int literal, but it is positive 65.
Then you apply the sign‑change operator to it.
Then you cast it to a char.

If you look through the Java Language Specification, you find something very obscure: there is no such thing as a negative number literal. They are all positive or 0 but you can change their sign with the − operator. If you go through that grammar section, the only place the − sign appears is after e in a number like 1.2345e-67.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:(...) there is no such thing as a negative number literal

Isn't 0xFFFFFFFF negative?
By negative I mean representing negative value (in this case -1), not preceded by - sign.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Joshua Bloch says that hex literals are unsigned. I shall see if I can find where, and whether I am mistaken.
But as you say, as soon as you put that hex literal into an int variable, it turns into minus1.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The minus sign is never part of the literal. It is the sign change operator (uminus) and is separate from the number. If you change −1 to − 1 you will get no change to the program semantics. There is an example in the JLS (=Java Language Specification), Java7 version
It is a compile-time error if a decimal literal of type int is larger than 2147483648 (2³¹), or if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator (§15.15.4).
That is in the same section as I quoted earlier. When you write -2147483648 you are not writing the negative number, but the uminus operator and the number unsigned. You cannot allow anything to intervene between the number and the minus. I know you can't let /*comments*/ come between them; have never tried whitespace.
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok got it..Thanks all
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!