Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This was an exam question which I couldn't complete.

How do you get the following java code to print false by only editing code within the MyClass constructor?

public class MyClass{        
    public MyClass(){
    }

    public static void main(String[] args) {            
        MyClass m = new MyClass();
        System.out.println(m.equals(m));
    }
}

You are NOT allowed to override the equals method, or change any of the code within the main method. The code must run without the program crashing.

According to my research, you can't set a Java object reference to null when you instantiate a class. So I'm officially stumped.

share|improve this question
9  
System.out.println(false); ;) – Reimeus 20 hours ago
    
Well, what is the equals() method? – Gendarme 20 hours ago
    
I think this is a puzzle in Java Puzzlers. I've definitely seen it before. – Paul Boddington 20 hours ago
4  
@vk239 The comments under your deleted answer were wrong. You didn't override equals you overloaded it. – Paul Boddington 20 hours ago
3  
@PaulBoddington That's a fair point, although it does still say "by only editing code within the MyClass constructor". – arshajii 20 hours ago

Something along these lines, I would guess:

public MyClass() {
    System.out.println(false);
    System.exit(0);
}

EDIT: I found a puzzle very similar to yours in Java Puzzlers, except in that question the only restriction was that you could not override equals, which basically makes the solution to overload it instead and simply return false. Incidentally, my solution above was also given as an alternative answer to that puzzle.

share|improve this answer
    
If that was the answer the exam was looking for, it is a bad exam. – Raedwald 4 hours ago

That was tough!!

public MyClass() {
    System.setOut(new PrintStream(new FilterOutputStream(System.out) {
        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            if(new String(b).contains("true")) {
                byte[] text = "false".getBytes();         
                super.write(text, 0, text.length);
            }
            else {
                super.write(b, off, len);
            }
        }
    }, true));
}

Or Paul Boddington's simplified version:

PrintStream p = System.out; 
System.setOut(new PrintStream(p) { 
    @Override
    public void println(boolean b) { 
        p.println(false); 
    }
});
share|improve this answer
4  
hahahahah I laughed to this answer :) – Johnny Willer 20 hours ago
2  
@JohnnyWiller I feel clever which is always a sign that I'm overcomplicating things lol – flkes 20 hours ago
2  
Damn you beat me by a few minutes! You can simplify this a bit. It can just be final PrintStream p = System.out; System.setOut(new PrintStream(p) { public void println(boolean b) { p.println(false); }}); – Paul Boddington 20 hours ago
1  
Great answer - I've upvoted. You beat me because I was not aware of setOut. I was trying to use field.set which doesn't seem to work. – Paul Boddington 20 hours ago
3  
@flkes Further shortening it: System.setOut(new PrintStream(System.out) { @Override public void println(boolean b) { super.println(false); } }); No need for a p variable. – AJNeufeld 19 hours ago

Another solution is

public MyClass() {
    new PrintStream(new ByteArrayOutputStream()).println(true);
    try {
        Field f = String.class.getDeclaredField("value");
        f.setAccessible(true);
        f.set("true", f.get("false"));
    } catch (Exception e) {
    }
}

The first line is needed because it is necessary for the string literal "true" to be encountered in the PrintStream class before the backing array is modified. See this question.

share|improve this answer
    
You must be loading some static code when you call println(boolean b). very interesting – flkes 16 hours ago
    
Looks like you need to get Java to intern() the String("true") into the pool. You can likely get away with doing something like String t = "true" to achieve the same effect. – Boris the Spider 7 hours ago
1  
@BorisTheSpider Weirdly it doesn't work. It seems you have to encounter the right "true" literal - i.e. the one in the PrintStream class. See the linked question for the true extent of how strange this all is. – Paul Boddington 7 hours ago

This is my solution

public class MyClass {

    public MyClass() {
        System.out.println("false");

        // New class
        class NewPrintStream extends PrintStream {
            public NewPrintStream(OutputStream out) {
                super(out);
            }

            @Override
            public void println(boolean b) {
                // Do nothing
            }
        }

        NewPrintStream nps = new NewPrintStream(System.out);
        System.setOut(nps);
    }

    public static void main(String[] args) {
        MyClass m = new MyClass();
        System.out.println(m.equals(m));
    }
}

Basically, this is the variation of @fikes solution.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.