ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.