This is Bad Assignment Operator Using java code :
Example Code :
public class Arithmetic {
public static void main(String args[]) {
byte b; // this is byte type Data
int i = 127; // this is int type Data
b = i;
System.out.println(b);
}
}
---------------------------------------------------------------------------------------
This Code Output Is Compile error :
1 error found:
File: D:\ezcod\CODE\Code_lits 01\BadAssignment.java [line: 5]
Error: BadAssignment.java:5: error: incompatible types: possible lossy conversion from int to byte
b = i;
^
1 error
How to fix this error......💪
1.You can CAST int data to byte
b =(byte) i;
public class Arithmetic {
public static void main(String args[]) {
byte b; // this is byte type Data
int i = 127; // this is int type Data
b = (byte) i;// this is casting
System.out.println(b);
}
}
2. OR byte Data to int
i =(int) b;
public class Arithmetic {
public static void main(String args[]) {
byte b; // this is byte type Data
int i = 127; // this is int type Data
i = (int) b;// this is casting
System.out.println(b);
}
}
Tags:
javacode