在 Java 中,为什么 String 是非原始数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27510494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:55:23  来源:igfitidea点击:

In Java, Why String is non-primitive data type?

javastring

提问by Ank

In Java, we can directly use Stringto declare a string variable name and specify its value. We do not have to define the string as an array by using new keyword, even though String is non-primitive data type.

在Java中,我们可以直接使用String来声明一个字符串变量名并指定其值。我们不必使用 new 关键字将字符串定义为数组,即使 String 是非原始数据类型。

Can someone please explain why String is non-primitive datatype?

有人可以解释为什么 String 是非原始数据类型吗?

采纳答案by Madhusudan Sharma

String str = “This is string literal”;

String str = “这是字符串文字”;

This is string literal. When you declare string like this, you are actually calling intern() method on String. This method references internal pool of string objects. If there already exists a string value “This is string literal”, then str will reference of that string and no new String object will be created.

这是字符串文字。当您像这样声明字符串时,您实际上是在对 String 调用 intern() 方法。此方法引用字符串对象的内部池。如果已经存在一个字符串值“这是字符串文字”,那么 str 将引用该字符串并且不会创建新的 String 对象。

String str = new String(“this is string created by new operator”);

String str = new String(“这是new操作符创建的字符串”);

This is string object. In this method JVM is forced to create a new string reference, even if “this is string created by new operator” is in the reference pool.

这是字符串对象。在这种方法中,JVM 被迫创建一个新的字符串引用,即使“这是由 new 操作符创建的字符串”在引用池中也是如此。

回答by Pranalee

String is non-primitive because only class can have methods. Primitive can not. And String need many functions to be called upon while processing like substring, indexof, equals, touppercase. It would not have been possible without making it class.

字符串是非原始的,因为只有类可以有方法。原始不能。并且 String 在处理时需要调用许多函数,例如 substring、indexof、equals、touppercase。如果不让它上课,这是不可能的。

Also class has made it possible to make strings immutable and final to enhance security and efficiency by allowing pooling.

此外,类使字符串不可变和 final 成为可能,通过允许池化来增强安全性和效率。

回答by Elliott Frisch

The StringJavadoc clearly indicates that Stringis a subclass of Object; and further String.equals(Object)overrides Object.equals(Object).

所述String的Javadoc清楚地表明,String是的一个子类Object; 并进一步String.equals(Object)覆盖Object.equals(Object).

JLS-3.10.5. String Literalsspecifies that

JLS-3.10.5。字符串文字指定

A string literalconsists of zero or more characters enclosed in double quotes.

一个字符串常量由双引号括起来的零个或多个字符。

Also, JLS-4.3.3. The Class Stringadds

此外,JLS-4.3.3。类字符串添加

Instances of class String represent sequences of Unicode code points.

A String object has a constant (unchanging) value.

String literals (§3.10.5) are references to instances of class String.

The string concatenation operator + (§15.18.1) implicitly creates a new String object when the result is not a compile-time constant expression (§15.28).

类 String 的实例表示 Unicode 代码点序列。

String 对象具有恒定(不变)值。

字符串文字(第3.10.5 节)是对 String 类实例的引用。

当结果不是编译时常量表达式 ( §15.28)时,字符串连接运算符 + ( §15.18.1) 会隐式地创建一个新的 String 对象。

It's also worth pointing out that arrays are also Object(s), and An Array of Characters is Not a String. Finally, if a Stringwasn't an Objectit couldn't be null.

还值得指出的是,数组也是Object(s),并且字符数组不是 String。最后,如果 aString不是 anObject它就不可能是null

回答by Coffee

Yes String is an object in Java. The fact that it can be used similarto primitives does not contradict

是 String 是 Java 中的一个对象。它可以原语一样使用这一事实并不矛盾

Please refer - Strings are objects in Java, so why don't we use 'new' to create them?

请参考 -字符串是 Java 中的对象,那么为什么我们不使用“new”来创建它们呢?

回答by Shubham

String creats an Object each time you assign a value in its String Pool. Where every time if you create a similar object it will look for that and refer, if that value is not there it will again create a new one. Study more on String Pool you will automatically come to know the difference.

