首页  >  文章  >  Java  >  Java中的字符串初始化

Java中的字符串初始化

WBOY
WBOY原创
2024-08-30 16:07:271045浏览

字符串初始化是编程的基本部分之一。字符串初始化意味着在java程序中使用变量之前为其赋值。字符串初始化可以通过两种方式完成:

  • 对象初始化
  • 直接初始化

字符串是java中的一个对象。字符串是指某些字符序列。  它也称为字符数组。字符串始终用引号表示;即使引号也不是字符串的一部分。字符串初始化也分两步进行,第一步是声明,第二步是初始化。字符串声明是指声明一个字符串而不显式分配任何值。在大多数用例中,字符串的唯一声明不需要初始化。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

Java 中字符串初始化的类型

在java中,字符串的初始化有多种方式。通过使用构造函数或使用文字。使用 new 关键字和使用 Literal 初始化 String 是有区别的。每次创建新的 java 对象时都使用 new 关键字初始化 String。已定义但未初始化的变量不能在程序中使用,因为它没有保存任何值。

1.对象初始化

使用 new 关键字初始化字符串。使用 new 关键字初始化 String 会在内存堆中创建一个新对象。通过对象初始化的字符串是可变的,这意味着字符串的值可以在初始化后重新分配。

String strMsg = new String("Be specific towards your goal!");

当使用构造函数初始化对象时,java编译器在堆内存中创建一个新对象。堆内存是 JVM 保留的内存。

2.直接初始化

使用文字初始化字符串。使用 Literal 初始化字符串会在内存池区域中创建一个对象。

String strMsg = "Be specific towards your goal!";

当使用文字初始化字符串时,分配给它的值已经在另一个字符串中初始化。在这种情况下,通过 Literal 创建的 String 不会创建新对象。它只是传递了对先前创建的对象的引用。

Java 中字符串初始化的示例

以下解释的是java中字符串初始化的不同示例。

示例#1

在下面创建的字符串中,两个字符串都是使用 Literal 创建的。

代码:

String ObjStringFirst="Welcome";
String ObjStringSecond="Welcome";

在上面给出的字符串中,我们使用字符串文字创建对象。在这种情况下,如果该对象已经存在并且具有相同的值,它将返回相同的对象;否则,它将创建一个新对象。

示例#2

在下面给出的示例中,我们可以看到字符串是如何初始化的。

代码:

public class StringExample {
public static void main(String[] args)
{
String nameFirst = "Alister";
String nameSecond = "Campbell";
String nameThird = new String("Adam");
String nameFourth = new String("Martin");
//String nameFifth;
String strNullFirst  = null;
String strNullSecond = null;
String strNullThird  = null;
System.out.println("String initialized :");
//printing variable which are initialized
System.out.println("First : " + nameFirst);
System.out.println("Second : " + nameSecond);
System.out.println("Third : " + nameThird);
//printing variable which are declared but not initialized
//System.out.println("String declared but not initialized :");
//System.out.println("Fifth : " + nameFifth);
}
}

输出:

Java中的字符串初始化

示例 #3

在此示例中,给出了 String 变量的声明。它包含一个在编译程序时声明但未初始化的变量。

代码:

public class StringExample {
public static void main(String[] args)
{
String nameFirst;
//printing variable which are declared but not initialized
System.out.println("String declared but not initialized :");
System.out.println("Fifth : " + nameFirst);
}
}

输出:

上述程序的输出如下所示。上面给出的程序将在编译时生成错误。字符串已声明但未初始化。

Java中的字符串初始化

在 String 声明之后,如果稍后为声明的 String 变量赋值,则编译不会抛出任何错误(因为 String 初始化分为两部分)。第一个是声明,第二个是赋值。

示例#4

在此示例中,我们使用空值进行字符串初始化。所有通过空值初始化的字符串变量将具有不同的对象,这三个变量都将具有不同的对象,因为通过 Literal 初始化的字符串将在字符串池中创建对象,而通过 new 关键字初始化的字符串将在内存堆区域中创建对象。

代码:

public class StringExample {
public static void main(String[] args)
{
String strVal1 = "";
String strVal2 = new String();
String strVal3 = new String("");
//printing variable which are initialized with empty values
System.out.println("String Values :");
System.out.println("First : " + strVal1);
System.out.println("Second : " + strVal2);
System.out.println("Third : " + strVal3);
}
}

输出:

在上面给出的程序中,即使 Strings 的对象不同,但分配给 String 的值是相同的。下面给出的输出显示了如何将空值分配给每个字符串变量。

Java中的字符串初始化

示例#5

在下面给出的程序中,给出了上面初始化字符串的比较。这里使用三种比较字符串的方式。

1. == operator: In java == operator compare the references, not the values.
2. equals() method: It compares the variable’s value, not the references; hence, if the value matches both the String variable, it returns true other false.
3. compareTo() method: compareTo() method check the values & if values are identical then it return 0 otherwise it return + & – based on the below given comparison.

  • It returns 0 if str1 == str2
  • It returns positive value if str1 > str2
  • It returns negative value if str1 < str2

Code:

public class StringExample {
public static void main(String[] args)
{
String strVal1 = "";
String strVal2 = new String();
String strVal3 = new String("");
//printing variable which are initialized with empty values
System.out.println("String Values :");
System.out.println("First : " + strVal1);
System.out.println("Second : " + strVal2);
System.out.println("Third : " + strVal3);
System.out.println(" \nComparing strings using == operator :");
System.out.println("Comparing first two strings :");
System.out.println(strVal1 == strVal2);
System.out.println("Comparing last two strings :");
System.out.println(strVal2 == strVal3);
System.out.println("Comparing first & last strings :");
System.out.println(strVal1 == strVal3);
System.out.println(" \nComparing strings using equals method :");
System.out.println("Comparing first two strings :");
System.out.println(strVal1.equals(strVal2));
System.out.println("Comparing last two strings :");
System.out.println(strVal2.equals(strVal3));
System.out.println("Comparing first & last strings :");
System.out.println(strVal1.equals(strVal3));
System.out.println(" \nComparing strings using compareTo method :");
System.out.println("Comparing first two strings :");
System.out.println(strVal1.compareTo(strVal2));
System.out.println("Comparing last two strings :");
System.out.println(strVal2.compareTo(strVal3));
System.out.println("Comparing first & last strings :");
System.out.println(strVal1.compareTo(strVal3));
}
}

Output:

The output of the comparing string program is given below. It contains the output after comparing the Strings in three different ways; thus, outcomes of the comparison are also different according to the method applied.

Java中的字符串初始化

Conclusion

The above-given article explains the initialization of String & different types of ways to initialize the String in java. Also given examples in the article describe the internal creation of objects when initiating String using Literal. It also describes the difference between declaration & initialization.

以上是Java中的字符串初始化的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn