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

java 字符串创建对象

Here, we are discussing two ways to create the string object in Java,

在这里,我们讨论了两种在Java中创建字符串对象的方法

  1. By using String Literal

    通过使用字符串文字

  2. By using new Keyword

    通过使用新的关键字

Now, we will discuss each way given above in detail with the help of an example.

现在,我们将借助示例详细讨论上面给出的每种方法。

1)使用字符串文字创建字符串对象 (1) String object creation using string literal)

  • String Literal is a proper sequence of characters.

    字符串文字是正确的字符序列。

  • String Literal is enclosed in double-quotes from the source set of characters.

    String Literal用字符源集的双引号引起来。

  • We should go for String Literal when we want to represent a proper sequence of characters taken together.

    当我们要表示一个适当的字符序列时,应该使用String Literal。

  • Every String Literal must be terminated with "\n" (null) character.

    每个String Literal必须以“ \ n” (空)字符结尾。

  • A string Literal is created in java by using double quotes(" ").

    通过使用双引号( “” )在Java中创建字符串Literal。

Example:

例:

    String str = "Java Programming";

Note:

注意:

  • Whenever we create a string literal, the Java Virtual Machine (JVM) first checks in the "string constant pool", so there are two cases:

    每当我们创建字符串文字时,Java虚拟机(JVM)都会首先检查“字符串常量池” ,因此有两种情况:

    1. If, String Literal already exists in the pool then, in that case, a reference to the pooled instance is returned (i.e. no new instance is created for the existing String Literal).
    2. Else, String Literal is not already existed in the pool then, in that case, a new string instance is created and placed in the pool (i.e. new instance is created for the new String Literal).
  • String objects are stored in a special memory area known as "String Constant Pool".

    字符串对象存储在称为“字符串常量池”的特殊存储区中。

Example:

例:

// Java program to demonstrate the example of
// creating string object by using String Literal.

public class CreateStringObjectByStringLiteral {
    public static void main(String[] args) {
        // Creating a string literal and placed in 
        // memory "string constant pool"

        String str1 = "Java Support OOPS Concepts";

        System.out.println(str1);

        // Again,we create the same literal ("Java Support OOPS 
        // Concepts") then in that case a new instance is not created 
        // because JVM check the "string constant pool" first before 
        // creating an object if exists then return the same object
        String str2 = "Java Support OOPS Concepts";
        System.out.println("Return the same already placed in SCP :" + " " + str2);
    }
}

Output

输出量

Java Support OOPS Concepts
Return the same already placed in SCP : Java Support OOPS Concepts

2)使用“ new”关键字创建一个String对象 (2) Creating a String object by using "new" keyword)

Here, we will see the second category by using "new" keyword and how to create a string object with the help of the "new" keyword.

在这里,我们将通过使用“ new”关键字以及如何在“ new”关键字的帮助下创建字符串对象来看到第二类。

It is similar kind of other object creation by using "new" keyword.

这与使用“ new”关键字创建其他对象类似。

Example:

例:

    String str = new String("Java Programming");

In the above example, two objects are created along with one reference (i.e. one object is for "string constant pool" and another object is for "heap").

在上面的示例中,创建了两个对象以及一个引用(即,一个对象用于“字符串常量池” ,另一个对象用于“堆” )。

Note:

注意:

  • Whenever we create a String object by using "new" keyword, The Java Virtual Machine (JVM) creates a new string object in "Heap Memory" and the literal "Java Programming" will be placed in "String Constant Pool" and the variable "str" will refer to the string object placed in "Heap Memory".

    每当我们使用“ new”关键字创建String对象时,Java虚拟机(JVM)都会在“堆内存”中创建一个新的字符串对象,并且文字“ Java Programming”将放置在“ String Constant Pool”中 ,而变量“ str”将引用放置在“堆内存”中的字符串对象。

  • When we create string objects by using "new" keyword so the objects are stored in a special memory area known as "Heap".

    当我们使用“ new”关键字创建字符串对象时,这些对象将存储在称为“ Heap”的特殊存储区域中。

Example:

例:

// Java program to demonstrate the example of
// creating string object by using "new" keyword.

public class CreateStringObjectByNewKeyword {
    public static void main(String[] args) {
        // Creating a string object and placed in 
        // memory "Heap"
        String str = new String("Hello, Java");

        // In the above case two new instance is created 
        // [one is for SCP and other is for Heap] along with 
        // one reference
        System.out.println(str);
    }
}

Output

输出量

Hello, Java


翻译自: https://www.includehelp.com/java/what-are-different-ways-to-create-a-string-object-in-java.aspx

java 字符串创建对象

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值