Over a million developers have joined DZone.

Interesting Java Language Puzzle

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

We ran into an unexpected result as part of Java's auto boxing feature. At least I think it is related to auto boxing. See the following code snippet
package com.foo;

public class AutoBoxTest {

	public static void main(String[] args) {
		Boolean abc = new Boolean(false);
		setMe(getThisValue() || abc == null? false: abc); 
	}
	
	static public Boolean getThisValue() {
		return new Boolean(true);
	}
	static public void setMe(Boolean pValue) {
		System.out.println("Value is : " + pValue);
	}
}

The result? Value is : false

Huh? It turns out changing the call to setMe() as follows "fixes" the issue, I'd just like to know if anyone can explain why?

   Boolean abc = new Boolean(false);
   setMe(getThisValue() || (abc == null? false: abc)); 

The result is: Value is : true Strange.

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}