String concatenation with primitive data type values in Java

Here, we are going to learn how to concatenate a string with primitive data type values in Java programming language?
Submitted by IncludeHelp, on July 14, 2019

Given a string and some of the primitive data type values, we have to concatenate them with the string in Java.

In the example below, we have declared and initialized some of the primitive type of objects and a string object; we are adding the value of primitive types in the string object and printing the values of the result.

Java code for string concatenation with primitive data type values

// Java code for string concatenation with 
// primitive data type values

public class Main {
    public static void main(String[] args) {
        boolean isMarried = false;
        boolean isQualified = true;
        int age = 21;
        double weight = 67.85;
        char gender = 'M';
        String name = "Shivang Yadav";
        String result = null;

        result = "isMarried: " + isMarried;
        System.out.println(result);

        result = "isQualified: " + isQualified;
        System.out.println(result);

        result = name + " is " + age + " years old.";
        System.out.println(result);

        result = name + " weight is " + weight + " kg.";
        System.out.println(result);

        result = "His gender is: " + gender;
        System.out.println(result);
    }
}

Output

isMarried: false
isQualified: true
Shivang Yadav is 21 years old.
Shivang Yadav weight is 67.85 kg.
His gender is: M

Java String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.