String Concatenation

String concatenation is the process where we add 2 strings to make up a new string. This can be done using the addition operator ( + ).

For instance :

String firstName = “Lahiru”;

String lastName = “Hashan”;

String name = firstName + lastName;

check out the output :

Output : LahiruHashan

No space because there is no space given.

Check out these examples :

stringconcat

Playing around with numbers :

stringconcat2

Now identify the new line and its output. Check what has happened.

stringconcat2

Now the answer has pop up without the brackets. As you can see the execution is happening from left to right. Therefore here the addition of 2 floats takes place first and then addition to the string takes place. This is an important aspect of Java.

There is a special method in String class to add strings, named “concat” method. We will be discussing in the next tutorial along with the other String methods.

See you next time.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s