Sunday 25 December 2016

Concatenate Two String Using + operator





Concatenate Two String Using + operator

   Basically does not allow the use of operators with string object, the + operator being an exception to this. It is used to concatenate two strings and creates a string object. It is the easiest way to join two String, or a String and a numeric in Java. Here is an example:

ConcatenateByPlus.java


class ConcatenateByPlus
{
  public static void main(String arg[])
  {
     String s1=”Learning Java”;
    String s2=” “;
    String s3=”is fun.”;
    Str=s1+s2+s3;
    System.out.println(str);
  }
}

  Output:
    Learning Java is fun.

  The + operator can also be used to concatenate a String object and different data type. The following example illustrate this.

TestCancate.java

class TestCancate
{
  Public satatic void main(String arg[])
  {
    Int day=10;
    String s1=”He is learning java”;
    String s2=”days.”;
    System.out.println(s1+” ”+day+””+s2);

  }
}

  The output generated is:
    He is learning java 10 days.




0 comments:

Post a Comment

Powered by Blogger.

Stats