autoboxing

Is int.class.isInstance(Object) a contradiction?

你离开我真会死。 提交于 2019-12-10 13:43:50
问题 Here's an example: public boolean check(Class<?> clazz, Object o) { return clazz.isInstance(o); } check(int.class, 7); // returns false Since isInstance accepts an Object , it won't work with int , because int is a primitive type and gets autoboxed to Integer . So is it at all possible to write a generic check method? Or should I make sure that clazz is of type Class<? extends Object> ? 回答1: Not all Class objects represent classes / reference types; there are also Class objects that represent

Do C# generics prevent autoboxing of structs in this case?

送分小仙女□ 提交于 2019-12-10 12:38:15
问题 Usually, treating a struct S as an interface I will trigger autoboxing of the struct, which can have impacts on performance if done often. However, if I write a generic method taking a type parameter T : I and call it with an S , then will the compiler omit the boxing, since it knows the type S and does not have to use the interface? This code shows my point: interface I{ void foo(); } struct S : I { public void foo() { /* do something */ } } class Y { void doFoo(I i){ i.foo(); } void

Java: Why isn't autoboxing happening here?

廉价感情. 提交于 2019-12-10 04:35:46
问题 This gives me an error: int[] l = new int[] {0, 2, 192, -1, 3, 9, 2, 2}; int[] l2 = new int[] {9001, 7, 21, 4, -3, 11, 10, 10}; int[] l3 = new int[] {5, 5, 5, 64, 21, 12, 13, 200}; Set<List<Integer>> lists = new HashSet<List<Integer>>(); lists.add(Arrays.asList(l)); Eclipse: The method add(List<Integer>) in the type Set<List<Integer>> is not applicable for the arguments ( List<int[]> ) I thought int was supposed to be autoboxed to Integer ? 回答1: Although int is autoboxed to Integer, int[] is

Why I can't use Comparator to sort primitives?

ⅰ亾dé卋堺 提交于 2019-12-09 22:01:00
问题 As Java 5 have autoboxing, why I can't use Comparator to sort primitives? An int wouldn't be wrapped into a Integer ? 回答1: Arrays.sort(..) have dedicated overloadings for sorting primitive arrays. If you need any special sorting rules apart from the standard ones, I'm afraid you'd have to use autoboxing. In addition to that, you'd have to transform your array to Integer[] , because int[] is not autoboxed. And if you are not talking about arrays, but about collections - then you have no choice

Comparing doubles in Java gives odd results

断了今生、忘了曾经 提交于 2019-12-09 04:44:44
问题 I really can'get my head around why the following happens: Double d = 0.0; System.out.println(d == 0); // is true System.out.println(d.equals(0)); // is false ?! This however works as expected: Double d = 0.0; System.out.println(d == 0.0); // true System.out.println(d.equals(0.0)); // true I'm positive that this is related to autoboxing in some way, but I really don't know why 0 would be boxed differently when the == operator is used and when .equals is called . Doesn't this implicitly

Auto-(un)boxing fail for compound assignment

让人想犯罪 __ 提交于 2019-12-08 16:37:19
问题 Thanks to the implicit casting in compound assignments and increment/decrement operators, the following compiles: byte b = 0; ++b; b++; --b; b--; b += b -= b *= b /= b %= b; b <<= b >>= b >>>= b; b |= b &= b ^= b; And thanks to auto-boxing and auto-unboxing, the following also compiles: Integer ii = 0; ++ii; ii++; --ii; ii--; ii += ii -= ii *= ii /= ii %= ii; ii <<= ii >>= ii >>>= ii; ii |= ii &= ii ^= ii; And yet, the last line in the following snippet gives compile-time error: Byte bb = 0;

Is this really widening vs autoboxing?

☆樱花仙子☆ 提交于 2019-12-08 14:28:22
问题 I saw this in an answer to another question, in reference to shortcomings of the Java spec: There are more shortcomings and this is a subtle topic. Check this out: public class methodOverloading{ public static void hello(Integer x){ System.out.println("Integer"); } public static void hello(long x){ System.out.println("long"); } public static void main(String[] args){ int i = 5; hello(i); } } Here "long" would be printed (haven't checked it myself), because the compiler chooses widening over

Disabling auto-boxing for Java in IntelliJ IDEA

∥☆過路亽.° 提交于 2019-12-07 08:34:28
问题 Is there a way how to disable auto-boxing for Java 5 and 6 in IntelliJ IDEA, to not allow a developer to use this feature in the IDE? 回答1: I don't think you can disable auto-boxing outright while maintaining the target compile version - that's a feature of the specific Java version. What you can do in IntelliJ is change the inspection level of Auto-boxing to "Error". To do that: Go to Settings > Inspections, and type "boxing" into the search bar. Click on "Auto-boxing". Set the severity to

Java Integer auto auto-boxing [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-06 07:42:05
问题 This question already has answers here : Weird Integer boxing in Java (10 answers) Closed 6 years ago . I have stumbled upon this in Java (JDK 1.7): Integer a = 100; Integer b = 100; Integer c = 1000; Integer d = 1000; System.out.println(a == b); //true System.out.println(c == d); //false System.out.println(new Integer(100) == new Integer(100)); //false System.out.println(new Integer(1000) == new Integer(1000)); //false The output is: true false false false Why does a==b evaluate to true?

How to set -Euwc param with axis2-wsdl2code-maven-plugin?

那年仲夏 提交于 2019-12-06 03:57:24
问题 We are using axis2 to generate web-service clients, (I regret this now!). With axis2 command-line tool you can pass switch -Euwc to wrap int into Integer, boolean into Boolean and so on in generated soruces. This is the one way to tell axis2 that its OK for certain int or boolean values to be nillable in schema. My question is how do you set this parameter via POM or other means with Maven to achieve same behaviour with genrated sources? My stackoverflow and google searches aren't revealing