Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0084 char and boolean are not compatible with each other

public class Main { public static void main(String[] argv) { char ch = 'a'; int num = 99; ch = num; } } Compiling the code above will generate the following error message. D:\>javac Main.java Main.java:5: possible loss of precision found : int required: char ch = num; ^ 1 error Java performs an automatic type conversion when storing a literal integer constant into variables of type byte, short, or long. public class Main { public static void main(String[] argv) { byte b = 1; } } But you cannot store a value out of the byte scope public class Main{ public static void main(String[] argv){ byte b = 11111; } } When compiling, it generates the following error message: D:\>javac Main.java Main.java:3: possible loss of precision found : int required: byte byte b = 11111; ^ 1 error