Java CASE, why am I getting a complete set of int int with and without use ''

As a Java beginner, I am currently playing with the case argument.

I set: int d = '1'; And with: System.out.println("number is: " + d); , this returns 51.

Now I found out that if I installed it as: int d = 1; he will return 1.

Now my question is: why does it return 49 when I set it to "3"? What difference does make make?

My code that returns 49:

 int a = '1'; switch(a) { case '1' : System.out.println("Good"); break; case '2' : case '3' : System.out.println("great"); break; default : System.out.println("invalid"); } System.out.println("value: " + a); 
+5
source share
6 answers

With '(single quotes) around a character, you tell the compiler that the value between them is a character (type of char variable in Java). Typically, computers understand only numbers, so characters are also displayed as numbers in memory. The value of the symbol "1" is 49. You want to actually store the value 1 in memory and use it to calculate things. So, you must tell the compiler that you want this to be interpreted as a real number, and not as a symbol.

Summarizing:

  • '1' is the character that prints number 1
  • 1 is the actual number 1

This means that 1 and '1' have different integer values.

Here you can see the integer value of the most commonly used characters in the encoding: ASCII table

+2
source

'1' is the character '1', whose integer value is 49. Each character has a numerical value in the range from 0 to 2 ^ 16-1. Therefore, 1 and '1' are different integer values.

+7
source

In Java, character values ​​are stored as int inside. This means that when you assign a char value using "A" for type int, the compiler passes the value of char to int, and therefore the value of the unit of code in UTF-16 is stored in the variable int.

Try also:

  int yourInt = 33; char ch = (char) yourInt; System.out.println(yourInt); System.out.println(ch); 
+1
source

1 is interpreted as a symbol whose value is 49.

make:

 int a = 1; switch(a) { case 1: ... 

Or, if you go with the current code:

 System.out.println("value: " + Integer.parseInt((char)a+''); 
0
source

This is because, for some strange reason, java treats char as short. Your expression "1" is a constant expression of type char. It is stored internally as some numerical value. Usually everything is fine with this approach, for example, System.out.println('1') will print exactly what you expect.

But when you write int a = '1'; , char is converted to int because char behaves like a short int there.

PS: if there was no implicit conversion between char and int (which in any case does not make sense), you received a compilation error.

0
source

When you declare int d = '1'; then 1 is not considered as an integer, it is a symbol. It converts to 49 according to its Unicode value. Similarly, character "3" will be closed until 51. Jvm does an implicit cast type from character to integer. you should try this code

 char c = '3'; int a ='1'; System.out.println(c); System.out.println((int)c); 

You will get the result as

3

51

49

0
source

All Articles