Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Interesting Java Language Puzzle

DZone's Guide to

Interesting Java Language Puzzle

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}