A Java program is mostly a collection of objects talking to other objects by invoking each other's methods. Every object is of a certain type, and that type is defined by a class or an interface. Most Java programs use a collection of objects of many different types.
Class - A template that describes the kinds of state and behavior that objects of its type support.
Object At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object that is an instance of that class. That object will have its own state and access to all of the behaviors defined by its class.
State (instance variables) Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state.
Behavior (methods) When a programmer creates a class, she creates methods for that class. Methods are where the class's logic is stored and where the real work gets done. They are where algorithms get executed and data gets manipulated.
Identifiers and Keywords
All the Java components we just talked about—classes, variables, and methods— need names. In Java, these names are called identifiers, and, as you might expect, there are rules for what constitutes a legal Java identifier.
Like all programming languages, Java has a set of built-in keywords. These keywords must not be used as identifiers.
The two aspects of Java identifiers that we cover here are:
Legal identifiers The rules the compiler uses to determine whether a name is legal.
Oracle's Java Code Conventions Oracle's recommendations for naming classes, variables, and methods. We typically adhere to these standards throughout the book, except when we're trying to show you how a tricky exam question might be coded. You won't be asked questions about the Java Code Conventions, but we strongly recommend that you use them.
Legal Identifiers:
Technically, legal identifiers must be composed of only Unicode characters, numbers, currency symbols, and connecting characters (such as underscores).
- Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore (_). Identifiers cannot start with a digit!
- After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers.
- In practice, there is no limit to the number of characters an identifier can contain
- You can't use a Java keyword as an identifier. Table 1-1 lists all of the Java keywords.
- Identifiers in Java are case-sensitive; foo and FOO are two different identifiers.
Examples of legal and illegal identifiers follow.
First some legal identifiers:
int _a;
int $c;
int ______2_w;
int _$;
int this_is_a_very_detailed_name_for_an_identifier;
The following are illegal (it's your job to recognize why):
int :b;
int -d;
int e#;
int .f;
int 7g;
No comments:
Post a Comment