1. Data and Object
Data of Java Objects are stored in instance variables (also called fields)
Instance variables must have a type, either be a base type or a reference
2. Class Modifiers
The abstract class modifier describes a class that has abstract methods. Abstract methods are declared with the abstract keyword and are empty (that is, they have no block defining a body of code for this method). An abstract class usually has a mixture of abstract methods and actual methods.
The final class modifier describes a class that have no subclasses.
The public class modifier describes a class that can be instantiated or extended by anything in the same package or by anything that imports the class.
If the public class modifier is not used, the class is considered friendly. This means that it can be used and instantiated by all classes in the same package.
3. Basic Types
boolean | Boolean value: true or false |
char | 16-bit Unicode character |
byte | 8-bit signed two's complement integer |
short | 16-bit signed two's complement integer |
int | 32-bit signed two's complement integer |
long | 64-bit signed two's complement integer |
float | 32-bit floating-point number |
double | 64-bit floating-point number |
A variable declared to have one of these types simply stores a value of that type, rather than a reference to some object. Therefore when we create a number object, we do the following:
BaseType | ClassName | Creation Example | Access Example |
byte | Byte | n = new Byte((byte)34) | n.byteValue() |
short | Short | n = new Short((short)100) | n.shortValue() |
int | Integer | n = new Integer(1045) | n.intValue() |
long | Long | n = new Long(10849L) | n.longValue() |
float | Float | n = new Float(3.934F) | n.floatValue() |
double | Double | n = new Double(3.934) | n.doubleValue() |
4. Variable Modifiers
- public: Anyone can access public instance variables.
- protected: Only methods of the same package or of subclasses can access protected instance variables.
- private: Only methods of the same class(not methods of a subclass) can access private instance variables.
- If none of the above modifiers are used, the instance variable is considered friendly. Friendly variables can be accessed by any class in the same package.
- static: A static variable is used to declare a variable that is associated with the class, not with individual instances of that class. Static variables are used to store "global" information about a class. It can exist even if no instance of their class is created.
- final: A final instance variable is one that must be assigned an initial value, and then can never be assigned a new value after that. it is a base type, then a constant.
All parameters in java are passed by value, that is, any time we pass a parameter to a method, a copy of that parameter is made for use within the method body. So if we pass an int variable to a method, then that variable's integer value is copied. The method can change the copy but not the original. If we pass an object reference as a parameter to a method, then the reference is copied as well. Remember that we can have many different variables that all refer to the same object.
5. BufferedReader
java.io.BufferedReader stndin; // Standard input(buffered)
stndin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
使用"stndin.readLine()"就可以得到input
6. Package
用法: 在文件最上面写: package (package_name);
import (package_name).* ; 就能够import所有的在次包里面的文件了.
In the case where two packages have classes of the same name, we must specifically reference the package that contains a class. Like this:
Gnomes.Mushroom shroom = new Gnomes.Mushroom("purple");
Cooking.Mushroom topping = new Cooking.Mushroom();
7. Writing Pattern
Responsibility: divide the work into different actors, each with a different responsibility. Try to describe responsibilities using action verbs. These actors will form the classes for the program.
Independence: define the work for each class to be as indempendent from other.
Behaviors: so that the consequences of each action performed by a class will be well understood by other classes that interact with it.
8. Exceptions
How to throw an exception object:
if (insertIndex > size()) {
throw new
BoundaryViolationException("No element at index " + insertIndex);
}
or
public void getReadyForClass() throws ShoppingListTooSmallException, OutOfMoneyException {
goShopping();
makeCookiesForTA();
}
Exception and Error are subclasses of Throwable, which denotes any object that can be thrown and caught.
Catching Exceptions:
try-catch block
try
(block_of_statements_1)
catch ((exception_type) (identifier))
(block_of_statements_2)
[catch ((exception_type) (identifier))
(block_of_statements_3)
...
[finally
(block_of_statements_n)]