How To Perform String Concatenation With Primitive Data Type Values in Java

       String Concatenation In Java

 

Hello Friends,

In this tutorial we are going to perform String Concatenation on  Primitive Data Type.

“String are nothing but the sequence of characters”.

String str = "Sankalp";

 

Some Basic Prerequisite about String Concatenation –

 

Basically, string concatenation means joining many strings together to formed a combined string as a result. In Java we can perform the concatenation of the strings by the datatypes – int, double, char, etc.

In Java the string concatenation is made using the ” + ” operator. This operator simply combines the string with other datatype.

“When you pass a string to concatenate it with the other primitive datatype the JVM converts the primitive datatype into the string first then it concatenates them. This is the most important thing which happens implicitly as the user has no idea about it.”

Some of the Primitive Datatypes for String Concatenation –

  • int
  • double
  • char
  1. int –
int age = 21;
String str = "Sankalp's Age is :" + age;

In the above example, we have assigned the value 21 to a variable named age, after that we have concatenated the string with the primitive datatype – int and assigned it to the str object of String class. The thing that happened in this case is that, the string – “Sankalp’s Age is :” which is a string itself is concatenated by converting the primitive datatype int to  String and then the two string got concatenated as –

“Sankalp’s Age is : 21”

This is the flow of how the String Concatenation works in Java.

We will see some Complex Concatenation to Understand this Concept More Clearly with an example-

int age = 21;
double stipend = 25000.00;

System.out.println("Hello Sankalp, Your Age is : " + age + " You have Rs." + stipend + " stipend.");

In this example we have two primitive datatypes i.e, int & double.

First we assigned a value 21 to the age and then taken a value of double type and assigned it to a variable named stipend.

After that we have printed a message ” Hello Sankalp, Your Age is: ” after that we have concatenated the integer value with it which will be implicitly converted to a string first then it will a string and then concatenated with the previous message.

Afterwards, we have printed the next message as “You have Rs.”  at this moment we have try to concatenate the double type value with the String message which will be converted first to string type then it will be printed as a string as usual.

So the thing we have to provide is the name of the primitive datatype variable followed by the concatenation operator ” + ” so that we can concatenate the String with other datatypes.

 

Go through the following key points :

 

The order of the concatenation is very important when you perform concatenation. If you start with a string , Java treats the whole operation of concatenation on string. If you start with variables of int type then it will perform the arithmetic operations first.

Lets illustrate this with the help of following example –

int a = 10;
int b = 20;
int c = 30;

String s1 = a + b + c + " is the Addition of The Given Nos.";
String s2 = "The Addition Of the given nos is: " + a + b + c;

In the first String named s1 the thing that will happen is-

First we have performed the concatenation of the integer type variables so the arithmetic

The operation will be performed on them, and then it will be concatenated with the next message.

 

In the next String named s2,
We have first taken a string message which will starts by order to performed the string concatenation

in this case so the message will be concatenated with the integer nos and the integer nos will not be

added as the order has started by string so it will just combine the numbers as 102030 and treat them as a string.

 

Thanks!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top