-1

Assuming

FileInputStream in=new FileInputStream ("file.txt");
char c=in.read();

Gives me an error:

java: incompatible types: possible lossy conversion from int to char

, but

char b=87;

Does not give me any error,rather it gives me the Unicode character

  • why does char c variable gives an error since in.read() returns an int value that can be a Unicode character but char b variable doesn't?
Atolz
  • 19
  • 4
  • 1
    Does this answer your question? [Can assign integer value to char but can't assign integer variable to char](https://stackoverflow.com/questions/26446512/can-assign-integer-value-to-char-but-cant-assign-integer-variable-to-char) – Savior Apr 09 '20 at 12:35

2 Answers2

0

Because an int can also be 2147000000 and that cannot be converted to a char. When you specify 87 the compiler knows that falls within the expected char boundaries but when you are receiving it from somewhere else there is no way to ensure data safety.

InsertKnowledge
  • 1,012
  • 1
  • 11
  • 17
-1

An int type is generally 32-bit (system dependent) while char is generally 8-bit. Therefore you are converting 32-bit to 8-bit and potentially losing data.

Dan Eisenhut
  • 141
  • 1
  • 2
  • 8