4

The following line of Java code produce error.
Even though datatypes in java are signed?

char c = -128;  
2

9 Answers 9

13

Char is the one data type that isn't signed in java. Its a 16 bit unsigned integer.

6
  • 1
    Is boolean a signed data type?
    – auser
    Jul 27, 2012 at 14:43
  • 1
    @JaynathanLeung Please elaborate. Is false negative and true positive? and where is zero in that scheme?
    – auser
    Jul 27, 2012 at 14:55
  • 1
    a boolean is 1-bit. Either a 1 or a 0, denoting true or false. Jul 27, 2012 at 14:57
  • I see you've changed your comment from yes it is to no it isn't.
    – auser
    Jul 27, 2012 at 15:01
  • 3
    you mean -1 or 0. It's a 2's complement bit. But in all seriousness, a boolean can only be "true" or "false" in java. It's probably implemented in bytecode as a signed 8 bit integer, but its not well defined. So it's not really worth bothering about.
    – yhyrcanus
    Jul 27, 2012 at 15:01
11

Straight from the Oracle tutorial for Java datatypes.

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

4

this is because -128 is int. you need to cast to char. please be aware that char is unsigned type, so after

char c = (char) -1;
System.out.print(Integer.valueOf(c).toString());

you will get 65535

0
1

It has a minimum value of '\u0000' (or 0) and a maximum value of'\uffff' (or 65,535 inclusive).

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

1

As per Oracle Java doc

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Therefore, you cannot really assign negative values to char and char c = -128 will result in error.
When you try to assign negative values to a char and type-cast it , it will rotate that value through the other (max) end.

char c;
c = (char) 65;    // 'A'
c = (char) -100;  // 'ワ' and it results in 65,535(inclusive) - 100 = (char) 65,436
c = (char) 65436; // 'ワ'

int i;
i = (char) 65;    // 65
i = (char) -1;    // 65535 : notice how the char value cycles through the other end
i = (char) -100;  // 65436 : same as (char) -100. Here, int returns 65,436.
0

Cast it:

char c = (char) -128;  

Though this will probably not behave like you expect. Perhaps a byte would be better?

0

If you ever find yourself confused about min or max values of primitives, find their object equivalent and look for the MIN_VALUE or MAX_VALUE constant. Eg:

http://docs.oracle.com/javase/6/docs/api/java/lang/Character.html#MIN_VALUE

0

char holds 16 bit unsigned value.We can assign integer to char but positive upto 2 raise to the power 16 which is 65536-1 as we start from 0.

if we need to assign -ve values that some of the bit will be loose and we will need to typecast.In this situation compiler will store the sign bit along with the actual value and the last bits will be dropped. char c=(char) -128 will not give compile error but you will get absurd results

0

You can also check whether an int value is valid as a character code by using Character.isValidCodePoint(int codePoint) method:

boolean isValid = Character.isValidCodePoint(-128); // false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.