How to convert from String to a primitive type or standard java Wrapper types
Date : March 29 2020, 07:55 AM
will be helpful for those in need As far as I'm aware, there is no real alternative to the version you presented. You can simplify it a bit (since the wrapper types are all final), but you essentially need to use if or switch or hashing to switch on the class. My advice is to code it like the above. Ugly code is only a problem per se if you have to look at it. So put it inside a utility method and don't look at it again. public static Object toObject( Class clazz, String value ) {
if( Boolean.class == clazz ) return Boolean.parseBoolean( value );
if( Byte.class == clazz ) return Byte.parseByte( value );
if( Short.class == clazz ) return Short.parseShort( value );
if( Integer.class == clazz ) return Integer.parseInt( value );
if( Long.class == clazz ) return Long.parseLong( value );
if( Float.class == clazz ) return Float.parseFloat( value );
if( Double.class == clazz ) return Double.parseDouble( value );
return value;
}
if (Boolean.class == clazz || Boolean.TYPE == clazz) {
return Boolean.parseBoolean(value);
}
|
Are Primitive Wrapper Classes in Java Treated as Reference Types?
Date : March 29 2020, 07:55 AM
help you fix your problem Primitive class wrappers are object references, not primitive types. In your example, you're assigning a new value to your variable, not updating the state. That's why foo keeps its old value (old because it was never changed): Integer foo = new Integer(1);
Integer bar = foo; //bar and foo "points" to the same location
bar = new Integer(2); //now bar only "points" to a new location, foo is unaffected
System.out.println(foo);
Integer a = 128;
Integer b = 128;
System.out.println(a == b); //false
System.out.println(a.equals(b)); //true
|
In Java, is it possible to override methods if return types are respectively a primitive and its wrapper class?
Tag : java , By : Ricardo
Date : November 18 2020, 03:49 PM
wish help you to fix your issue You can only return same type or a sub-class of the parent's return type and its known as Co-Variant return types. The compiler lets you auto-box and unbox between primitives and wrappers but this doesn't make one a sub-class of the other. Primitives are not classes and cannot be used in the way you have.
|
In Java, do operators perform identically on primitive types and primitive wrapper classes?
Tag : java , By : unadopted
Date : March 29 2020, 07:55 AM
it fixes the issue Equality operators (== and !=) are not reliable when working with wrapper classes. First, generally they compare object references, not object values. For example: Integer a = new Integer(24);
Integer b = new Integer(24);
System.out.println(a == b); // Prints false
System.out.println(a != b); // Prints true
Integer a = 24;
Integer b = 24;
System.out.println(a == b); // Prints true
System.out.println(a != b); // Prints false
|
Does the Java JLS specify promotion of primitive wrapper types?
Tag : java , By : user186012
Date : March 29 2020, 07:55 AM
|