Sunday, September 5, 2021

     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). 

  1. Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore (_). Identifiers cannot start with a digit!
  2. After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers.
  3. In practice, there is no limit to the number of characters an identifier can contain
  4. You can't use a Java keyword as an identifier. Table 1-1 lists all of the Java keywords.
  5. 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;

Oracle's Java Code Conventions

Oracle estimates that over the lifetime of a standard piece of code, 20 percent of the effort will go into the original creation and testing of the code, and 80 percent of the effort will go into the subsequent maintenance and enhancement of the code.

Complete List of Java Keywords  
abstract      boolean     break     byte     case     catch     char     class     const 
continue     default        do          double else      extends final     finally   float 
for               goto            if            implements     import   instanceof         int  
interface     long            native    new                  package  private         protected  
public          return        short      static                strictfp   super            switch  
synchronized  this          throw    throws             transient         try     void    volatile    while  assert enum

Classes and interfaces 

The first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase (a format that's sometimes called "CamelCase"). For classes, the names should typically be nouns. Here are some examples:
Dog
Account 
PrintWriter

For interfaces, the names should typically be adjectives, like these: 
 Runnable 
 Serializable

Methods 

The first letter should be lowercase, and then normal CamelCase rules should be used. In addition, the names should typically be verb-noun pairs. For example:

getBalance 
 doCalculation 
 setCustomerName

Variables 
Like methods, the CamelCase format should be used, but starting with a lowercase letter. Oracle recommends short, meaningful names, which sounds good to us. Some examples: 

buttonWidth 
 accountBalance 
 myString

Constants 
Java constants are created by marking variables static and final. They should be named using uppercase letters with underscore characters as separators: 

 MIN_HEIGHT


No comments:

Post a Comment

Variable Declarations

  There are two types of variables in Java: ■ Primitives A primitive can be one of eight types: char , boolean , byte , short , int , long...