Notes on Java

Some past notes on JAVA

Some past notes on JAVA

JAVA doc

1
2
3
4
5
6
7
/** 
     * @Title Method main 
     * @Description This method handles the main process, demonstrates go() and initialize GUI. 
     * @Param args Unused. 
     * @Return Nothing. 
     * @update 2021-04-10      
     */ 

Multi-threading

  • Putting a Thread to Sleep
    • Can be used to make threas take turns
    • sleep will force thread to leave running state and allow another thread to run
    • Sleeping thread will always go back to runnable state after wake up

Concurrency problem

  • May occur when 2 or more threas have access to same object on the heap
    • race condition -> Possibly Data corruption

Synchronization

  • To solve concurrency problem
    • Making method “atomic”
    • Use keyword “synchronized” to modify a method so that only 1 thread at a time can access it
1
2
3
private synchronized void methodA(){
    // ...
}
  • To protect data, synchronize the methods that act on that data

Life cycle of object 

  • Stack
    • Methods to be run 
    • LIFO 
    • Local Variable lives in Stack 
  • Heap 
    • All alive objects 
    • Objects live in Heap 
    • Instance Variable lives in Heap 

Alive Vs. In scope 

  • Alive = Not destroyed 
  • In scope = Within current running method 

Lifetime: 

Instance variable 

  • Live and die with object  Object 
  • Alive when live references exist 
  • Destoryed when last live reference disappears 
  • Destroyed when containing method completed & out of stack 
  • Destroyed when Object = null;  Reference variable 
  • Destroyed when Reference = null; 
  • Call to super() must be first statement in constructor 
  • A constructor can have either super() or this() 

GUI

Frame settings

1
2
frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); 
frame.setTitle("A Simple Card Game"); 

Event-Handling

1
2
3
class ReplaceListener implements ActionListener { 
public void actionPerformed(ActionEvent event) {}} 
btn_rpcard1.addActionListener(new ReplaceListener()); // Listener for replace button

Interface

Creation

1
2
3
public interface Pet { 
    public abstract void play(); // Interface methods are implicityly public & abstract 
} 

Usage

1
public class Dog extends Canine implements Pet {} 

Modules

GUI

1
2
3
Import javax.swing.*; 
Import java.awt.*; 
Import java.awt.event.*; 

Arraylist

1
Import java.util.ArrayList; 

Serialize

1
Import java.io.*; 

Networking

1
2
Import java.io.*; 
Import java.net.*; 

Exception(s) handling

Finally will always run even if try or catch has return() 

Ducking exception

  • Repeats until some method in the calling chain has try{} catch{} handling 

Catching multiple exceptions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
try {

} catch (Exception1 ex1){

} catch (Exception2 ex2){
    
} catch (Exception3 ex3){
    
} catch (Exception4 ex4){
    
}

Other notes & Concepts

  • Superclass of sub class should have constructor defined 
  • Static variable = same value for all instance of same class   
  • Defind constant = static final double…   
  • super.roam() <- roam() in superclass is called   
  • IS-A vs HAS-A   
  • Abstract class -> To prevent a class from ever being instantiated  
  • Abstract method -> Method has to be overridden (Class with abstract method must be abstract class) 
  • Abstract class can choose to implement (abstract) methods in any ways, but concrete class must implement all    
  • Polymorphism -> To allow reference type as superclass of the actual new object type 
  • Override -> To have specialized operations for same method 
  • Overload -> To have methods of same name but different arguments   
  • Final variable, method, class = uneditable, cannot override, cannot extend 
  • Objects O override 
  • Method X override 
  • Method O Polymorphism 
  • Variable X Polymorphism 
Licensed under CC BY-NC-SA 4.0