String Basics

 

String Basics

Strings in java is a sequence of characters that Is enclosed in double quotes. Whenever java comes across a String literal in the code, it creates a string literal with the value of string.


Example:

public class string {
    public static void main(String[] args) {
        String name;
        name = "Maxon";
        System.out.println("My name is " + name);
    }
}


Output:

My name is Maxon

 

The same can be done using an array of characters.


Example:


public class string {
    public static void main(String[] args) {
        char[] name = {'M', 'a', 'x', 'o', 'n'};
        String welcomeMsg = new String(name);  
        System.out.println("Welcome " + welcomeMsg);
    }
}


Output:

Welcome Maxon

 

Concatenate Strings:

Concatenation between two strings in java is done using the + operator.


Example:


public class string {
    public static void main(String[] args) {
        String fname, lname;
        fname = "Maxon";
        lname = "Codes";
        System.out.println(fname + " " + lname);
    }
}


Output:

Maxon Codes

 

Alternatively, we can use the concat() method to concatenate two strings.


Example:


public class string {
    public static void main(String[] args) {
        String fname, lname;
        fname = "Maxon";
        lname = " Codes";
        System.out.println(fname.concat(lname));
    }
}


Output:

Maxon Codes

 

What if we concatenate string with an integer?


Well concatenating a string and an integer will give us a string.


Example:

public class string {
    public static void main(String[] args) {
        String name;
        int quantity;
        quantity = 12;
        name = " Apples";
        System.out.println(quantity + name);
    }
}


Output:

12 Apples