Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

string

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?

like image 486
Ank Avatar asked Dec 16 '14 17:12

Ank


People also ask

Is a string a primitive data type?

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)

What is the non-primitive data type in java?

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 ).

Is java string a primitive?

String is an object, in android or java it isn't a primitive type at all. you can use strings to store in SharedPreferences.

When string can represent any type of data why we use primitive data type?

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.


10 Answers

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.

like image 90
Pranalee Avatar answered Sep 30 '22 07:09

Pranalee


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.

like image 33
Elliott Frisch Avatar answered Sep 29 '22 07:09

Elliott Frisch


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?

like image 28
Caffeinated Avatar answered Sep 29 '22 07:09

Caffeinated


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.

like image 28
Shubham Avatar answered Oct 02 '22 07:10

Shubham


String str = “This is string literal”;

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 str = new String(“this is string created by new operator”);

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.

like image 23
Madhusudan Sharma Avatar answered Oct 03 '22 07:10

Madhusudan Sharma


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.

like image 22
DRastislav Avatar answered Oct 02 '22 07:10

DRastislav


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

like image 20
rousseauo Avatar answered Sep 30 '22 07:09

rousseauo


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);
like image 24
Dunes Avatar answered Sep 29 '22 07:09

Dunes


  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.
like image 27
R.Nish Avatar answered Oct 01 '22 07:10

R.Nish


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.

like image 24
Nicholas Eason Avatar answered Oct 03 '22 07:10

Nicholas Eason