How can “a

This is not possible with primitive types. You can achieve it with boxed Integers: Integer a = new Integer(1); Integer b = new Integer(1); The <= and >= comparisons will use the unboxed value 1, while the != will compare the references and will succeed since they are different objects.

How do I convert Double[] to double[]?

If you don’t mind using a 3rd party library, commons-lang has the ArrayUtils type with various methods for manipulation. Double[] doubles; … double[] d = ArrayUtils.toPrimitive(doubles); There is also the complementary method doubles = ArrayUtils.toObject(d); Edit: To answer the rest of the question. There will be some overhead to doing this, but unless the array … Read more

When using == for a primitive and a boxed value, is autoboxing done, or is unboxing done

It is defined in the JLS #15.21.1: If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2). And JLS #5.6.2: When an operator applies binary numeric promotion to a … Read more

Why do we use autoboxing and unboxing in Java?

Some context is required to fully understand the main reason behind this. Primitives versus classes Primitive variables in Java contain values (an integer, a double-precision floating point binary number, etc). Because these values may have different lengths, the variables containing them may also have different lengths (consider float versus double). On the other hand, class … Read more

Why does int num = Integer.getInteger(“123”) throw NullPointerException?

The Big Picture There are two issues at play here: Integer getInteger(String) doesn’t do what you think it does It returns null in this case the assignment from Integer to int causes auto-unboxing Since the Integer is null, NullPointerException is thrown To parse (String) “123” to (int) 123, you can use e.g. int Integer.parseInt(String). References … Read more

Comparing boxed Long values 127 and 128

TL;DR Java caches boxed Integer instances from -128 to 127. Since you are using == to compare objects references instead of values, only cached objects will match. Either work with long unboxed primitive values or use .equals() to compare your Long objects. Long (pun intended) version Why there is problem in comparing Long variable with … Read more

Weird Integer boxing in Java

The true line is actually guaranteed by the language specification. From section 5.1.7: If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions … Read more

Booleans, conditional operators and autoboxing

The difference is that the explicit type of the returnsNull() method affects the static typing of the expressions at compile time: E1: `true ? returnsNull() : false` – boolean (auto-unboxing 2nd operand to boolean) E2: `true ? null : false` – Boolean (autoboxing of 3rd operand to Boolean) See Java Language Specification, section 15.25 Conditional … Read more

Why do people still use primitive types in Java?

In Joshua Bloch’s Effective Java, Item 5: “Avoid creating unnecessary objects”, he posts the following code example: public static void main(String[] args) { Long sum = 0L; // uses Long, not long for (long i = 0; i <= Integer.MAX_VALUE; i++) { sum += i; } System.out.println(sum); } and it takes 43 seconds to run. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)