In the earlier examples, we have seen the + sign. And as we know, + is used for addition of two numbers.
Well! In Java, + can be used to join two Strings as well.
Say, we have two Strings, Hello and World. And we want to join/concatenate them into one String, HelloWorld.
Let us solve it with the below example.
public class MyApplication { public static void main(String[] args) { String x = "Hello"; String y = "World"; String z = x + y; System.out.println(z); } }
So, we have stored the String Hello in x.
String x = "Hello";
And, stored the String World in y.
String y = "World";
And used the + operator to join/concatenate Hello and World.
String z = x + y;
And store the concatenated value in z.
So, + is not just used to add two numbers but + is also used to join/concatenate two Strings.
Next, let us see, how can we iterate the elements of a String.