目的是在声明期间在bean中创建新的Strings? [英] Purpose of creating new Strings in a bean during declaration?

查看:62
本文介绍了目的是在声明期间在bean中创建新的Strings?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继承了现有的代码库,无法与原始开发人员联系.

Inherited an existing codebase, unable to contact original developer.

我了解对于普通的Java bean,您具有变量声明,获取器和设置器.

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
}

处理该bean的代码没有任何特殊情况,并且在除去初始化后,其工作原理完全相同.

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.

是否有创建这种方式的理由?会不会有我没有意识到的副作用?

这不会阻止将字段设置为null,因此在两种情况下都需要相同类型的值检查.

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

推荐答案

创建字符串对象的不同方法有哪些?

我们可以像任何普通的Java类一样,使用新的运算符来创建String对象,或者可以使用双引号(字面赋值)来创建String对象.

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.

String类中可用的构造函数太多,无法从char数组,byte数组,StringBuffer和StringBuilderetc等获取String.

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

当我们使用双引号创建String时,JVM会在String池中查找以查找是否存储了其他具有相同值的String.如果找到,它将仅返回对该String对象的引用,否则它将创建一个具有给定值的新String对象并将其存储在String池中.

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.

当我们使用new运算符时,JVM将创建String对象,但不要将其存储在String Pool中.我们可以使用intern()方法将String对象存储到String池中,或者如果池中已经存在具有相等值的String,则返回引用.

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.

所以当你这样做

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

在StringPool中检查了这些对象,并且由于s1已经存在于其中,因此s2将采用相同的引用,因此s1 == s2为真.

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

但是当您这样做时:

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

您使用的是 new运算符,因此JVM不会检查堆中是否已经有字符串,它只会为s4分配一个 new 空间, s3 == s4也是吗?当然没有.

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.

这篇关于目的是在声明期间在bean中创建新的Strings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