autoboxing

Is autoboxing possible for the classes I create?

隐身守侯 提交于 2019-11-26 20:42:10
Is there any way to use autoboxing for the classes I create? For example, I have this subclass of Number . public class UnsignedInteger extends Number { int n; public UnsignedInteger(int n) { if(n >= 0) this.n = n; else throw new IllegalArgumentException("Only positive integers are supported"); } } Now, UnsignedInteger i = new UnsignedInteger(88); works perfectly fine, but is there any way to make this compile : UnsignedInteger i = 88; ? It won't for me. Thanks in advance! rgettman In short, no. There's no way to get that to compile. Java only defines a limited set of pre-defined boxing

Comparing boxed Long values 127 and 128

时光怂恿深爱的人放手 提交于 2019-11-26 19:25:37
I want to compare two Long objects values using if conditions. When these values are less than 128 , the if condition works properly, but when they are greater than or equal to 128 , comparison fails. Example: Long num1 = 127; Long num2 = 127; if (num1 == num2) { // Works ok } Comparison on the code above works properly, but fails in the code below: Long num1 = 128; Long num2 = 128; if (num1 == num2) { // Does NOT work } Why is there a problem in comparing Long variables with values greater than 127 ? If the variables data types are changed to long primitives , then the comparisons work for

Java Modifying Elements in a foreach

烂漫一生 提交于 2019-11-26 18:36:39
问题 I'm learning Java on my own; and therefore the code below has no function other than for learning/testing. Essentially I'm trying to modify the elements of an Integer array (namely, halving them) whilst in a foreach loop. I should note that I'm not re-ordering, adding, or deleting elements; simply changing their values. Here is my code: Logger.describe("Now copying half of that array in to a new array, and halving each element"); Integer[] copyArray = new Integer[DEFAULT_SAMPLE_SIZE / 2];

NullPointerException with autoboxing in ternary expression

夙愿已清 提交于 2019-11-26 17:44:07
问题 Run the following Java code: boolean b = false; Double d1 = 0d; Double d2 = null; Double d = b ? d1.doubleValue() : d2; Why is there a NullPointerException? 回答1: The return type of the conditional expression b ? d1.doubleValue : d2 is double . A conditional expression must have a single return type. Following the rules for binary numeric promotion, d2 is autounboxed to a double , which causes a NullPointerException when d2 == null . From the language spec, section §15.25: Otherwise, if the

NullPointerException through auto-boxing-behavior of Java ternary operator

眉间皱痕 提交于 2019-11-26 16:40:48
I tripped across a really strange NullPointerException the other day caused by an unexpected type-cast in the ternary operator. Given this (useless exemplary) function: Integer getNumber() { return null; } I was expecting the following two code segments to be exactly identical after compilation: Integer number; if (condition) { number = getNumber(); } else { number = 0; } vs. Integer number = (condition) ? getNumber() : 0; . Turns out, if condition is true , the if -statement works fine, while the ternary opration in the second code segment throws a NullPointerException . It seems as though

Why doesn't autoboxing overrule varargs when using method overloading in Java 7?

◇◆丶佛笑我妖孽 提交于 2019-11-26 14:14:09
问题 We have a class LogManager in our Java project which looks like this: public class LogManager { public void log(Level logLevel, Object... args) { // do something } public void log(Level logLevel, int value, Object... args) { // do something else } } When compiling the project with OpenJDK 6 under Debian everyting works fine. When using OpenJDK 7 the build (done with ant) produces the following errors and the build fails: [javac] /…/LogManager.java:123: error: reference to log is ambiguous,

Java: Array of primitive data types does not autobox

ぐ巨炮叔叔 提交于 2019-11-26 13:43:29
I have a method like this: public static <T> boolean isMemberOf(T item, T[] set) { for (T t : set) { if (t.equals(item)) { return true; } } return false; } Now I try to call this method using a char for T : char ch = 'a'; char[] chars = new char[] { 'a', 'b', 'c' }; boolean member = isMemberOf(ch, chars); This doesn't work. I would expect the char and char[] to get autoboxed to Character and Character[] , but that doesn't seem to happen. Any insights? There is no autoboxing for arrays, only for primitives. I believe this is your problem. Why would char[] be boxed to Character[] ? Arrays are

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

别等时光非礼了梦想. 提交于 2019-11-26 13:03:32
The following code throws NullPointerException : int num = Integer.getInteger("123"); Is my compiler invoking getInteger on null since it's static? That doesn't make any sense! What's happening? polygenelubricants 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 Java Language Guide/Autoboxing

Java autoboxing and ternary operator madness

筅森魡賤 提交于 2019-11-26 11:21:27
问题 Just spent a frustrating couple of hours debugging this code: LinkedHashMap<String, Integer> rsrqs = new LinkedHashMap<String, Integer>(); Integer boxedPci = 52; Integer boxedRsrq = boxedPci != null ? rsrqs.get(boxedPci.toString()) : -1; The above produces a NullPointerException. The below code doesn\'t: LinkedHashMap<String, Integer> rsrqs = new LinkedHashMap<String, Integer>(); Integer boxedPci = 52; Integer boxedRsrq = boxedPci != null ? rsrqs.get(boxedPci.toString()) : Integer.valueOf(-1)

Booleans, conditional operators and autoboxing

佐手、 提交于 2019-11-26 11:13:30
Why does this throw NullPointerException public static void main(String[] args) throws Exception { Boolean b = true ? returnsNull() : false; // NPE on this line. System.out.println(b); } public static Boolean returnsNull() { return null; } while this doesn't public static void main(String[] args) throws Exception { Boolean b = true ? null : false; System.out.println(b); // null } ? The solution is by the way to replace false by Boolean.FALSE to avoid null being unboxed to boolean --which isn't possible. But that isn't the question. The question is why ? Are there any references in JLS which