primitive

Java Vector or ArrayList for Primitives

。_饼干妹妹 提交于 2019-12-17 18:26:22
问题 Is there an expandable array class in the Java API equivalent to the Vector or ArrayList class that can be used with primitives (int, char, double, etc)? I need a quick, expandable array for integers and it seems wasteful to have to wrap them in the Integer class in order to use them with Vector or ArrayList . My google-fu is failing me. 回答1: There is unfortunately no such class , at least in the Java API. There is the Primitive Collections for Java 3rd-party product. It's pretty dangerous to

Are primitive data types in c# atomic (thread safe)?

a 夏天 提交于 2019-12-17 16:29:35
问题 For example, do I need to lock a bool value when multithreading? 回答1: There is no such thing as an atomic type . Only operations can be atomic. Reading and writing a data type that fits into a single word ( int on a 32-bit processor, long on a 64-bit processor) is technically "atomic", but the jitter and/or processor can decide to reorder instructions and thus create unexpected race conditions, so you either need to serialize access with lock , use the Interlocked class for writes (and in

Why does autoboxing in Java allow me to have 3 possible values for a boolean?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 16:28:51
问题 Reference: http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html "If your program tries to autounbox null, it will throw a NullPointerException." javac will give you a compile-time error if you try to assign null to a boolean. makes sense. assigning null to a Boolean is a-ok though. also makes sense, i guess. but let's think about the fact that you'll get a NPE when trying to autounbox null. what this means is that you can't safely perform boolean operations on Booleans without

What does int.class mean

本小妞迷上赌 提交于 2019-12-17 16:27:38
问题 In java int, float and etc., are primitive types. Wrapper classes are used in case we need to use it with generics. But still the following declaration works in java, Class<Integer> intClass=int.class How can we call int.class even though it is a primitive type? 回答1: A primitive becoming an Object For primitives, there are Class objects available as constants named TYPE in the corresponding wrapper classes -- i.e. int.class is changed to java.lang.Integer.TYPE . For other types, the compiler

Why are there no byte or short literals in Java?

隐身守侯 提交于 2019-12-17 15:59:27
问题 I can create a literal long by appending an L to the value; why can't I create a literal short or byte in some similar way? Why do I need to use an int literal with a cast? And if the answer is "Because there was no short literal in C", then why are there no short literals in C? This doesn't actually affect my life in any meaningful way; it's easy enough to write (short) 0 instead of 0S or something. But the inconsistency makes me curious; it's one of those things that bother you when you're

javascript: do primitive strings have methods?

╄→гoц情女王★ 提交于 2019-12-17 06:14:21
问题 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(

Why do people still use primitive types in Java?

柔情痞子 提交于 2019-12-16 20:04:26
问题 Since Java 5, we've had boxing/unboxing of primitive types so that int is wrapped to be java.lang.Integer , and so and and so forth. I see a lot of new Java projects lately (that definitely require a JRE of at least version 5, if not 6) that are using int rather than java.lang.Integer , though it's much more convenient to use the latter, as it has a few helper methods for converting to long values et al. Why do some still use primitive types in Java? Is there any tangible benefit? 回答1: In

How to manually free primitive variables?

╄→尐↘猪︶ㄣ 提交于 2019-12-13 23:16:25
问题 I understand that in Java, we usually do not need to worry about deleting variables manually since garbage collection can take care of that. However, in some situations, we need to free an object before garbage collection takes effect in order to prevent memory leak. To free an object, we can simple set its reference to be null. Now my question is, how can we manually free a primitive variable? If there is no way to manually free a primitive variable, then why, since there is a mechanism to

Would it be stupid to recode `dim` to return `length` if `dim(x)==NULL`? [duplicate]

こ雲淡風輕ζ 提交于 2019-12-13 19:17:22
问题 This question already has an answer here : The number of data points in matrix and vector forms (1 answer) Closed 5 years ago . As pointed out in this question and copious official & unofficial R documentation, x <- complex(15) dim(x) == NULL For me it's annoying to have to write a separate method (or if clause) for atomic vectors rather than being able to use dim(x)[1] . Would it be stupid to recode dim (a primitive) so it automatically returns length if dim(x)==NULL ? To be a bit more

Generic methods for primitive types in java [duplicate]

天涯浪子 提交于 2019-12-13 18:26:25
问题 This question already has answers here : Is it possible to use a primitive type (int) in as a generic type in Java? (2 answers) Closed 5 years ago . Here is an example: import java.util.Collection; /** * Created by IDEA on 16/11/14. */ public class Size { public static int size(Iterable<?> data) { if (data instanceof Collection) { return ((Collection<?>) data).size(); } int counter = 0; for (Object i : data) { counter++; } return counter; } public static int size(int[] data) { return data