problem about jls-Collection of common programming errors


  • chuacw
    java jvm jls
    The following Java code generates the following JVM bytecode.I’m curious why the code from offset 31 to offset 36 is generated. Nothing in the JLS7 or JVM7 specification talks about this. Did I miss anything?Even if I remove the println statements, the code (offset 31 to offset 36) still get generated, only at an earlier location, since the println call has been removed.// Java codevoid testMonitor() {Boolean x = new Boolean(false);synchronized(x) {System.out.println(“inside synchronized”);Syste

  • Paul Vargas
    java multithreading concurrency final jls
    The below text is from jls http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5.3Even then, there are a number of complications. If a final field is initialized to a compile-time constant expression (§15.28) in the field declaration, changes to the final field may not be observed, since uses of that final field are replaced at compile time with the value of the constant expression.Can anyone Please give me better explanation for the above. i couldn’t understand the statement

  • hvgotcodes
    java jdk jre jls
    Java is the language, the JRE is the runtime environment, and the JDK are the development tools? The JLS is the spec that says that the JRE must do x,y,z, and therefore makes Java what it is? Are these views correct, and if not can someone enlighten me?Are there any things we think are Java but are not? I think I heard that about Android — is it true?

  • atamanroman
    java jls
    This ugly piece of code does compile but throws NPE if s == nullpublic static boolean isNullOrEmpty(String s) {return s != null ? s.isEmpty() : null; }while this does not (as expected):public static boolean isNullOrEmpty(String s) {if(s != null)return s.isEmpty();elsereturn null; }I know both of them are plainly wrong, but as I found the first piece of code in our sources, I was quite surprised it did compile.Edit: Here’s the relevant part of the JLS from Java 7. I guessed the first statement wo

  • Bart van Heukelom
    java scope stack javac jls
    In Java this is the case:public void method() {if (condition) {Object x = ….;}System.out.println(x); // Error: x unavailable }What I’m wondering is this: Is the fact that x is limited to the scope of the if-statement just a feature of the Java compiler, or is x actually removed from the stack after the if-statement?

  • palacsint
    java multithreading thread-safety final jls
    I am trying to simply test out the initialization safety of final fields as guaranteed by the JLS. It is for a paper I’m writing. However, I am unable to get it to ‘fail’ based on my current code. Can someone tell me what I’m doing wrong, or if this is just something I have to run over and over again and then see a failure with some unlucky timing?Here is my code:public class TestClass {final int x;int y;static TestClass f;public TestClass() {x = 3;y = 4;}static void writer() {TestClass.f = ne

  • Piotr Findeisen
    java generics javac jls
    The following code import java.util.*; import java.io.*;@SuppressWarnings(“unchecked”) List<Serializable> list = (List<Serializable>) (List<?>)Collections.singletonList(new Object());for (Object el : list) { // -> ClassCastExceptionSystem.out.println(el); }is correct Java (even though the code is suspicious). Using javac and java 6 it throwsException in thread “main” java.lang.ClassCastException: java.lang.Object cannot be cast to java.io.Serializablewhile it runs without er

  • John Assymptoth
    java increment decrement jls
    When I try to write a postfix/prefix in/decrement, followed by a post/prefix in/decrement, I get the following error: Invalid argument to operation ++/–.But, according to JLS:PostIncrementExpression:PostfixExpression ++andPostfixExpression:PrimaryExpressionNamePostIncrementExpressionPostDecrementExpressionso writing:PostfixExpression ++ ++should be possible… Any thoughts?

  • BeeOnRope
    java concurrency variable-assignment jls
    Given the following class:class Foo {public volatile int number;public int method1() {int ret = number = 1;return ret;}public int method2() {int ret = number = 2;return ret;} }and given multiple threads calling method1() and method2() concurrently on the same Foo instance, can a call to method1() ever return anything other than 1?

  • John Assymptoth
    java capture jls
    I’ve noticed JLS talks of 5.1.10 Capture Conversion, but I fail to understand what they are.Can anyone explain them to me/give examples?

Web site is in building