Hi why it's possible to instantiate String and not possible for Numbers .I have made an example for that
public static void main(String[] args) throws InstantiationException,
IllegalAccessException {
String a = "s";
String newInstance = a.getClass().newInstance();
System.out.println(newInstance);
Double b = 0d;
Double newInstance2 = b.getClass().newInstance();
System.out.println(newInstance2);
}
Long is for integer numbers. Double is for real numbers (i.e. numbers which have decimal points in them!).
Telephone numbers need to be stored as a text/string data type because they often begin with a 0 and if they were stored as an integer then the leading zero would be discounted.
Primitive data types - includes byte , short , int , long , float , double , boolean and char.
Traditionally, the "int" type is the "natural" type for the processor - i.e., it's the size of the processor's registers. This can be 4, 8, 16, 32... bits, but usually the processor can handle it as fast as possible. Type "long" is for when you need more precision, even at the expense of slower code.
Calling newInstace invokes the default constructor. Double does not have one.
If you want to instantiate using reflection then you have to get one of the Contructors of the class using Class.#getConstructor by passing it the appropriate argument types and then call its method Constructor#newInstance by passing it the appropriate arguments.
java.lang.String
has an empty constructor (calling new String()
is the same as calling new String("")
).
Numbers, on the other hand, don't have no-arg constructors (what would the value of a new Double()
be anyway? there is no equivalent to an "empty number"), and thus can't be invoked this way, even not by reflection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With