Java char is also an int?
Asked Answered
H

7

6

I was trying to get some code done for class:

public int getValue(char value) {
    if (value == 'y') return this.y;
    else if (value == 'x') return this.x;

Since I might not be able to return anything in the end, it told me to do this at the end:

return value;

This surprised me because the return type for the method was of type int. Yet, it was telling me to return a char! I'm using eclipse, and accustomed to the endless number of warnings and stuff, this was a major surprise.

So, is a char really an int? Why is this happening?

Handcart answered 8/6, 2015 at 19:54 Comment(4)
docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.2Maudmaude
Each char has a char code associated with it. The char code is an integer. When you cast char to int you get the char code for that character...Oubliette
So it would essentially return the ascii value associated with the char?Handcart
@Oubliette What you are calling "char code" is a UTF-16 code unit, one or two of which encode a Unicode codepoint, one base codepoint and any number of "combining character" codepoints form a grapheme, which is one textual element, such as Ç.Piegari
M
7

The Java Language Specification states

When a return statement with an Expression appears in a method declaration, the Expression must be assignable (§5.2) to the declared return type of the method, or a compile-time error occurs.

where the rules governing whether one value is assignable to another is defined as

Assignment contexts allow the use of one of the following:

and

19 specific conversions on primitive types are called the widening primitive conversions:

  • char to int, long, float, or `double

and finally

A widening primitive conversion does not lose information about the overall magnitude of a numeric value in the following cases, where the numeric value is preserved exactly: [...]

A widening conversion of a char to an integral type T zero-extends the representation of the char value to fill the wider format.

In short, a char value as the expression of a return statement is assignable to a return type of int through widening primitive conversion.

Maudmaude answered 8/6, 2015 at 20:6 Comment(0)
N
2

A char is smaller than an int, so you can return it and it will prepend zeroes to make a longer number. That's not the right thing to return - in your case I'd probably throw an exception instead; however, the editor suggested it because it's something that you're allowed to return and you need to return something.

The following code is legal:

char c = 'h';
int i = c;
Nonjuror answered 8/6, 2015 at 19:58 Comment(0)
P
2

In computing, everything is numbers! Just bits and bytes.

int, char, byte, short and long are just numbers. A char is just a number that the compiler knows is usually used for displaying the character represented by the particular number (e.g. 32 = space, 48 = zero, etc).

A String is a sequence of numbers and other stuff, so a bit more complicated. We don't want to go there.

An int is a four byte number and a char is a two byte number, so you can fit any char number in an int.

The designers of Java just decided they would let you convert from char to int without requiring any special casts or conversions.

Populous answered 8/6, 2015 at 20:2 Comment(0)
A
2

A char is not an int. However, it is an integral type. That is to say, it's considered to be a whole number that can be converted to and from other integral types (long,short,byte and int), according to the Java Language Specification.

Basically what this means is that it is assignment-compatible to int. Its value is between 0 and 65535, and if you assign it to an int or cast it to an int and print it, you'll get the UTF-16 value of the character it represents.

Agouti answered 8/6, 2015 at 20:3 Comment(0)
W
1

There is an implicit and natural conversion from int to char and vice-versa. Note that you thus have the usual arithmetic defined on charm which comes very handy when you want, let's say, to iterate on the alphabet :

for (char c='a' ; c<='z' ; c++) { ... }

However, note that a char is 2 bytes long whereas an int is 4 bytes long, so casting an int down to a char may result in an integer overflow.

Wyoming answered 8/6, 2015 at 19:59 Comment(4)
Alright, thanks. Basically, it will implicitly cast to an int, and this is a one way only road?Handcart
Anyways, perfect answer. I didn't know it would just cast it implicitly... I'm used to having everything explicit (I'm come from a pythonic world) :)Handcart
Please note that a char is two bytes long. It's basically an unsigned short.Maryellen
Um, what? Java won't implicitly cast int to char. You can do it explicitly, but it doesn't work the same both ways.Mertens
F
1

By definition (in java) a char is an 8bit unsigned integer. (0 to 256)

An int an 32bit signed integer. (−2.147.483.648 to 2.147.483.647)

char a = 65;                    //65 is the ASCII Number of 'A'
System.out.println(a);
>>> A

b = a + 1
System.out.println(b);
>>> B

java autoboxing converts char to int and vice versa

Fundamental answered 8/6, 2015 at 20:7 Comment(0)
B
-2

Hope this little example solves your confusion:

public int getValue(int value) { 
    if (value == 'y') 
        return this.y; 
    else if (value == 'x') 
        return this.x; 
}

If you pass char as an int like getValue('x'), it would return the value of the int.

Butch answered 8/6, 2015 at 20:9 Comment(2)
But what if value is d? Then I don't want to return x.Handcart
it will return the Ascii valdue of the returned charButch

© 2022 - 2024 — McMap. All rights reserved.