2012年1月3日 星期二

1.Declarations and Access Control (java sun certification note )

Encapsulation

1.Declarations and Access Control (java sun certification note )


Declarations and Access Control


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 which 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' logic is stored. Methods are
where the real work gets done. They are where algorithms get executed, and
data gets manipulated.

Inheritance
Interfaces
■All interface methods are implicitly public and abstract. In other words,
you do not need to actually type the public or abstract modifiers in the
method declaration, but the method is still always public and abstract.
■ All variables defined in an interface must be public, static, and final—
in other words, interfaces can declare only constants, not instance variables.
Interface methods must not be static.
Because interface methods are abstract, they cannot be marked final,
strictfp, or native. (More on these modifiers later.)
An interface can extend one or more other interfaces.
■ An interface cannot extend anything but another interface.
An interface cannot implement another interface or class.
■ An interface must be declared with the keyword interface.
■ Interface types can be used polymorphically
- The variable in interface but be a constant: ie. public static final int xx;
-example: public static final int xxx; abstract public void bounce();

Complete List of Java Keywords

abstract boolean break byte case catch
char class const continue default do
double else extends fi nal fi nally fl oat
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

JavaBean spec
-JavaBean Property Naming Rules: ie. public void setSize(), public void isBlue();
-JavaBean Listener Naming Rules: ie. addActionListener(), removeActionListener(),=>Listener method names must end with the word "Listener".

 Source File Declaration Rules
-A file can have more than one nonpublic class.
-one file, one public class, one package statement, one set import files.

Class Declarations and Modifiers
-Access modifiers: public, protected, private.
-Non-access modifiers (including strictfp, final, and abstract).
-default access (no modifier) of class can only access within same package. ???How about int explicitly package???
-final abstract class NOT exists
-strictfp define the methods follow IEEE 754 standard rules for floating points.
-Final Classes: the class can't be subclassed.            vs           Final Methods
-Abstract Classes:
  i.e.public abstract class Car extends Vehicle {}
-Interface Classes:

Nonaccess Member Modifiers
-Final Methods
-Final Arguments:
  i.e. public Record getRecord(int fileNumber, final int recordNumber) {}
-Abstract Methods
-Synchronized Methods:The synchronized keyword indicates that a method can be accessed by only one
thread at a time.
  i.e. public synchronized Record retrieveUserInfo(int id) { }
-Native Methods:  The native modifier indicates that a method is implemented in platform-dependent
code, often in C.

Variable Argument Lists (var-args)
 i.e. void doStuff2(char c, int... x) { } // expects first a char,// then 0 to many ints










Instance Variables:
-Cannot be marked synchronized(only for methods, not variable), static(because then they'd become class variables), abstract(only in abstract class),native


Transient Variables:
If you mark an instance variable as transient, you're telling the JVM to skip (ignore) this variable when you attempt to serialize the object containing it.

Volatile Variables:The volatile modifier tells the JVM that a thread accessing the variable must always reconcile its own private copy of the variable with the master copy in memory.

Array ( use HashMap, ArrayList, and TreeSet)
i.e.  Thread[] threads; // Recommended
      Thread threads []; // Legal but less readable
      Thread[5] threads. // ILLEGAL


Assertions
An assertion is a statement containing a boolean expression that is assumed to be true when
the statement is executed.
assert(a > 0); // throws an AssertionError if a <= 0
Declaring Enums:
   i.e. enum CoffeeSize { BIG, HUGE, OVERWHELMING };
         CoffeeSize cs = CoffeeSize.BIG;

???????Declaring Constructors, Methods, and Variables in an enum??????

Thread

by subclassing:
class MyThread extends Thread
{
public void run()
{
System.out.println("Inside run()");
}
}

MyThread mt = new MyThread();
mt.start();

by implementing interface:
class MyRunnable implements Runnable
{
public void run()
{
System.out.println("Inside run()");
}
}

MyRunnable mc = new MyRunnable();
Thread t = new Thread(mc);

NOTES:
abstract class BB extends AA ==> correct
-- abstract can have static methods.
-- only real class should 還債

  • array should be init (remember to init primitive type)
    int [] number;
    number=new int[53];
  • random number:
import java.util.Random;
 Random a=new Random();  //seed not provided=>feed current timestamp??
int pos=a.nextInt(52);
  •    primitive boolean value is : ture/false, not TURE/FLASE, not True/False
  • int [] score ==> can use score.length to get the size;
  • char[] stringArray= "BB".toCharArray()         還原: String a=String.valueOf(stringArray);
  • String operation:
"aaabbpi".replace("pi", "3.14");  // aaabb3.14
"aaabbpi".contains("pi");// true"aaabbpipi".replaceFirst ("pi","3.14); //aaabb3.14pi
"aaa".equalsIgnoreCase("aAa");//true  vs "aaa".equals("aaa");// true

  • use split to count non-space substring:
public int strCount(String str, String sub) {
  str=" "+str+" ";
  String []b=str.split(sub);
  return b.length-1;
}
  • enum 
enum Facts{A,B,C};
for(Facts d: Facts.values())
  System.out.print(d); //output: ABC
  •  if (o instanceof Moof) {} // if o is an instance of Moof
  • arrayList

  • ArrayList stringList = new ArrayList();
  • for(String item: stringList){
System.out.println("retreived element: " + item);}
stringList.set(0,"Item2");