简体   繁体   中英

What is the purpose and meaning of (char)i or (int)i in java?

From this example

FileInputStream fis=new FileInputStream("lalu.txt");
int i=0;

while(i=fis.read() !=-1){
    System.out.println((char)i);  
}

My purpose to ask this question is why the char is wrapped in parentheses here?

why the char is wrapped in parentheses here?

Whoa, some magic happens here? Nope, it's very common in Java, known as Explicit Type Casting .

A cast expression converts, at run time, a value of one numeric type to a similar value of another numeric type; or confirms, at compile time, that the type of an expression is boolean; or checks, at run time, that a reference value refers to an object whose class is compatible with a specified reference type.

The parentheses and the type they contain are sometimes called the cast operator.

CastExpression:
( PrimitiveType ) UnaryExpression
( ReferenceType ) UnaryExpressionNotPlusMinus

As you declare i as an int and you cast it char (in the parenthesis you write char ) which means you are doing narrowing primitive conversion .

According to §5.1.3:

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

By default, the int data type is a 32-bit signed two's complement integer, and the char data type is a single 16-bit Unicode character(reference here ), which means

  • int is 4 bytes, and
  • char is 2 byte (Unicode character).

So, when you narrowing integer to character by explicit casting, you just simply telling the compiler to discard the first two byte from the integer and treat i as a character!

Let's see with an example:

int i = 0x41990041; // hex 41990041 is equivalent to decimal 1100546113
System.out.println("printing integer: " + i);
System.out.println("printing char representation of int: " +(char)i);

The output is:

printing integer: 1100546113

printing char representation of int: A

Here, I took a random 4 bytes value into integer i . Look carefully, the first two bytes 4199 is discarded when the casting performed in run time and the last two bytes 0041 is printed as character. See here the character representation of hex value 0041 is A .

first they are not brackets they are parentheses.

the parentheses are there because this is how compiler understands that you are casting it to other. else it will throw compile time error.

A type name in parentheses, followed by a variable, (such as your (char) i ) is called a 'type cast'. It tells the compiler, "Treat this variable as this type instead of its variable type", ie read i as a character instead of an integer. You have to start with i being an integer because there is no character numbered -1 to show when the end of file is reached. After it is compared with -1 , it can be casted (changed) to a char , because that is what the variable truly represents. For instance, if the first character of your file was 'N' , i would be set to 'N' 's ASCII value, 78. (char) i changes 78 back to 'N' . System.out.println(i) in this instance would print 78 instead of N .

To simplify, this is called type casting. It tells the compiler to read a variable as a different type.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM