How can a Java variable be different from itself?

前端 未结 10 1836
日久生厌
日久生厌 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条回答
  •  自闭症患者
    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");
            }
        }
    }
    

提交回复
热议问题