Łukasz Krzyżak Franczyk Łukasz Krzyżak Franczyk - 17 days ago 5
Java Question

Why do classes need to be instantiated using their constructor but Strings, which are a class, don't?

I have a question.

I understand that I don't have to initialize primitives in an array of primitives, but i have to initialize objects in an array of objects, for example:

int[] int_array = new int[2];
int_array[0]=0;
int_array[1]=1;

Foo[] foo_array = new Foo[2];
foo_Array[0]=new Foo(); //need to initialize
foo_Array[1]=new Foo(); //need to initialize
foo_Array[0].name ="foo0";
foo_Array[1].name ="foo1";


but the Java Compiler doesn't need from me to initialize a String objects:

String[] string_array = new String[2];
string_array[0]=new String(); //don't need to initialize, but when I initialize it does nothing
string_array[1]=new String(); //don't need to initialize, but when I initialize it does nothing
string_Array[0] ="string0";
string_Array[1] ="string1";


Can anyone explain what is going on?

Answer

If I understand the question correctly, you are asking: "why do classes need to be instantiated using their constructor but Strings, which are a class, don't?"

You are absolutely correct: you can initialise primitives by assigning them directly to a value. Eg:

int iMyInt = 5

On the other hand, classes cannot be initialised in this way. You must go by their constructor to do so, or make them null. Eg:

Foo myFoo = new Foo();

The String class being a, well, class and not a primitive, should logically need to be initialised the same way as all the other classes, right?

Wrong. The reason is, the string class is, in fact, a special class. Namely, Java allows you to initialise it the same way as primitives, that is to say, by assigning it to a string literal, which is anything inside double quotes. Eg:

String sMyString = "myString-literal01";

That's it really. Keep in mind however, that both ways of initialising the string are not exactly identical: every time you initialise a string by assigning it to a string literal, the JVM checks what's called the String pool - a special place inside the Java memory, to see if it can find that string literal in there. If it does, it returns that and doesn't create a new object. On the other hand, every time you initialise a string by calling the String constructor, it will create a new object. Here's a quick example to illustrate that:

String sMyString1 = "ChrisN",
    sMyString2 = "ChrisN",
    sMyString3 = new String("ChrisN");

Boolean bAreString1And2TheSameObject = (sMyString1 == sMyString2),
    bAreString1And3TheSameObject = (sMyString1 == sMyString3);

System.out.println("String 1 and 2 are the same object : " + String.valueOf(bAreString1And2TheSameObject)); // Output: true 

System.out.println("String 1 and 3 are the same object : " + String.valueOf(bAreString1And3TheSameObject)); //Output: false

I happened to read this off this website yesterday (I highly recommend you read it as well) : https://javarevisited.blogspot.de/2013/07/java-string-tutorial-and-examples-beginners-programming.html

Comments