Java char is also int?

I tried to get the code for the class:

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

Since I may not have been able to return anything at the end, he told me to do it at the end:

 return value; 

This surprised me because the return type for the method was of type int . However, he told me to return char ! I use eclipse and am used to an infinite number of warnings, etc. This was a serious surprise.

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

+5
source share
7 answers

The Java language specification contains

When an Expression expression of a declaration appears in the return method, Expression must be assigned (Β§5.2) the declared return type of the method , or a compile-time error occurs.

where the rules governing the assignment of one value to another are defined as

Destination contexts allow you to use one of the following values:

and

19 specific transformations on primitive types are called expanding primitive transformations:

  • char before int , long , float or `double

and finally

An expanding primitive conversion does not lose information about the total value of a numerical value in the following cases when the numerical value is stored exactly: [...]

The expanding conversion of a char to the integral type T zero - extends the representation of the char value to fill in a wider format.

In short, the value of char as an expression of the return assigned to the return type int by extending the primitive transform.

+6
source

A char less than int , so you can return it and it will add zeros to make a longer number. This is not the right thing to return - in your case, I will most likely throw an exception; however, the editor suggested this because it is what you are allowed to return and you need to return something.

The following code is legal:

 char c = 'h'; int i = c; 
+1
source

In the calculations, all the numbers! Just bits and bytes.

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

A string is a sequence of numbers and other things, so it’s a bit more complicated. We do not want to go there.

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

The Java designers simply decided that they would let you convert from char to int without any special tricks or conversions.

+1
source

A char not an int . However, it is an integral type. In other words, it is considered an integer that can be converted to and from other integral types ( long , short , byte and int ), according to the Java language specification .

This basically means that it is compatible with int assignment. Its value is from 0 to 65535, and if you assigned it to int or passed it to int and printed, you will get the UTF-16 value of the character it represents.

+1
source

There is an implicit and natural conversion from int to char and vice versa. Note that you thus have the usual arithmetic defined on char m, which is very convenient if you want, let's say, iterate alphabetically:

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

However, note that a char is 2 bytes long, while int is 4 bytes long, so dropping int to char can lead to integer overflows.

0
source

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

int int 32-bit signed integer. (-2.1447,483,648 to 2,1447,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

0
source

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 int as getValue('x') , it will return int .

-2
source

All Articles