Over a million developers have joined DZone.
Platinum Partner

Interesting Java Language Puzzle

· Java Zone

The Java Zone is brought to you in partnership with AppDynamics. Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems.

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.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}