Int, Boolean, double, char, are primitive data types. These are great for storing a whole number, true/false values, a single letter or symbol, but what if you wanted to store and reference some text, something that requires more than just a single character?
In this tutorial, we’ll be looking at the second overarching category of Java data types called Reference types.
what is reference datatype in java?
A reference type is a data type that’s based on a class rather than on one of the primitive types that are built in to the Java language.
Reference data types in java are those which contain reference/address of dynamically created objects. These are not predefined like primitive data types.
Following are the reference types in Java.
- class types − This reference type points to an object of a class.
- array types − This reference type points to an array.
- interface types − This reference type points to an object of a class that implements an interface.
What is string?
A string is a sequence of ordered characters. it is a reference type data type. Before, we could represent a single character with a char, but now we can represent a word or a person’s name with the string data type.
To create a string, we surrounded the series of characters with quotation marks. This represents a string value. There are two ways to represent a string, both are same but different in approach
String techarge = new String("Java is for everyone");
String techarge = "Java is for everyone";
- String Object is used for holding String of text.
- Part of java.lang package.
- Strings can be added by using concatenation Operator(+).
- Characters use single quotes(‘ ‘) as the delimiter while Strings use double quotes(” “) as delimiters.
- String objects are immutable. Value of the string object cannot be changed once assigned.
- String class is final means method of the String class cannot be overridden.
- All String literals are added to String pool by JVM.
- String have method length() while Array has a property length to check the length.
- To overcome the immutable property of String StringBuffer and StringBuilder comes into the picture.