primitive

Java generic arithmetic

丶灬走出姿态 提交于 2021-01-27 06:32:18
问题 I'm trying to create some Java classes, that should work with either float or double numbers (for simulation purposes I am required to support both). The classes need to do some basic arithmetic and also require use of trigonometric functions (sin, cos, atan2). I tried to do a generic approach. As Java does not allow primitive types in generics and MyClass<T extends Number> does indeed allow Double and Float, but makes basic arithmetic impossible, I build a wrapper class around Double and

Java generic arithmetic

橙三吉。 提交于 2021-01-27 06:32:13
问题 I'm trying to create some Java classes, that should work with either float or double numbers (for simulation purposes I am required to support both). The classes need to do some basic arithmetic and also require use of trigonometric functions (sin, cos, atan2). I tried to do a generic approach. As Java does not allow primitive types in generics and MyClass<T extends Number> does indeed allow Double and Float, but makes basic arithmetic impossible, I build a wrapper class around Double and

How is the “==” Equality Operator Used Between Primitive Types and Reference Types? [duplicate]

流过昼夜 提交于 2020-08-10 19:27:06
问题 This question already has answers here : Does == actually work the same or different when comparing two primitives vs two Objects in Java? (2 answers) Closed 5 months ago . In java the "==" operator compares values for primitive types and compares the reference to the objects location in memory for reference types. For example: Primitive Types int x = 5; int y = 5; System.out.println(x == y); //true Reference Types String stringOne = new String("Hello"); String stringTwo = new String("Hello")

Async function returning promise, instead of value

ぃ、小莉子 提交于 2020-07-15 05:10:26
问题 I'm trying to understand how async/await works in conjunction together with promises. Code async function latestTime() { const bl = await web3.eth.getBlock('latest'); console.log(bl.timestamp); // Returns a primitive console.log(typeof bl.timestamp.then == 'function'); //Returns false - not a promise return bl.timestamp; } const time = latestTime(); // Promise { <pending> } Issue As far as I understand, await should be blocking and in the code above it seemingly blocks returning an object bl

What are the Java primitive data type modifiers?

霸气de小男生 提交于 2020-06-25 10:47:28
问题 Alright, I've been programming in Java for the better part of three years, now, and consider myself very experienced. However, while looking over the Java SE source code, I ran into something I didn't expect: in class Double : public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308 public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324 I did not expect this and can't find out what it means. If you don't know, I'm referring to the p and P that are

How to collect the results of a Stream in a primitive array?

白昼怎懂夜的黑 提交于 2020-05-11 04:47:41
问题 I'm trying to convert 2D list to a 2D int array. However, it seems I can only collect objects, not primitives. When I do: data.stream().map(l -> l.stream().toArray(int[]::new)).toArray(int[][]::new); I get the compile-time error Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>) . However, if I change int[] to Integer[] , it compiles. How can I get it to just use int ? 回答1: Use mapToInt method to produce a stream of primitive integers: int[][] res = data.stream().map(l

What's difference between primitive and wrapper class in JPA (Hibernate) column mappings?

落花浮王杯 提交于 2020-03-18 12:07:56
问题 For instance, there’s an integer column in a database table. Then in java model, it can be mapped both as primitive int and Integer . My question is what's difference between the int and Integer in this case? And performance concern? Thanks! 回答1: I tend to avoid using primitives. This is especially true for the Id attribute. This makes it possible to detect a not yet set value by testing for null . If using Java 5 or above, auto-boxing takes away the pain (and is not a performance concern).

information loss from long to float in Java [duplicate]

╄→尐↘猪︶ㄣ 提交于 2020-01-30 03:27:29
问题 This question already has answers here : Why does Java implicitly (without cast) convert a `long` to a `float`? (4 answers) Closed 4 years ago . if you call the following method of Java void processIt(long a) { float b = a; /*do I have loss here*/ } do I have information loss when I assign the long variable to the float variable? The Java language Specification says that the float type is a supertype of long . 回答1: Yes, this is possible: if only for the reason that float has too few

Using Proxy with wrapper objects around primitives [duplicate]

余生颓废 提交于 2020-01-16 08:47:11
问题 This question already has answers here : How to determine that a JavaScript function is native (without testing '[native code]') (2 answers) Closed 2 years ago . I want to use javascript's Proxy objects to augment a data structure with a bit of additional logging. While doing so, I noticed a problem: simpleProxy = new Proxy(new Number(4), {}) simpleProxy.valueOf(); // TypeError: valueOf method called on incompatible Proxy The error makes sense to me. valueOf is invoked with the this object