CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ

Java

Java Programming
String variables
Join and append strings

How to join strings

String firstName = "Bob"; // firstName is Bob
String lastName = "Smith"; // lastName is Smith
String name = firstName + " " + lastName; // name is Bob Smith

How to join a string and a number

double price = 14.95;
String priceString = "Price: " + price;

How to append one string to another with the + operator

firstName = "Bob"; // firstName is Bob
lastName = "Smith"; // lastName is Smith
name = firstName + " "; // name is Bob followed by a space
name = name + lastName; // name is Bob Smith

How to append one string to another with the += operator

firstName = "Bob"; // firstName is Bob
lastName = "Smith"; // lastName is Smith
name = firstName + " "; // name is Bob followed by a space
name += lastName; // name is Bob Smith

Previous | String variables | Create a String object | Join and append strings | Special characters | Next