Ignora collegamentiHome / Blog

Blog


How to concatenate strings in Java

The + operator is generally used to make the sum of numeric values, but in Java can be used to concatenate strings.

It is possible to concatenate two strings or a string and a value of type default (for example string+int, string+char, string+double)
  
The following example shows how the concatenation in Java:

public class ConcatenationTest
{
	public static void main(String[] args)
	{
	String s = "Hello ";
	String name = "Bill";
	short age = 56;
	int salary = 24000;
	char c = '!';
	double d = 1.846;
			
	System.out.println(s + name);
	System.out.println(name + age);
	System.out.println(name + salary + c);
	System.out.println(name + d);
	}
}

The output of the program is as follows:

Hello Bill
Bill56
Bill24000!
Bill1.846

 



Category: Java