A string is a sequence of characters. In Java, string is the most important data type. A string is enclosed in quotation marks, as in the case of the example “KrugerPark”. But the quotation marks are not themselves part of the string. The string are useful in representing various types of non-numeric data, such as dates, times, names and addresses.

The primitive data types are specified by the keywords int, double, etc. While these keywords start with a lowercase letter, the keyword String, which represents the string data type, starts with an uppercase letter. This is because of the fact that keyword String is the name of a predefined class. A Java string is an instantiated object of the String class.

A String variable is simply a variable that stores a reference to an object of the class String. You declare a String variable in much the same way as you define a variable of one of the basic types. You can also three way to initialize it in the declaration. For example :

string declaration and initialization javaThis declares the variable str1, str2 and str3 as type String and initializes it with a reference to a String object encapsulating the specified string.


string declaration and initialization javastring declaration and initialization java

Program Source

public class Javaapp {
    public static void main(String[] args) {
        String str1 ;
        str1 = new String("C Programming");
        String str2 = new String("C++ Programming");
        String str3 = "Java Programming";