0% found this document useful (0 votes)
15 views

C4learn - Java Concatenate Strings

This document discusses concatenating strings in Java. It provides an example Java program that concatenates three strings using the + operator, resulting in the sentence "I love my country India". It then concatenates a string and integers, producing the outputs "India1010" and "20India". The explanation notes that the + operator concatenates operands from left to right, and converts non-string operands to strings.

Uploaded by

qabiswajit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

C4learn - Java Concatenate Strings

This document discusses concatenating strings in Java. It provides an example Java program that concatenates three strings using the + operator, resulting in the sentence "I love my country India". It then concatenates a string and integers, producing the outputs "India1010" and "20India". The explanation notes that the + operator concatenates operands from left to right, and converts non-string operands to strings.

Uploaded by

qabiswajit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

In this chapter we will be learning Concatenating two strings in java

programming. Concatenating two strings means combining two


objects in java.
Table of content [hide]

o
o
o

1 Joining two Strings [Concatenate Strings] in Java :


2 Output :
3 Explanation of the Program :
3.1 First Concatenation :
3.2 Second Concatenation :
3.3 Third Concatenation :

Joining two Strings [Concatenate Strings] in Java :


Consider the following example
public class ConcatenateString {
public static void main(String[] args) {
String Str1 = "I love";
String Str2 = "my country";
String Str3 = "India";
String myString;
myString = Str1 + Str2 + Str3;
System.out.println("Complete Statement : " +
myString);
myString = Str3 + 10 + 10;
System.out.println("Complete Statement : " +
myString);
myString = 10 + 10 + Str3;
System.out.println("Complete Statement : " +
myString);
}
}

Output :
Complete Statement : I love my country India
Complete Statement : India1010
Complete Statement : 20India

Explanation of the Program :

In Java Programming when two strings will be combined together


then it will create another object of string. Below are some thumb
rules while adding or combining two strings.
Operand 1

Operand 2

Result of Concatenation

Integer

String

String

String

String

String

Integer

Integer

Integer

1.

+ operator has associtivity from left to right.

2. Each time + operator will check the type of operands and then
decide which operation to perform.
3. If any of the operand is of type String then another type will
converted to String using respective wrapper classes.

First Concatenation :
myString =
=
=
=

Str1 + Str2 + Str3;


"I love " + "my country " + "India"
"I love my country " + "India"
"I love my country India"

Second Concatenation :
myString =
=
=
=

Str3 + 10 + 10;
"India" + 10 + 10
"India10" + 10
"India1010"

Third Concatenation :
myString =
=
=
=

10 + 10 + Str3;
10 + 10 + "India"
20 + "India"
"20India"

You might also like