简体   繁体   中英

Java object initialization - When is it necessary?

I'm curious about java initialization:
When is it necessary to initialize objects like this:

String init = new String("");

and which objects - like Strings - don't have to be initialized like above?

String init = "";

From your comment on your question:

Then my question is which objects need the "new Object()" initialization?

You're asking about construction , not initialization.

All objects need to be constructed by someone. The strings created by string literals are constructed by the JVM. You basically never want to use new String("text content here") .

There are several other objects constructed by the JVM, such as the Class object for each class you load. But other than String , for objects you want to interact with, you usually either have to explicitly construct them (via new ) or receive them from a method that constructs them. String is a bit special because it's the only object type in Java (I think ) with a literal notation. (All of the other literals, like 42 or false , are primitives.)

Basically there's a subtle difference between the 2 ways of initialization you mentioned: When you use:

String str = new String("");

a new String object is created on the heap and str points to it.

But in the latter case:

String str = "";

If there is already a string with this value ("") on the string pool , then the reference is initialized to point to it and no new object is created. If it is not found in the string pool, then a new string is created in the string pool and str is initialized to point to it.

For strings, you almost always want to use the second form. It does less work for what -- in most circumstances -- is the same result.

The same advice goes for numeric classes, such as Integer , Double , etc.

In all other cases you don't normally have the choice, and have to use the first form.

In Java, all objects must be initialized. The second case init = "" is also an initialization, except that the compiler lets you avoid the explicit call: object creation is still there. Starting with Java 5, the compiler also "knows" about wrappers for Java primitives, letting you use primitive constants in expressions that require a wrapper class (this is called autoboxing ).

All local variables must be initialized explicitly, while member fields can be initialized implicitly to null or the default value of the primitive.

All Objects have to be initialized with new but some, like these for example:

 String myString= "";
 Integer myInteger= 2;
 Float myFloat= 2f;
 Double myDouble= 2d;
 Byte myByte = 2;

and every kind of array, which can be initialized like `T[] array = {//list some Ts};

String s1 = "1";
String s2 = new String("2");
int i1 = 1;
int i2 = new Integer(2);
long l1 = 1l;
long l2 = new Long(2l);
float f1 = 1f;
float f2 = new Float(2f);
double d1 = 1d;
double d2 = new Double(2d);
char c1 = '1';
char c2 = new Character('2');
byte b1 = 1;
byte b2 = new Byte((byte) 2);
int[] a1 = {1};
int[] a2 = new int[] {2};

Most classes require explicit construction (the new), however a few classes does not, these are the autoboxing classes (String, Integer, Double, and a few more, as well as arrays of these (using the comma seperated list initiliazation)) and these are the only ones!

What happens at the compiler level is really just that the compiler translates the implicit construction to an explicit one (eg One using new)

All objects need to be initialized before they can be used. You can try declaring:

int myPrimitiveInt;
Integer myObjectInt;

But myPrimitiveInt cannot be used until you give it a value, and myObjectInt is implicitly initialized as null. For either of these to be used (except to get a null from the object), they need to be given a value, whether you use a constructor or not.

myPrimitiveInt = 5;
myObjectInt = new Integer(5);
myObjectInt = 5;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM