Wednesday, June 4, 2014

String Concatenation Operator

String name = "Venkatesh";
int age = 5;
int salary = 6;

System.out.println(name + age + salary);

What is the Output?

Answer:- 

Rule:
If either operand is a String, the + operator becomes a String concatenation operator. If both operands are numbers, the + operator is the addition operator.

Note: The operands are the things on the left or right side of the operator.
Example: the expression a+b --> here a & b are the operands and "+" is operator.


Finally the answer is : Venkatesh56

2 comments:

  1. the order is from left to right. Am I correct ?
    for ex in String c=5+4+"String"+5+4;
    the result is 9String54

    ReplyDelete