Problem:- How to Concatenation in Java Using + Operator or concatenation in java example java program for concatenating string or java program for print multiple values in one statement. String Concatenation in Java. using + operator.
Explanation:- Concatenation means adding two or more values, Java we have plus ( + ) operator for Concatenation two same and different type of values. The need of Concatenation and why we use?. The simple answer is when we need to add the values and printing the multiple the values in a single line we use Concatenation. Below is the example of Concatenation so you can understand very well.
Example:- Suppose you want to print the output of Factorial of number 5 or any other number if you don't use Concatenation then you need multiple print statement for printing the output of Factorial check the below two examples for printing the output of Factorial. factorial of 5 is 120.
Without using Concatenation
System.out.println("Factorial of ");
System.out.println(number);
System.out.println(" is ");
System.out.println( fact );
So without using Concatenation your output will be
Factorial of
5
is
120
Using Concatenation
System.out.println( "Factorial of " + number + " is " + fact );
Now the output of the program will be
Factorial of 5 is 120
Here number and fact are variable. You notice that both the output of the program.
How to Concatenation in Java Using + Operator
class Program3 { public static void main(String[] args) { System.out.println(20+20); System.out.println("java "+"developer"); System.out.println("Number is"+20); System.out.println("Number is"+20+20); System.out.println("Number is"+(20+20)); System.out.println("20+ is the Number"); System.out.println(20+20+"is the Number"); } }
Output:-
You May Also See
2. Java Program To Find Area Of Triangle
3. Java Program to Convert a person's name in Abbreviated
4. Java Program For Calculate A Simple Interest
5. Java Program For Find The Gross Salary Of An Employee
Thanks For Understable Code
ReplyDelete