wellnwill Marathahalli Bangalore

String Concatenation Operator (+)

  • If at least one operand is string type then “+” operator performs concatenation.
  • If expression having numeric value only then “+” operator performs arithmetic addition.
  • Expression evaluation left to right.
class Test36
{
  public static void main(String argss[])
  {
    int a=20; 
    int b=20;
    System.out.println("addition is"+a+b);
  }
}

// O/P - addition is 2020
class Test37
{
  public static void main(String argss[])
  {
    int a=20; 
    int b=20;
    System.out.println("addition is"+(a+b));
  }
}

// O/P - addition is 40
class Test38
{
  public static void main(String argss[])
  {
    int a=20; 
    int b=20;
    System.out.println(a+b+" is addition");
  }
}

// O/P - 40 is addition
class Test39
{
  public static void main(String argss[])
  {
    int a=20; 
    int b=20;
    System.out.println(a-b+" is subtraction rs");
  }
}
// O/P - 0 is subtraction rs.
class Test40
{
  public static void main(String argss[])
  {
    int a=20; 
    int b=20;
    System.out.println("subtraction result is"+ a-b);
  }
}

// O/P - CompileTimeError 
       - bad operand type for binary operator.	
          
class Test41
{
  public static void main(String argss[])
  {
    String a="java"; 
    int b=20,c=30,d=40;
    System.out.println(a+b+c+d);
    System.out.println(b+c+d+a);
    System.out.println(b+c+a+d);
  }
 }
 // O/P -  java203040
               90java
               50java40
Footer with Map

Top