每次您在其字符串池中分配一个值时,字符串都会创建一个对象。每次创建类似对象时,它都会查找并引用该对象,如果该值不存在,它将再次创建一个新对象。研究更多关于字符串池,你会自动了解其中的区别。

回答by R.Nish

  1. String is an array of characters
  2. Primitive data types have limitations which fixed data type
  3. but in strings size is vary so that is the main reason why the the strings are non primitive
  4. String in Java is itself is a class and has its own methods to manipulate and operate over object of String class
  5. Strings has its own feature that they are immutable.
  1. 字符串是一个字符数组
  2. 原始数据类型有固定数据类型的限制
  3. 但在字符串大小是变化的所以这是字符串是非原始的主要原因
  4. Java中的String本身就是一个类,并且有自己的方法来操作和操作String类的对象
  5. 字符串有其自身的特性,即它们是不可变的。

回答by Dunes

I think you are confusing 'primitive' and 'literal'. A primitive is a datatype that is not an object. A literal is a convenient way of describing the bit pattern for a datatype. For instance -1 describes the bit pattern 0xFFFFFFFF for an int,and 'a' describes the unicode code point for a lower case A in 16 bits (0x0061). Literals aren't restricted to describing primitive datatypes. You can describe an array. For instance, int[] a = {1, 2, 3};.

我认为您混淆了“原始”和“文字”。原语是一种不是对象的数据类型。文字是描述数据类型位模式的便捷方式。例如,-1 描述了 int 的位模式 0xFFFFFFFF,而 'a' 描述了 16 位 (0x0061) 中小写 A 的 unicode 代码点。文字不限于描述原始数据类型。您可以描述一个数组。例如,int[] a = {1, 2, 3};

A string literal is just a way of describing an immutable array of characters with some methods attached. The literal is syntactic sugar for describing something that would otherwise be very complicated. For example:

字符串文字只是一种描述带有某些方法的不可变字符数组的方式。文字是一种语法糖,用于描述否则会非常复杂的东西。例如:

String s = "ab";

Is much simpler than:

比以下简单得多:

char[] c = new char[2];
c[0] = 'a';
c[1] = 'b';
String s = new String(c);

回答by Nicholas Eason

In Java, String is an object that stores the location to where the actual "value" of where the String is located.

在 Java 中,String 是一个对象,用于存储 String 所在的实际“值”所在的位置。

You DO need to use the newkeyword when making an array of Strings, as you do with making an array of anything else.

new在制作字符串数组时,您确实需要使用关键字,就像制作其他任何东西的数组一样。

String[] text = new String[4]

String[] text = new String[4]

This create's four String referencesthat lead the computer to where the text is located at. Also, all Strings default to a value of nullbecause until you give them a value to store at a memory address, there is nothing to be stored.

这将创建四个字符串引用,将计算机引导至文本所在的位置。此外,所有字符串的默认值都是 ,null因为在您给它们一个值以存储在内存地址之前,没有什么可以存储的。

回答by rousseauo

String is a Java Object and not a primitive data type. String is part of the java.lang package that is imported by default in any java project.

字符串是 Java 对象,而不是原始数据类型。String 是 java.lang 包的一部分,在任何 java 项目中默认导入。

There is no need to define an array of char, just use String.

不需要定义一个char数组,只需要使用String。

Possible duplicate: Java String import

可能重复:Java 字符串导入

回答by DRastislav

String is object, is immutable, that means that you cannot change the object itself, but you can change the reference to the object.

String 是对象,是不可变的,也就是说你不能改变对象本身,但是你可以改变对对象的引用。

This is how String works

这就是字符串的工作方式

String myStr = "test";

This as usual, creates a string named "test" and assign it a reference "myStr".

像往常一样,创建一个名为“test”的字符串并为其分配一个引用“myStr”。

Important point to note here is, while the String object is immutable, its reference variable is not.

这里要注意的重要一点是,虽然 String 对象是不可变的,但它的引用变量不是。