Setting Short Value Java

In Java, integer literals are of type int by default. For some other types, you may suffix the literal with a case-insensitive letter like L, D, F to specify a long, double, or float, respectively. Note it is common practice to use uppercase letters for better readability. The Java Language Specification does not provide the … Read more

Is it valid to compare a double with an int in java?

Yes, it’s valid – it will promote the int to a double before performing the comparison. See JLS section 15.20.1 (Numerical Comparison Operators) which links to JLS section 5.6.2 (Binary Numeric Promotion). From the latter: Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules: If either … Read more

How to determine the primitive type of a primitive variable?

Try the following: int i = 20; float f = 20.2f; System.out.println(((Object)i).getClass().getName()); System.out.println(((Object)f).getClass().getName()); It will print: java.lang.Integer java.lang.Float As for instanceof, you could use its dynamic counterpart Class#isInstance: Integer.class.isInstance(20); // true Integer.class.isInstance(20f); // false Integer.class.isInstance(“s”); // false

How To Test if Type is Primitive

You can use the property Type.IsPrimitive, but be carefull because there are some types that we can think that are primitives, but they aren´t, for example Decimal and String. Edit 1: Added sample code Here is a sample code: if (t.IsPrimitive || t == typeof(Decimal) || t == typeof(String) || … ) { // Is … Read more