Combining strings with + sign - Java Language Basics

Java examples for Language Basics:String

Introduction

Combine two strings by using the plus sign + as a concatenation operator.

Demo Code

public class Main {
  public static void main(String[] args) {
    String hello = "Hello, ";
    String world = "World!";
    String greeting = hello + world;

    System.out.println(greeting);
  }/*from   ww  w  .j  a v a 2s  .co  m*/

}

You can concatenate a string literal along with the string variables.

For example:

Demo Code

public class Main {
  public static void main(String[] args) {
    String hello = "Hello";
    String world = "World!";
    String greeting = hello + ", " + world;

    System.out.println(greeting);
  }/*ww w.jav  a2  s. c o m*/
}

Related Tutorials