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?
Primitive data types - includes byte , short , int , long , float , double , boolean and char. Non-primitive data types - such as String , Arrays and Classes (you will learn more about these in a later chapter)
Non-primitive data types are called reference types because they refer to objects. The main difference between primitive and non-primitive data types are: Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except for String ).
String is an object, in android or java it isn't a primitive type at all. you can use strings to store in SharedPreferences.
No, string is not a primitive data type in Java, even though it is one of the most extensively used objects. Strings in Java are instances of string classes defined in Java. lang package. This is the reason why string is not a primitive data type.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With