Reading Note for OCP Java SE7 I by Manning Publication, Chapter 1

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 statement should be the first statement in a class.
  • Package statementshould be defined exactly once in a class.

Format of import: import package_name.class_name. import statement can not be placed before a package statement.

java
1
2
3
4
5
import testPackage.testClass;
class test{
testClass t;
//testClass can be used directly here because of import.
}

/*...*/ and /*...*...*...*/ are all acceptable in java comments.

  • The properties of an object are implemented using variables.
  • The behavior is implemented using methods.
  • The state of a class is defined using attributes or instance varoables.

A class is a design from which an object can be created.

An 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:

  • public
  • static
  • void
  • name as main
  • Takes array of string parameter:
    java
    1
    2
    3
    4
    5
    public static void main(String... args) // Valid
    public static void main(String args...) //Compile error
    public static void main(String[] args) // Valid
    public static void main(String args[]) //Valid
    static 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 command-line values:

Markdown
1
2
//Passing two parameter to main method in command line
C:\>java Class_Name Parameter1 Parameter2

Java packages

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(.java file).

Importing more classes does not increase the size of your own class. You can’t import classes with same name from different package.

java
1
2
3
import java.util.Date;
import java.sql.Date;
//This will not compile

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 import and static can not be reversed.

1
2
package test;
import static com.package_name.class_name; // import and static can not be reversed.

Java access modifiers

A top-level class is a class that is not defined within any other class. A top-level class can be defined only using the public or default access modifiers.

Local variables and methods can not be defined with any modifiers.

Details about public, private, default, protected access modifiers can be found in Reading Reading Note for OCP Java SE7 II by Manning Publication, Chapter 1.

Nonaccess Modifiers

  • synchronized: this method can not be access by multiple threads concurrently. synchronized can not be applied to class, interface or variables.

  • native: transfer methods from a library in other programming language such as C or C++. native can not be applied to class, interface or variables.

  • transient: This variable can not be serialized when its object is serialized. transient can not be applied to class, interface or methods.

  • volatile: This variable can be safely modified by different threads. volatile can not be applied to class, interface or methods.

  • strictfp: make sure the calculations using floating-point are similar in all platforms.strictfp can not be applied to variables.

  • abstract:

The class with abstract can not be instantiated. i.e. Class c = new Class(); can be compiled.

abstract calss may or may not define abstract method. concreate class can not define abstract method.

Interface is abstract by default.

abstract method does not have a body. Ususally a derived class with implement it.

variables can not be defined abstract.

  • final:

final class can not be extented by other classes.

Interface can not be defined as final.

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

final method can not be overriddden.

  • static:

static variable can be shared by all objects in its class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Test{
static int a;
}
class Test1{
Test t1 = new Test();
Test t2 = new Test();
t1.a = 1;
t2.a = 2;
/*
Now, t1.a = t2.a = Test.a = 2,
because all of them refer to the same static attribute a.
*/
}

static methods and static variables can not use any instance variable. static methods can use static variable. non-static variables and methods can access static variables and methods.

1
2
3
4
5
6
7
8
class Test{
static int a;
int b;
static void testNum{
return a; //can not use b here
}
}

static can also be used to define utility method. utility method is usally used to computer and return a value. such as static int getAverage().

The non-private 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 static.