How can a Java variable be different from itself?

前端 未结 10 1834
日久生厌
日久生厌 2020-11-29 16:14

I am wondering if this question can be solved in Java (I\'m new to the language). This is the code:

class Condition {
    // you can change in the main
    p         


        
相关标签:
10条回答
  • One easy solution is:

    System.out.println("Gotcha!");if(false)
    if( a == a ){
      System.out.println("Not yet...");
    } else {
      System.out.println("Gotcha!");
    }
    

    But I don't know all the rules to this riddle...

    :) I know that this is a cheat, but without knowing all rules, is this the easiest solution to the question :)

    0 讨论(0)
  • 2020-11-29 16:43

    Create your own class System in tha same package with Condition.
    In this case your System class will hide java.lang.System class

    class Condition
    {
        static class System
        {
            static class out
            {
                static void println(String ignored)
                {
                    java.lang.System.out.println("Not ok");
                }
            }
        }
    
        public static void main (String[] args) throws java.lang.Exception
        {
            int x = 0;
            if (x == x) 
            {
               System.out.println("Not ok");
            } 
            else 
            {
               System.out.println("Ok");
            }
        }
    }  
    

    Ideone DEMO

    0 讨论(0)
  • 2020-11-29 16:44

    I managed to get a Gotcha! from this:

    volatile Object a = new Object();
    
    class Flipper implements Runnable {
      Object b = new Object();
    
      public void run() {
        while (true)  {
          Object olda = a;
          a = b;
          a = olda;
        }
      }
    
    }
    
    public void test() {
      new Thread(new Flipper()).start();
    
      boolean gotcha = false;
      while (!gotcha) {
        // I've added everything above this - I would therefore say still legal.
        if (a == a) {
          System.out.println("Not yet...");
        } else {
          System.out.println("Gotcha!");
          // Uncomment this line when testing or you'll never terminate.
          //gotcha = true;
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-29 16:46

    There are so many solutions:

    class A extends PrintStream {
        public A(PrintStream x) {super(x);}
        public void println(String x) {super.println("Not ok");}
        public static void main(String[] args) {
            System.setOut(new A(System.out));
            int x = 0;
            if (x == x) {
                System.out.println("Ok");
            } else {
                System.out.println("Not ok");
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:47

    By the Java Language Specifications NaN is not equal to NaN.

    Therefore any line that caused x to be equal to NaN would cause this, such as

    double x=Math.sqrt(-1);
    

    From the Java Language Specifications:

    Floating-point operators produce no exceptions (§11). An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no mathematically definite result produces NaN. All numeric operations with NaN as an operand produce NaN as a result. As has already been described, NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false and any != comparison involving NaN returns true, including x!=x when x is NaN.

    0 讨论(0)
  • 2020-11-29 16:47

    The replaced line could read.

    double x = Double.NaN;
    

    This would cause the gotcha to be printed.

    Java Language Specification (JLS) says:

    Floating-point operators produce no exceptions (§11). An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no mathematically definite result produces NaN. All numeric operations with NaN as an operand produce NaN as a result. As has already been described, NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false and any != comparison involving NaN returns true, including x!=x when x is NaN.

    0 讨论(0)
提交回复
热议问题