Variable Declarations
-
Must declare a variable (once) before using it
-
Form of a declaration:
-
type identifier;
-
type identifier, identifier, ...;
-
Convention - start identifier with lower case - indicate multiple words
with uppercase letter
-
Form of a declaration:
-
String msg;
-
String messageOfTheDay, soupOfTheDay;
Assignment
-
Must always assign to a variable before using it!
-
Form of a assignment:
-
identifier = value;
-
left hand side (or LHS) refers to id
-
right hand side (or RHS) refers to value
-
variable must be declared, types must be consistent
-
LHS is used to store, while RHS used to retrieve
-
Examples:
String teamName;
PrintStream outputMonitor;
teamname = "Tigers"; // legal
outputMonitor = System.out; // legal
teamName = System.out; // illegal
teamName = outputMonitor; // illegal
teamname = "The Clemson Tigers"; // legal
String s;
s = teamName; // legal
String s; // illegal
PrintSteam s; // illegal
Assignment Changes Variables
-
Consider:
String teamName = "Tigers";
teamName = "Blue Devils";
teamName = "Tar Heels";
-
Each assignment sets teamName to a new value.
-
Old references are gone.
Reference Variables and Objects
-
Reference variable refer to objects not reference variables!
-
Consider:
String teamName = "Tigers";
String name;
name = teamName;
-
name and teamName refer to same object!
Variables are Independent
-
Reference variable refer to objects not reference variables!
-
Consider:
String teamName = "Tigers";
String name;
name = teamName;
teamName = "Clemson";
name = teamName;
Ordering Statements - More than one way to skin a cat
-
Consider:
class Program2 {
public static void main(String arg[]) {
String teamName;
String bigTeamName;
teamName = "clemson tigers";
bigTeamName = teamName.toUpperCase();
System.out.println(teamName);
System.out.println(bigTeamName);
}
}
class Program2 {
public static void main(String arg[]) {
String teamName;
teamName = "clemson tigers";
String bigTeamName;
bigTeamName = teamName.toUpperCase();
System.out.println(teamName);
System.out.println(bigTeamName);
}
}
class Program2 {
public static void main(String arg[]) {
String teamName = "clemson tigers";
String bigTeamName = teamName.toUpperCase();
System.out.println(teamName);
System.out.println(bigTeamName);
}
}
More String Methods
-
Prototypes for String:
| Method |
Returns |
Arguments |
Description |
| toUpperCase |
String |
none |
Creates uppercase version of given string |
| toLowerCase |
String |
none |
Creates lowercase version of given string |
| length |
a number |
none |
Length of given string |
| trim |
String |
none |
Creates a string with leading and ending blanks removed |
| concat |
String |
String |
Creates a string with given string concatenated to String object |
| substring |
String |
positions of first char and last char + 1 |
Creates a string that is substring of given string |
| substring |
String |
position of first char |
Creates a string that is substring of given string starting at position
of first char |
-
Note - Strings are indexed by 0
-
substring is an overloaded method, i.e., class defines two methods with
same method-name but different signatures
Cascading
-
Message sent to object is returned by message and in turn message sent
object returned by that message
String firstName = "Robert";
String lastName = "Martin";
String yearBorn = "1956";
String code;
code = firstName.substring(0,2).concat (lastName).substring(0,4).concat (yearBorn);
Composition
-
Return value of one message becomes argument for another.
String firstName = "Robert";
String lastName = "Martin";
String yearBorn = "1956";
String code;
code = firstName.substring(0,2).concat (lastName.substring(0,2).concat (yearBorn);
Know Terminology and review at end of chapter