In Java, we can directly use String
to 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.
Can someone please explain why String is non-primitive datatype?
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.
Also class has made it possible to make strings immutable and final to enhance security and efficiency by allowing pooling.
The String
Javadoc clearly indicates that String
is a subclass of Object
; and further String.equals(Object)
overrides Object.equals(Object)
.
JLS-3.10.5. String Literals specifies that
A string literal consists of zero or more characters enclosed in double quotes.
Also, JLS-4.3.3. The Class String adds
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 ).
It's also worth pointing out that arrays are also Object
(s), and An Array of Characters is Not a String . Finally, if a String
wasn't an Object
it couldn't be null
.
Yes String is an object in Java. The fact that it can be used similar to primitives does not contradict
Please refer - Strings are objects in Java, so why don't we use 'new' to create them?
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.
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.
There is no need to define an array of char, just use String.
Possible duplicate: Java String import
String is object, is immutable, that means that you cannot change the object itself, but you can change the reference to the object.
This is how String works
String myStr = "test";
This as usual, creates a string named "test" and assign it a reference "myStr".
Important point to note here is, while the String object is immutable, its reference variable is not.
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};
.
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);
In Java, String is an object that stores the location to where the actual "value" of where the String is located.
You DO need to use the new
keyword when making an array of Strings, as you do with making an array of anything else.
String[] text = new String[4]
This create's four String references that lead the computer to where the text is located at. Also, all Strings default to a value of null
because until you give them a value to store at a memory address, there is nothing to be stored.
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.
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.
Primitive data types are types that don't have a method. Whereas strings have method eg toupper, lowercase etc. In intellij you can check is there is a method or not.
this is showing the methods for the (int) which shows no methods(it is primitive
This image is showing the (String) which shows methods (it is not primitive)
Because string is actually a group of character (char) datatype. So it is being derived from char datatype. It have its origin datatype i. E. Char but int float chat etc do not have origin type. They are unique. Which is why string is derived datatype
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.