The dot (.) operator is used to select members of a class or object instance. (We’ll talk about those in detail later.) It can retrieve the value of an instance variable (of an object) or a static variable (of a class). It can also specify a method to be invoked on an object or class:
int i = myObject.length;
String s = myObject.name;
myObject.someMethod();
A reference-type expression can be used in compound evaluations by selecting further
variables or methods on the result:
int len = myObject.name.length();
int initialLen = myObject.name.substring(5, 10).length();
Here we have found the length of our name variable by invoking the length() method
of the String object. In the second case, we took an intermediate step and asked for a
substring of the name string. The substring method of the String class also returns a
String reference, for which we ask the length. Compounding operations like this is also
called chaining method calls, which we’ll mention later. One chained selection operation
that we’ve used a lot already is calling the println() method on the variable out of the
System class:
System.out.println("calling println on out");
0 comments:
Post a Comment