In Java Language Strings can be initialized by enclosing a string value in double quotes. Strings can be concatenated using the plus ('+') operator. So is String considered a Java data type? Inspite of all the above, String is not considered a primitive data type in Java.
Primitive Data Types
Primitive data types are pre-defined in the Java Language Specification. Java defines the following eight primitive data types and they are reserved words.
Special status enjoyed by String
String is a pre-defined class and it belongs to java.lang package. String objects are immutable. String objects enjoy some special treatment in Java. Besides String objects are immutable.
// Strings can be initialized by assigning string literals. String message = "Strings are special"; // String objects can be concatenated String newString = message + " in Java."; // Compiler performs concatenations at compile time if possible // The variable 'helloString' would be initialized with "Hello World" // at compile time itself! String helloString = "Hello " + "World" ; // Java 'interns' the Strings - that is - maintains a special lookup // table called 'string intern pool' of unique string literals // used in the code and reuses string references. // The variable 'message' and 'anotherMsg' have the same reference // pointing to the string literal "Strings are special"; String anotherMsg = "Strings are special";
Even though it feels like String should be a primitive data type in Java, it is not.