primitive

Java Arrays.asList on primitive array type produces unexpected List type [duplicate]

落爺英雄遲暮 提交于 2019-11-26 23:14:00
Possible Duplicate: Arrays.asList() not working as it should? Apparently the return type of Arrays.asList(new int[] { 1, 2, 3 }); is List<int[]> . This seems totally broken to me. Does this have something to do with Java not autoboxing arrays of primitive types? The problem is that Arrays.asList takes a parameter of T... array . The only applicable T when you pass the int[] is int[] , as arrays of primitives will not be autoboxed to arrays of the corresponding object type (in this case Integer[] ). So you can do Arrays.asList(new Integer[] {1, 2, 3}); . Try: Arrays.asList(new Integer[] { 1, 2,

javascript: do primitive strings have methods?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 22:54:12
MDN states: primitive, primitive value A data that is not an object and does not have any methods. JavaScript has 5 primitive datatypes: string, number, boolean, null, undefined. With the exception of null and undefined, all primitives values have object equivalents which wrap around the primitive values, e.g. a String object wraps around a string primitive. All primitives are immutable. So when we call a "s".replace or "s".anything is it equivalent to new String("s").replace and new String("s").anything ? No, string primitives do not have methods. As with numeric primitives, the JavaScript

Java: Why am I required to initialize a primitive local variable?

烂漫一生 提交于 2019-11-26 22:32:21
public class Foo { public static void main(String[] args) { float f; System.out.println(f); } } The print statement causes the following compile-time error, The local variable f may not have been initialized If primitives in Java already have a default value (float = 0.0f) , why am I required to define one? Edit: So, this works public class Foo { float f; public static void main(String[] args) { System.out.println(new Foo().f); } } Thanks, everyone! Because it's a local variable. This is why nothing it's assigned to it : Local variables are slightly different; the compiler never assigns a

Java: how much time does an empty loop use?

…衆ロ難τιáo~ 提交于 2019-11-26 22:14:58
问题 I am trying to test the speed of autoboxing and unboxing in Java, but when I try to compare it against an empty loop on a primitive, I noticed one curious thing. This snippet: for (int j = 0; j < 10; j++) { long t = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) ; t = System.currentTimeMillis() - t; System.out.print(t + " "); } Every time I run this, it returns the same result: 6 7 0 0 0 0 0 0 0 0 Why does the first two loops always take some time, then the rest just seem to

Cast primitive type array into object array in java

吃可爱长大的小学妹 提交于 2019-11-26 20:07:47
问题 Why I cannot do this in java? Object[] o = (Object[])(new int[]{0,1,2,3.14,4}); I have a method that receives an object and then represents it as a string, but depending on his type (primitive, primitive wrapper, array, etc...). When I was creating a Unit test, I was passing an array as Object which is Ok, but when I perform cast of that object into Object[] I'm getting ClassCastException . This is only happening with primitive type arrays. Is there any way to avoid this behavior? If not,

Dynamically find the class that represents a primitive Java type

佐手、 提交于 2019-11-26 19:59:37
I need to make some reflective method calls in Java. Those calls will include methods that have arguments that are primitive types (int, double, etc.). The way to specify such types when looking up the method reflectively is int.class, double.class, etc. The challenge is that I am accepting input from an outside source that will specify the types dynamically. Therefore, I need to come up with these Class references dynamically as well. Imagine a delimited file a list of method names with lists of parameter types: doSomething int double doSomethingElse java.lang.String boolean If the input was

Are Java primitives immutable?

旧巷老猫 提交于 2019-11-26 19:49:08
问题 If a method has a local variable i : int i = 10; and then I assign a new value: i = 11; Will this allocate a new memory location? Or just replace the original value? Does this mean that primitives are immutable? 回答1: Will this allocate a new memory location? Or just replace the original value? Java does not really make any guarantees that variables will correspond to memory locations; for example, your method might be optimized in such a way that i is stored in a register — or might not even

How do I draw lines using XNA?

做~自己de王妃 提交于 2019-11-26 19:48:23
问题 I've read a bunch of tutorials involving XNA (and it's various versions) and I still am a little confused on drawing primitives. Everything seems to be really convoluted. Can someone show me, using code, the simplest XNA implementation of drawing one or two lines on to the screen? Perhaps with a brief explanation (including the boilerplate)? I'm not a games programmer and I have little XNA experience. My ultimate goal is to draw some lines onto the screen which I will eventually transform

How can you pass multiple primitive parameters to AsyncTask?

我是研究僧i 提交于 2019-11-26 19:23:52
There are related questions, such as How can I pass in 2 parameters to a AsyncTask class? , but I ran into the difficulty of trying in vain to pass multiple primitives as parameters to an AsyncTask, so I want to share what I discovered. This subtlety is not captured in the existing questions and answers, so I want to help out anyone who runs into the same problem as I did and save them the pain. The question is this: I have multiple primitive parameters (e.g. two longs) that I want to pass to an AsyncTask to be executed in the background--how can it be done? (My answer...after struggling with

Java equivalent of unsigned long long?

我是研究僧i 提交于 2019-11-26 18:25:18
In C++, I enjoyed having access to a 64 bit unsigned integer, via unsigned long long int , or via uint64_t . Now, in Java longs are 64 bits, I know. However, they are signed. Is there an unsigned long (long) available as a Java primitive? How do I use it? Sean Bright I don't believe so. Once you want to go bigger than a signed long, I think BigInteger is the only (out of the box) way to go. Starting Java 8, there is support for unsigned long (unsigned 64 bits). The way you can use it is: Long l1 = Long.parseUnsignedLong("17916881237904312345"); To print it, you can not simply print l1, but you