Wednesday 8 February 2017

Java String Concatenation




String Concatenation:

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:

 class ConcatenateByPlus
 {
  public static void main(String arg[])
  {
   String s1="Learning Java";
   String s2=" ";
   String s3="is fun.";
   String 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.

 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 by the program is:

He is learning java 10 days.



 Next Topic Shahbaz

0 comments:

Post a Comment

Powered by Blogger.

Stats