Before you begin

This unit is part of the “Intro to Java programming” learning path. Although the concepts discussed in the individual units are standalone in nature, the hands-on component builds as you progress through the units, and I recommend that you review the prerequisites, setup, and unit details before proceeding.

Unit objectives

  • Learn multiple ways to instantiate a String object and set its value
  • Understand string concatenation and method chaining
  • Gain familiarity with the Java language’s arithmetic operators

Strings

So far, you’ve encountered several variables of type String. In the Java language, strings are first-class objects of type String, with methods that help you manipulate them.

Here are a couple of ways to create a String, using the example of creating a String instance named greeting with a value of hello:


greeting = new String("hello");


String greeting = "hello";

Because Strings are first-class objects, you can use new to instantiate them. Setting a variable of type String to a string literal has the same result, because the Java language creates a String object to hold the literal, and then assigns that object to the instance variable.

Concatenating strings

You can do many things with String, and the class has many helpful methods. Without even using a method, you’ve already done something interesting within the Person class’s testPerson() method by concatenating, or combining, two Strings:


l.info("Name: " + p.getName());

The plus (+) sign is shorthand for concatenating Strings in the Java language. (You incur a performance penalty for doing this type of concatenation inside a loop, but for now, you don’t need to worry about that.)

Concatenation exercise

Now, you can try concatenating two more Strings inside of the Person class. At this point, you have a name instance variable, but it would be more realistic in a business application to have a firstName and lastName. You can then concatenate them when another object requests Person‘s full name.

Return to your Eclipse project, and start by adding the new instance variables (at the same location in the source code where name is currently defined):


//private String name;
private String firstName;
private String lastName;

Comment out the name definition; you don’t need it anymore, because you’re replacing it with firstName and lastName.

Chaining method calls

Now, tell the Eclipse code generator to generate getters and setters for firstName and lastName (refer back to Unit 5 if necessary). Then, remove the setName() and getName() methods, and add a new getFullName() method to look like this:


public String getFullName() {
  return getFirstName().concat(" ").concat(getLastName());
}

This code illustrates chaining of method calls. Chaining is a technique commonly used with immutable objects like String, where a modification to an immutable object always returns the modification (but doesn’t change the original). You then operate on the returned, changed value.

Operators

You’ve already seen that the Java language uses the = operator to assign values to variables. As you might expect, the Java language can do arithmetic, and it uses operators for that purpose too. Now, I give you a brief look at some of the Java language operators you need as your skills improve.

The Java language uses two types of operators:

  • Unary: Only one operand is needed.
  • Binary: Two operands are needed.

Table 1 summarizes the Java language’s arithmetic operators:

Table 1. Java language’s arithmetic operators
Operator Usage Description
+ a+b Adds a and b
+ +a Promotes a to int if it’s a byte, short, or char
- a - b Subtracts b from a
- -a Arithmetically negates a
* a * b Multiplies a and b
/ a / b Divides a by b
% a % b Returns the remainder of dividing a by b (the modulus operator)
++ a++ Increments a by 1; computes the value of a before incrementing
++ ++a Increments a by 1; computes the value of a after incrementing
-- a-- Decrements a by 1; computes the value of a before decrementing
-- --a Decrements a by 1; computes the value of a after decrementing
+= a += b Shorthand for a = a + b
-= a -= b Shorthand for a = a - b
*= a *= b Shorthand for a = a * b
%= a %= b Shorthand for a = a % b

Additional operators

In addition to the operators in Table 1, you’ve seen several other symbols that are called operators in the Java language, including:

  • Period (.), which qualifies names of packages and invokes methods
  • Parentheses (()), which delimit a comma-separated list of parameters to a method
  • new, which (when followed by a constructor name) instantiates an object

The Java language syntax also includes several operators that are used specifically for conditional programming — that is, programs that respond differently based on different input. You look at those in the next unit.

Previous: Adding behavior to a Java classNext: Conditional operators and control statements