autoboxing

java : Understanding Arrays.asList(T…array) method for primitive types

一曲冷凌霜 提交于 2019-12-28 04:25:12
问题 I wrote following code and was surprised to see the output: Integer a = 211; int b = 211; int[] array = {210,211,212}; System.out.println(Arrays.asList(array).contains(a)); System.out.println(Arrays.asList(array).contains(b)); Output: false false I found this question and some other questions linked to it and learned that asList method doesn't Autobox stuffs. I checked the returned type in eclipse javadoc preview: I couldn't quite understand this return type. int[] is an object and not a

When overloading compiler does not prefer primitive to Object only in case of varargs parameter presence

坚强是说给别人听的谎言 提交于 2019-12-24 12:11:58
问题 Please, could you help me to understand why compilation of first call of testVargArgsAutoboxingPriority fails? In case of second call compiler is able to select proper method by preferring primitive (first parameter) to Object but after varargs parameter addition compiler is not able to make the selection any more. Fail message is \jdk1.6.0_45\bin\javac.exe ocjp6/AutoBoxingOldStyleVarargsPriority.java ocjp6\AutoBoxingOldStyleVarargsPriority.java:7: reference to testVargArgsAutoboxingPriority

Java: Is it ok to set Integer = null?

纵饮孤独 提交于 2019-12-23 07:27:45
问题 I have a function that returns an id number if the argument exists in the database. If not, it returns null. Is this begging for a null pointer exception? Negative id numbers are not permitted, but I thought it would be clearer to have non-existent arguments returning null instead of an error code like -1. What do you think? private Integer tidOfTerm(String name) throws SQLException { String sql = "SELECT tid FROM term_data WHERE name = ?"; PreparedStatement prep = conn.prepareStatement(sql);

Why during autoboxing final long to Byte compilation error happens, but final int to Byte is ok?

南笙酒味 提交于 2019-12-23 07:27:44
问题 There is no error during auto-boxing of constants with int and short types to Byte , but constant with long type do has error. Why? final int i = 3; Byte b = i; // no error final short s = 3; Byte b = s; // no error final long l = 3; Byte b = l; // error 回答1: From JLS Sec 5.2, "Assignment contexts" (emphasis mine): In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int : A narrowing primitive conversion may be used if the type of the variable is

Wrappers of primitive types in arraylist vs arrays

筅森魡賤 提交于 2019-12-21 19:59:18
问题 In "Core java 1" I've read CAUTION: An ArrayList is far less efficient than an int[] array because each value is separately wrapped inside an object. You would only want to use this construct for small collections when programmer convenience is more important than efficiency. But in my software I've already used Arraylist instead of normal arrays due to some requirements, though "The software is supposed to have high performance and after I've read the quoted text I started to panic!" one

Autoboxing/Unboxing while casting Integer to int using 'cast' method

不想你离开。 提交于 2019-12-20 02:54:08
问题 Here is a very simple case: I am trying to cast an Object type to a primitive like this: Object object = Integer.valueOf(1234); int result1 = int.class.cast(object); //throws ClassCastException: Cannot convert java.lang.integer to int int result2 = (int)object; //works fine This is the source code of cast method of class 'Class' public T cast(Object obj) { if (obj != null && !isInstance(obj)) throw new ClassCastException(cannotCastMsg(obj)); return (T) obj; } private String cannotCastMsg

Should autoboxing be avoided in Java?

风格不统一 提交于 2019-12-19 13:07:15
问题 There are instances where a method expects a primitive type double and you pass a Double object as a parameter. Since the compiler unboxes the passed object will this increase memory usage or decrease performance? 回答1: This is what Java Notes says on autoboxing: Prefer primitive types Use the primitive types where there is no need for objects for two reasons. Primitive types may be a lot faster than the corresponding wrapper types, and are never slower. The immutability (can't be changed

Should autoboxing be avoided in Java?

假装没事ソ 提交于 2019-12-19 13:06:06
问题 There are instances where a method expects a primitive type double and you pass a Double object as a parameter. Since the compiler unboxes the passed object will this increase memory usage or decrease performance? 回答1: This is what Java Notes says on autoboxing: Prefer primitive types Use the primitive types where there is no need for objects for two reasons. Primitive types may be a lot faster than the corresponding wrapper types, and are never slower. The immutability (can't be changed

Why doesn't Java autoboxing extend to method invocations of methods of the autoboxed types?

巧了我就是萌 提交于 2019-12-18 12:43:27
问题 I want to convert a primitive to a string, and I tried: myInt.toString(); This fails with the error: int cannot be dereferenced Now, I get that primitives are not reference types (ie, not an Object) and so cannot have methods. However, Java 5 introduced autoboxing and unboxing (a la C#... which I never liked in C#, but that's beside the point). So with autoboxing, I would expect the above to convert myInt to an Integer and then call toString() on that. Furthermore, I believe C# allows such a

Overriding and return type compatibility

孤者浪人 提交于 2019-12-18 08:56:13
问题 The following compiles without any problem boolean flag = true; Boolean flagObj = flag; Now imaging the following scenario interface ITest{ Boolean getStatus(); } class TestImpl implements ITest{ public boolean getStatus(){ // Compile error: return type is incompatible return true; } } My question is about the compile error at the mentioned line. My Interface mentions return type as Boolean but the implemented method returns boolean ( the literal ) My question is, if Boolean and boolean are