Note: All tips that is written by book will has a sign near it.
All code example in this post comes from book.
Chapter 1: Java Basics
The structure of a Java class and source code file
- Java source code file: xxx.java
- Java compiler: javac (.exe)
Java bytecode (compiled code for JVM): xxx.class
All java classes belongs to a
package: default(no name) or user-defined.
Package statementshould be the first statement in a class.
Package statementshould be defined exactly once in a class.
import statement can not be placed before a package statement.
/*...*...*...*/ are all acceptable in java comments.
- The properties of an object are implemented using
- The behavior is implemented using
- The state of a class is defined using
A class is a design from which an object can be created.
interface specifies a constract for the classes to implement.
A source code file can not define more than one
public class or interface.
Executable Java Applications
An exectutable Java class has a
main method. A
main method must be:
- name as
array of stringparameter:
java12345public static void main(String... args) // Validpublic static void main(String args...) //Compile errorpublic static void main(String args) // Validpublic static void main(String args) //Validstatic public void main(String args) // Valid
The method parameters that are passed to
main method in command line are also called
command-line parameters or
Rules about packages:
- Based on Java naming conventions, package names should all be in low case.
- There can be a maximum of one package statement per Java source code file(
Importing more classes does not increase the size of your own class. You can’t import classes with same name from different package.
Default page is automatically imported. A class from default page can not be used in any named packaged class, regardless of whether they are defined within the same directory or not.
static varaible can be accessed by using
import static. The order of
static can not be reversed.
Java access modifiers
top-level class is a class that is not defined within any other class. A
top-level class can be defined only using the
default access modifiers.
Local variables and
methods can not be defined with any modifiers.
protected access modifiers can be found in Reading Reading Note for OCP Java SE7 II by Manning Publication, Chapter 1.
synchronized: this method can not be access by multiple threads concurrently.
synchronizedcan not be applied to class, interface or variables.
native: transfer methods from a library in other programming language such as C or C++.
nativecan not be applied to class, interface or variables.
transient: This variable can not be serialized when its object is serialized.
transientcan not be applied to class, interface or methods.
volatile: This variable can be safely modified by different threads.
volatilecan not be applied to class, interface or methods.
strictfp: make sure the calculations using floating-point are similar in all platforms.
strictfpcan not be applied to variables.
The class with
abstract can not be instantiated. i.e.
Class c = new Class(); can be compiled.
abstract calss may or may not define
concreate class can not define
abstract by default.
abstract method does not have a body. Ususally a
derived class with implement it.
variables can not be defined
final class can not be extented by other classes.
Interface can not be defined as
final variable can only be assigned value once. Note
final int t; means
t has not been assigned value yet, so code like
t = 1; is valid, but no more assignment for
final method can not be overriddden.
static variable can be shared by all objects in its class.
static methods and
static variables can not use any
static methods can use
non-static variables and methods can access
static variables and methods.
static can also be used to define
utility method is usally used to computer and return a value. such as
static int getAverage().
static variables and methods are inherited by derived class. The
static members are not involved in runtime polymorphism.
static members can not be overridden in derived class, but can be redefined.
A top-level class or interface can not be defined as