如何在Java中初始化和比较字符串?
众所周知,String 在Java中是不可变的,所以它产生了两种初始化方式,因为我们在Java中确实有 String Pool 的概念。
方法:在Java中初始化字符串
- 直接初始化
- 间接初始化
方式一:直接初始化(字符串常量)
在此方法中,将在内存堆区域内的字符串池区域中创建一个字符串常量对象。因为它是一个常量,我们不能修改它,即 String 类是不可变的。
插图:
String str = "GeeksForGeeks";
str = "geeks"; // This statement will make str
// point to new String constant("geeks")
// rather than modifying the previous
// String constant.
来自左翼媒体
String str = "GeeksForGeeks";
从上面的正确媒体
str = "geeks";
Note: If we again write str = “GeeksForGeeks” as next line, then it first check that if given String constant is present in String pooled area or not. If it present then str will point to it, otherwise creates a new String constant.
方式二:对象初始化(动态)
在此方法中,将在堆区域(而不是在大写的字符串池区域内)创建一个 String 对象。我们不能修改它(比如大写)。同样具有相同的值,在字符串池区域中也会创建一个字符串常量,但该变量将仅指向堆区域中的字符串对象。
插图:
String str = new String("very");
str = "good";
我们可以从中得出一些结论:
左下方媒体
String str = new String("very");
来自以下媒体的权利如下:
str = "good"
现在,这是一个直接赋值,因此在字符串池区域中创建了一个值为“good”的字符串常量,str 将指向它。
Note: If we again write str = new String(“very”), then it will create a new object with value “very”, rather than pointing to the available objects in heap area with same value.But if we write str = “very”, then it will point to String constant object with value “very”, present in String pooled area.
方法:比较字符串及其引用
- equals() 方法:它比较字符串的值是否相等。返回类型是布尔值。在几乎所有情况下,您都可以使用 useObjects.equals()。
- ==运算符:它比较引用而不是值。返回类型是布尔值。 == 用于您知道自己正在处理实习字符串的极少数情况。
- compareTo() 方法:它按字典顺序比较值并返回一个整数值,该值描述第一个字符串是否小于、等于或大于第二个字符串。例如,如果 str1 和 str2 是两个字符串变量,则参考如下:
- str1 == str2:返回 0
- str1 > str2 :返回正值
- str1 < str2 :返回负值
Note: The positive and negative values returned by compareTo method is the difference of first unmatched character in the two strings.
实现:我们将通过下面的示例讨论如何与字符串进行比较以证明上述内容的合理性
例子:
Java
// Java program to Illustrate Comparison of Two Strings
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input strings to compare
String s1 = "Ram";
String s2 = "Ram";
String s5 = "Shyam";
String s3 = new String("Ram");
String s4 = new String("Ram");
// Checking whether strings are equal or not
// with help of equals() method
System.out.println(
" Comparing strings with equals:");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equals(s5));
String nulls1 = null;
String nulls2 = null;
// NullPointerException will be throws if
// we try to compare nulls strings
// System.out.println(nulls1.equals(nulls2));
// Comparing strings using == operator
System.out.println(" Comparing strings with ==:");
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s3 == s4);
System.out.println(nulls1 == nulls2);
// Comparing strings via compareTo() method
System.out.println(
" Comparing strings via compareTo() Method :");
System.out.println(s1.compareTo(s3));
System.out.println(s1.compareTo(s5));
// NullPointerException if we try to compare strings
// with usage of compareTo() method
// System.out.println(nulls1.compareTo(nulls2));
}
}
Comparing strings with equals:
true
true
false
Comparing strings with ==:
true
false
false
true
Comparing strings via compareTo() Method :
0
-1