The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

bitterlemon (44) [Avatar] Offline
#1
Chapter 4.1.1. tells me about 3 ways to create Strings.

1)
//creating a String with new operator
String s1 = new String("Hallo");

2)
// creating a String using the assignment operator =
String s2 = "Hallo";

3)
// creating String by enclosing a value with double quotes (")
System.out.println("Hallo");

1) always creates a new String-object.
2) and 3) are creating only new String-objects if they are not already in the String constant pool.

I'm wondering:
- 2) and 3) are working the same way with the String constant pool.
- Not only 3) works with double quotes. 2) need double quotes as well.

Actually for me it look like only the double quotes (") are the second way of creating a String beside the new operator. For me it seems like there is no third way to create String objects.

Because a crazy code like this...
int i = "Hi there".lenth();
...compiles perfectly, I think the quotes are all you need for creating a String object. That in mind, 2) only assigns the already existing String to a reference variable, nothing more.

I think the difference between 2) and 3) is only what you wanna do with the String object in front of you. Do you wanna assign it to a refernce variable using "=", or do you wanna hand over the String object to a method.like 3) does.

I don't see any difference between the creation of String-objects in option 2) and 3).

Or am I missing something?
cassisian (28) [Avatar] Offline
#2
Re: Chapter 4.1.1 Question: Are there two or three ways to create Strings?
"That in mind, 2) only assigns the already existing String to a reference varibale, nothing more."

You are right.

"I don't see any difference between the creation of String-objects in option 2) and 3)."

Me, neither.
bitterlemon (44) [Avatar] Offline
#3
Re: Chapter 4.1.1 Question: Are there two or three ways to create Strings?
Hmmm, that would make this whole thing with String creation much easier to remember. This...

Car car1 = new Car("BMW");
Car car2 = new Car("BMW");
Car car3 = new Car("Audi");

...creates three cars objects. There is no problem because the constructors for car1 and car2 get called with exactly the same value. Everyone who is in Chapter 4 knows that already. So why should it be different with String-Objects, when they are created "the normal way" with the new-operator?

String s1 = new String("house");
String s2 = new String("house");
String s3 = new String("garage");

Again, 3 different objects. No Problem because s1 and s2 got created with the same value. So nothing to wonder until now.

Now the only difference:

Normally you can create objects only using the new-operator. You can create String objects the same way.
But there is a second way with String objects. You can create a String object using double quotes ("). This modification comes inseperable with the String constant pool.

The following code proofs, that String object created using double quotes are added to the String constant pool only if they are not already in it.

This code prints true on the console:

stringConstantPoolIsWorking("OCA");

public static void stringConstantPoolIsWorking(String s1){
String s2 = "OCA";
System.out.println(s1 == s2);
}