Sunday, May 28, 2017

Introduction to Java programming, Part-1 (9)

Strings and operators


The tutorial has so far introduced several variables of type String, but without much explanation. You learn more about strings in this section, and also find out when and how to use operators.

Strings

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 typeString 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 nameinstance 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 firstNameand lastName.

Chaining method calls

Now, tell the Eclipse code generator to generate getters and setters for firstName and lastName (refer back to the "Your first Java class" section 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 2 summarizes the Java language's arithmetic operators:
Table 2. Java language's arithmetic operators
Additional operators
In addition to the operators in Table 2, 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 section.
Previous Post
Next Post

post written by:

0 coment�rios: