Sunday, October 28, 2018

Java String Class Example

Here is the basic String class example to create a new String object in Java and to append one String to another String object.

public class StringTest {

	public static void main(String[] args) {
		/* The following two declarations will create 2 new objects
		 * */
		String string1 = "The"; // declaring a String - method1 
		String string2 = new String("Testbook");
                // declaring a String method2
		
		System.out.println(string1 + string2);
                // appending 2 String 
                //variables into one String. This will create a new object
		System.out.println(string1.concat(string2));
                //appending 2 Strings -method 2
	}

}
 
Output:
TheTestbook
TheTestbook

No comments:

Post a Comment