autoboxing

Why auto-boxing marked as a warning?

别来无恙 提交于 2019-12-05 14:20:00
问题 I understand that auto un-boxing should be done with care because the reference that is being un-boxed can be null. Why is auto-boxing marked as warning as well? Are there some pitfalls I am missing here? 回答1: If you don't expect performance issues (in terms of micro-optimization) you can safely disable this warning. It is just an indication in case you're not aware that auto-boxing happends here. In business-logic code where you have I/O overhead (due to DB transactions or disc access) auto

Why I can't use Comparator to sort primitives?

北城余情 提交于 2019-12-04 15:25:39
As Java 5 have autoboxing, why I can't use Comparator to sort primitives? An int wouldn't be wrapped into a Integer ? 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 - collections can hold only objects. Because you cannot parameterise a Comparator<T> -- or any other

Java Integer auto auto-boxing [duplicate]

♀尐吖头ヾ 提交于 2019-12-04 13:56:53
This question already has an answer here: Weird Integer boxing in Java 10 answers 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? What is the reason for this? Is this similar to String internalization? Is this similar to String internalization? Yes -

Wrappers of primitive types in arraylist vs arrays

强颜欢笑 提交于 2019-12-04 10:09:12
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 thing I can change is changing double variables to Double so as to prevent auto boxing and I don't know if

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

走远了吗. 提交于 2019-12-04 07:14:41
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 much. There's a Jira issue, which seems to be closed by developers without pointing in right direction.

How to use (primitive) autoboxing/widening with Hamcrest?

可紊 提交于 2019-12-04 03:04:11
问题 I came across https://code.google.com/p/hamcrest/issues/detail?id=130 to add some sugar syntax for Hamcrest matchers. But the idea was rejected by the Hamcrest developers. Any other smart ideas to make tests better readable by avoiding having to type L behind longs? @Test public void test1() { int actual = 1; assertThat(actual, is(1)); } @Test public void test2() { long actual = 1L; assertThat(actual, is(1)); // fails as expected is <1> but result was <1L> // assertThat(actual, is(1L)); off

Java 8 autoboxing + generics: different behaviour with variable vs. method

孤街醉人 提交于 2019-12-03 16:45:34
问题 I found a piece of code that after switching from Java 7 to Java 8 stopped compiling. It does not feature any of the new Java 8 stuff like lambda or streams. I narrowed the problematic code down to the following situation: GenericData<Double> g = new GenericData<>(1d); Double d = g == null ? 0 : g.getData(); // type error!!! You can probably guess that GenericData 's constructor has one parameter of that generic type and the getData() method returns just that generic type. (For the complete

Java 8 autoboxing + generics: different behaviour with variable vs. method

隐身守侯 提交于 2019-12-03 05:08:48
I found a piece of code that after switching from Java 7 to Java 8 stopped compiling. It does not feature any of the new Java 8 stuff like lambda or streams. I narrowed the problematic code down to the following situation: GenericData<Double> g = new GenericData<>(1d); Double d = g == null ? 0 : g.getData(); // type error!!! You can probably guess that GenericData 's constructor has one parameter of that generic type and the getData() method returns just that generic type. (For the complete source code see below.) Now what bothers me is that in Java 7 that code compiled just fine whereas with

How can “a <= b && b <= a && a != b” be true? [duplicate]

走远了吗. 提交于 2019-12-03 03:27:29
问题 This question already has answers here : How to make loop infinite with “x <= y && x >= y && x != y”? (4 answers) How can i define variables to make an infinity while loop with these conditions? [closed] (4 answers) Closed 6 years ago . Here is the code i have to figure it out how is it possible. I have a clue but i do not know how to do it. I think it is about negative and positive numbers and maybe the variable modifiers as well. I am a beginner i looked the solution everywhere but i could

Comparing doubles in Java gives odd results

淺唱寂寞╮ 提交于 2019-12-03 03:13:57
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 violate the equals contract ? * It is reflexive: for any non-null reference value * x, x.equals(x) should