autoboxing

Integer value comparison

核能气质少年 提交于 2019-12-02 22:04:37
I'm a newbie Java coder and I just read a variable of an integer class can be described three different ways in the API. I have the following code: if (count.compareTo(0)) { System.out.println(out_table); count++; } This is inside a loop and just outputs out_table . My goal is to figure out how to see if the value in integer count > 0 . I realize the count.compare(0) is the correct way? or is it count.equals(0) ? I know the count == 0 is incorrect. Is this right? Is there a value comparison operator where its just count=0 ? Integers are autounboxed, so you can just do if (count > 0) { .... }

Auto boxing and primitive types to match method signature

孤街浪徒 提交于 2019-12-02 19:23:59
问题 In version 1.5 , Java have introduced the concept of auto-boxing . public interface SomeInterface { public void test(Integer val); } public class Main implements SomeInterface { /*The method signature gets different and compiler is asking to override un-implemented methods*/ public void test(int t) { } } Then why I am getting compile time error for overriding un-implemented methods, why above test method's arguments are not auto-boxed to match parent test method signature? 回答1: It's because

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

僤鯓⒐⒋嵵緔 提交于 2019-12-02 17:51:17
This question already has an answer 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 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 not find anything usable. the question is that: You need to declare and initialize the two variables. The if condition must be

java: Auto-boxing and casting? [closed]

允我心安 提交于 2019-12-02 15:52:52
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I'm confused with a small problem , see the following : Double j = new Double(5); // No problem. double j =5;// //But //Here the problem: Double j = 5; Long k =5; Float g = 5.0; I know the solution but I want to understand why in some situations the cast is done implicitly and in others not. 回答1:

java: Auto-boxing and casting? [closed]

一个人想着一个人 提交于 2019-12-02 09:58:57
I'm confused with a small problem , see the following : Double j = new Double(5); // No problem. double j =5;// //But //Here the problem: Double j = 5; Long k =5; Float g = 5.0; I know the solution but I want to understand why in some situations the cast is done implicitly and in others not. There is nothing wrong with Double j = new Double(5); because Java will convert 5 from an int to the double that a Double constructor will take. It will also convert the 5 to a double for the line: double j =5; That is a widening primitive conversion. There is a problem with these lines. Double j = 5; Long

I get these weird characters when I try to print out a vector element!

坚强是说给别人听的谎言 提交于 2019-12-02 02:58:29
问题 I'm using Netbeans. When I run the program below, I get this as output [I@de6ced ! How come? import java.util.Arrays; import java.util.Vector; public class Test { public static void main (String[] args) { int[] a = new int[1]; a[0] = 5; Vector<Integer> a1 = new Vector(Arrays.asList(a)); System.out.println(a1.elementAt(0)); } } I also tried working around it but then I got a Exception in thread "main" java.lang.ClassCastException: [I cannot be cast to java.lang.Integer at TopCoder.Test.main

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

大憨熊 提交于 2019-12-02 02:04:18
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(Object obj) { return "Cannot cast " + obj.getClass().getName() + " to " + getName(); } Why is this

Oracle java tutorial - possible error regarding Character autoboxing java comment [duplicate]

依然范特西╮ 提交于 2019-12-02 00:57:53
This question already has an answer here: Does autoboxing call valueOf()? 4 answers I'm new to JAVA, currently learning Oracle tutorial generics section. I think there is a mistake there, and I want to make sure I'm not wrong. I'll appreciate your feedback. I saw the following explanation at https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html Pair < Integer, Character > p = new Pair<>(8, 'a'); Note that the Java compiler autoboxes 8 to Integer.valueOf(8) and 'a' to Character('a') : Pair < Integer, Character > p = new Pair<>(Integer.valueOf(8), new Character('a') ); I think

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

限于喜欢 提交于 2019-12-01 16:59:43
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 course works.. } @Test public void test3() { Long actual = new Long(1); assertThat(actual, is(1)); //

Should autoboxing be avoided in Java?

房东的猫 提交于 2019-12-01 15:15:35
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? 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 after creation) of the wrapper types may make their use impossible. There can be some unexpected behavior