#226 byte b3 = 1; byte b4 = -b3; //should not compile

open
nobody
None
5
2005-06-22
2005-06-22
Nigel
No

The following code should not compile:
> cat t9.java
public class t9 {
public static void main(java.lang.String[] $0s)
{
setStrictJava(true);
byte b2 = 128; //should not
compile
byte b1 = -b2; //should not
compile
System.out.println("b1 = "+b1+"
b2 = "+b2);
byte b3 = 1;
byte b4 = -b3; //should not
compile
System.out.println("b3 = "+b3+"
b4 = "+b4);
return;
}
}
Process cat exited with code 0

Which it does not:
> javac t9.java
t9.java:3: cannot find symbol
symbol : method setStrictJava(boolean)
location: class t9
setStrictJava(true);
^
t9.java:4: possible loss of precision
found : int
required: byte
byte b2 = 128; //should not
compile
^
t9.java:5: possible loss of precision
found : int
required: byte
byte b1 = -b2; //should not
compile
^
t9.java:8: possible loss of precision
found : int
required: byte
byte b4 = -b3; //should not
compile
^
4 errors
Process javac exited with code 1

But BeanShell runs it with interesting results:
> java -classpath ../bsh-2.0b5.jar bsh.Interpreter t9.java
b1 = -128 b2 = -128
b3 = 1 b4 = -1
Process java exited with code 0

Discussion