Purpose of creating new Strings in a bean during declaration?

一世执手 提交于 2020-01-06 03:05:26

问题


Inherited an existing codebase, unable to contact original developer.

I understand that for a normal Java bean, you have variable declarations, getters and setters.

public class FooBean{
    private String foo;
    private String bar;

    // getters and setters
}

However in this code, I notice that all the strings are initialised.

public class FooBean{
    private String foo = new String();
    private String bar = new String();

    // getters and setters
}

The code that handles this bean doesn't have any particular special case and it seems to work exactly the same with the initialisations removed.

Is there a reason to create this way? Does it have some kind of side effect I'm not realising?

It doesn't prevent the fields from being set to null, so the same kind of value checking is required in either case.


回答1:


What are different ways to create String Object?

We can create String object using new operator like any normal java class or we can use double quotes (literal assignment) to create a String object.

There are too several constructors available in String class to get String from char array, byte array, StringBuffer and StringBuilderetc etc.

To your Question:

When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.

When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.

So when you do

String s1 = "abc";
String s2 = "abc";

those are checked in the StringPool and since s1 already exist there, s2 will take the same reference, hence, s1 ==s2 is true.

but when you do:

String s3 = new String("abc");
String s4 = new String("abc"); 

you are using the new operator, therefore the JVM is not checking if there is an string already in the heap, it will just allocate a new space for s4, so is s3==s4 ??? of course no.

Please take a look at the image below for a more illustrative example.




回答2:


It just gives an initial non null value.

But it would be better to write

private String foo = "";
private String bar = "";

since there is no need to create new String objects, better reuse the empty string instance from the string pool.




回答3:


Considering that there's no reason to use new String(); instead of "" (in this case, there can be some edge cases where you want distinct empty Strings), this is probably just a bad habit he's learned somewhere.




回答4:


No, this doesn't really make sense. It's usually bad practice to use the String constructor over a simple literal "". There is a small but important difference between foo = new String() and foo = "" though:

String foo1 = new String();
String bar1 = new String();
System.out.println(foo1 == bar1);

String foo2 = "";
String bar2 = "";
System.out.println(foo2 == bar2);

The output of this will be

false
true

foo2 and bar2 will point to the same literal and will therefore be 'equal' (in the sense of ==). See here for details.

You should never rely on this, though. Always use equals on strings



来源:https://stackoverflow.com/questions/36176968/purpose-of-creating-new-strings-in-a-bean-during-declaration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!