primitive

Java Double vs double: class type vs primitive type

*爱你&永不变心* 提交于 2019-11-28 19:33:25
I was curious to what the performance differences between Java's class and primitive type for double were. So I created a little benchmark and found the class type to be 3x-7x slower than the primitive type. (3x on local machine OSX, 7x on ideone) Here is the test: class Main { public static void main(String args[]) { long bigDTime, littleDTime; { long start = System.nanoTime(); Double d = 0.0; for (Double i = 0.0; i < 1432143.341; i += 0.1) { d += i; } long end = System.nanoTime(); bigDTime = end - start; System.out.println(bigDTime); } { long start = System.nanoTime(); double d = 0.0; for

How to cast Object to boolean?

只谈情不闲聊 提交于 2019-11-28 17:21:43
How can I cast a Java object into a boolean primitive I tried like below but it doesn't work boolean di = new Boolean(someObject).booleanValue(); The constructor Boolean(Object) is undefined Please advise. If the object is actually a Boolean instance, then just cast it: boolean di = (Boolean) someObject; The explicit cast will do the conversion to Boolean , and then there's the auto-unboxing to the primitive value. Or you can do that explicitly: boolean di = ((Boolean) someObject).booleanValue(); If someObject doesn't refer to a Boolean value though, what do you want the code to do? Assuming

What is an int() Called?

…衆ロ難τιáo~ 提交于 2019-11-28 10:54:26
It's been rehashed over and over that primitive types don't have constructors. For example this _bar is not initialized to 0 when I call Foo() : class Foo{ int _bar; }; So obviously int() is not a constructor. But what is it's name? In this example I would say i is: (constructed? initialized? fooed?) for(int i{}; i < 13; ++i) Loki Astari mentions here that the technique has some sort of name. EDIT in response to Mike Seymour : #include <iostream> using namespace std; class Foo{ int _bar; public: void printBar(){ cout << _bar << endl; } }; int main() { Foo foo; foo.printBar(); Foo().printBar();

How comparison Object and primitive, with operator == works in Java? [duplicate]

。_饼干妹妹 提交于 2019-11-28 09:53:55
问题 This question already has an answer here: When using == for a primitive and a boxed value, is autoboxing done, or is unboxing done 3 answers For example: Long objectLong = 555l; long primitiveLong = 555l; System.out.println(objectLong == primitiveLong); // result is true. Is there invocation objectLong.longValue() method to compare Long to long or maybe some other way? 回答1: As ever, the Java Language Specification is the appropriate resource to consult From JLS 15.21.1 ("Numerical Equality

Is int an object in Java?

这一生的挚爱 提交于 2019-11-28 07:40:41
问题 More precisely, is int a part of the Integer class (a stripped down version or something) or is it something else entirely? I am aware that int is a value type and Integer a reference type, but does int inherit from Object anyway? (I am assuming that in this regard int, long, boolean etc are all similar. int was just chosen for convenience) 回答1: The basic types in Java are not objects and does not inherit from Object. Since Java 1.5 introduced allowed auto boxing between int and Integer(and

TypeScript: why is a number assignable to a reference of type Object?

瘦欲@ 提交于 2019-11-28 07:34:49
问题 Why is this legal TypeScript? var x: number = 5 var y: Object = x Surely a number is not an Object . One might suspect that x is implicitly coerced (auto-boxed) to an object, but no: if (!(y instanceof Object)) { console.log(typeof y) } prints number For the record: $ tsc --version Version 1.8.10 回答1: Type compatibility in TypeScript is based on structural subtyping, not nominal typing. That said, consider the two following interface definitions: interface IFoo { X: number } interface IBar {

Java Vector or ArrayList for Primitives

北战南征 提交于 2019-11-28 07:27: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. 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 use auto-boxing together with existing collection classes (in particular List implementations). For example

Hashcode of an int

那年仲夏 提交于 2019-11-28 07:27:18
What is the hashcode of a primitive type, such as int? for example, let's say num was an interger. int hasCode = 0; if (num != 0) { hasCode = hasCode + num.hashCode(); } Marko Topolnik For the hashCode of an int the most natural choice is to use the int itself. A better question is what to use for the hashCode of a long since it doesn't fit into the int -sized hashcode. Your best source for that—and all hashCode -related questions—would be Effective Java . Taken from the Integer.class source code: /** * Returns a hash code for this {@code Integer}. * * @return a hash code value for this object

Objective C Boolean Array

蓝咒 提交于 2019-11-28 06:44:28
I need to utilize an array of booleans in objective-c. I've got it mostly set up, but the compiler throws a warning at the following statement: [updated_users replaceObjectAtIndex:index withObject:YES]; This is, I'm sure, because YES is simply not an object; it's a primitive. Regardless, I need to do this, and would greatly appreciate advice on how to accomplish it. Thanks. Nick Partridge Yep, that's exactly what it is: the NS* containers can only store objective-C objects, not primitive types. You should be able to accomplish what you want by wrapping it up in an NSNumber: [updated_users

Check type of primitive field

时光总嘲笑我的痴心妄想 提交于 2019-11-28 06:12:06
I'm trying to determine the type of a field on an object. I don't know the type of the object when it is passed to me but I need to find fields which are long s. It is easy enough to distinguish the boxed Long s but the primitive long seems more difficult. I can make sure that the objects passed to me only have Longs , not the primitives, but I'd rather not. So what I have is: for (Field f : o.getClass().getDeclaredFields()) { Class<?> clazz = f.getType(); if (clazz.equals(Long.class)) { // found one -- I don't get here for primitive longs } } A hacky way, which seems to work, is this: for