primitive

Using primitives or wrapper class in Hibernate?

随声附和 提交于 2019-12-10 14:58:13
问题 Mapping database with Hibernate. We should use Double with @NotNull constraint Or use the double primitive type instead. What is the best practice? (Using Java 6) @Column(name = "price_after_tax", nullable=false) @NotNull public Double getPriceAfterTax() { return priceAfterTax; } OR @Column(name = "price_after_tax") public double getPriceAfterTax() { return priceAfterTax; } Many thanks! 回答1: I think you should use Double as it can hold even null value. So, in future if by any chance you

Java Mappings and Primitives

随声附和 提交于 2019-12-10 04:10:07
问题 I want to create a mapping that takes a String as the key and a primitive as the value. I was looking at the Java docs and did not see that Primitive was a class type, or that they shared some kind of wrapping class. How can I constrain the value to be a primitive? Map<String, Primitive> map = new HashMap<String, Primitive>(); 回答1: Java Autoboxing allows to create maps on Long, Integer, Double and then operate them using primitive values. For example: java.util.HashMap<String, Integer> map =

Fastest most efficient way to determine decimal value is integer in Java

一个人想着一个人 提交于 2019-12-10 03:32:42
问题 Given a double variable named sizeValue and sizeValue contains something other than 0, what is the most efficient way to determine that sizeValue contains a value that is an integer? Currently i'm using sizeValue % 1 == 0 any other faster ways? 回答1: give a try to Math.ceil: private static boolean isInt(double x) { return x == Math.ceil(x); } EDIT I've done some benchmarks with the following methods: private static boolean isInt1(double x) { return x == (int) x; } private static boolean isInt2

Java multidimensional array considered a primitive or an object

风格不统一 提交于 2019-12-09 19:24:44
问题 Is int[][] matrix = new int[10][10]; a primitive or is it considered an object? When i send it as a parameter to a function, does it send its reference (like an object) or its value (like a primitive)? 回答1: Every Java array is an Object. When you pass it as an argument, you pass a copy of the reference to the array. 回答2: Arrays are objects. Arrays of arrays are also objects. Java doesn't really have multidimensional arrays as such, just support for arrays of arrays. int [][] foo = {{1}, {2,2}

Move semantics and primitive types

左心房为你撑大大i 提交于 2019-12-09 15:34:48
问题 Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::cout << "Printing v1" << std::endl; print(v1); std::vector<int> v2(std::make_move_iterator(v1.begin()), std::make_move_iterator(v1.end())); std::cout << "Printing v1" << std::endl; print(v1); std::cout << "Printing v2" << std::endl; print(v2); std::vector<std::string> v3{"some", "stuff", "to", "put", "in", "the", "strings"}; std::cout << "Printing v3" << std::endl; print(v3); std::vector<std::string> v4(std:

Why can't this be a primitive?

天涯浪子 提交于 2019-12-08 19:51:17
问题 I was messing around with JavaScript, and noticed that this can never be a primitive. What am I talking about? Let me explain. Take this function for example. function test(){ return typeof this; } test.call('Abc'); // 'object' test.call(123); // 'object' They are both 'object' , not 'string' or 'number' , like I'd expect. After a bit of confusion (and messing with instanceof ), I figured out what's going on. 'Abc' is being coverted to a String object, and 123 is being converted to a Number

int vs float arithmetic efficiency in Java

大城市里の小女人 提交于 2019-12-08 16:41:29
问题 I'm writing an application that uses Dijkstra algorithm to find minimal paths in the graph. The weights of the nodes and edges in the graph are float numbers, so the algorithm doing many arithmetics on float numbers. Could I gain a running time improve if I convert all weight to int s? Is int arithmetic operations are faster in Java then float ones? I tried to write a simple benchmark to check that out, but I'm not satisfied with the results I got. Possibly the compiler has optimized some

Where are Rust's boolean and other primitive types implemented?

流过昼夜 提交于 2019-12-08 15:17:11
问题 I was going through the code behind some of the basic types in Rust, e.g. the pleasantly simple implementation of Option<T> or the weird macro magic behind tuple and I was able to find all of the types that I wanted in libcore. All except for one - bool . I couldn't find it anywhere else either. Where is the code behind bool in Rust? I know this is not the most novel type out there, but I was surprised I could not find it. Thanks to the answers by Francis and rodrigo, I noticed that the code

IsPrimitive doesn't include nullable primitive values

久未见 提交于 2019-12-08 15:07:43
问题 I want to check if a Type is primitive or not and used the following code: return type.IsValueType && type.IsPrimitive; This works fine aslong as the primitive isnt nullable. For example int?, how can I check if the type is a nullable primitive type? (FYI: type.IsPrimitive == false on int?) 回答1: From MSDN: The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single. So basically you should expect Nullable<Int32> to not

Is this really widening vs autoboxing?

☆樱花仙子☆ 提交于 2019-12-08 14:28:22
问题 I saw this in an answer to another question, in reference to shortcomings of the Java spec: There are more shortcomings and this is a subtle topic. Check this out: public class methodOverloading{ public static void hello(Integer x){ System.out.println("Integer"); } public static void hello(long x){ System.out.println("long"); } public static void main(String[] args){ int i = 5; hello(i); } } Here "long" would be printed (haven't checked it myself), because the compiler chooses widening over