Read "Streaming Systems" 1&2, Streaming 101 Read "F1, a distributed SQL database that scales" Read "Zanzibar, Google’s Consistent, Global Authorization System" Read "Spanner, Google's Globally-Distributed Database" Read "Designing Data-intensive applications" 12, The Future of Data Systems IOS development with Swift Read "Designing Data-intensive applications" 10&11, Batch and Stream Processing Read "Designing Data-intensive applications" 9, Consistency and Consensus Read "Designing Data-intensive applications" 8, Distributed System Troubles Read "Designing Data-intensive applications" 7, Transactions Read "Designing Data-intensive applications" 6, Partitioning Read "Designing Data-intensive applications" 5, Replication Read "Designing Data-intensive applications" 3&4, Storage, Retrieval, Encoding Read "Designing Data-intensive applications" 1&2, Foundation of Data Systems Three cases of binary search TAMU Operating System 2 Memory Management TAMU Operating System 1 Introduction Overview in cloud computing 2 TAMU Operating System 7 Virtualization TAMU Operating System 6 File System TAMU Operating System 5 I/O and Disk Management TAMU Operating System 4 Synchronization TAMU Operating System 3 Concurrency and Threading TAMU Computer Networks 5 Data Link Layer TAMU Computer Networks 4 Network Layer TAMU Computer Networks 3 Transport Layer TAMU Computer Networks 2 Application Layer TAMU Computer Networks 1 Introduction Overview in distributed systems and cloud computing 1 A well-optimized Union-Find implementation, in Java A heap implementation supporting deletion TAMU Advanced Algorithms 3, Maximum Bandwidth Path (Dijkstra, MST, Linear) TAMU Advanced Algorithms 2, B+ tree and Segment Intersection TAMU Advanced Algorithms 1, BST, 2-3 Tree and Heap TAMU AI, Searching problems Factorization Machine and Field-aware Factorization Machine for CTR prediction TAMU Neural Network 10 Information-Theoretic Models TAMU Neural Network 9 Principal Component Analysis TAMU Neural Network 8 Neurodynamics TAMU Neural Network 7 Self-Organizing Maps TAMU Neural Network 6 Deep Learning Overview TAMU Neural Network 5 Radial-Basis Function Networks TAMU Neural Network 4 Multi-Layer Perceptrons TAMU Neural Network 3 Single-Layer Perceptrons Princeton Algorithms P1W6 Hash Tables & Symbol Table Applications Stanford ML 11 Application Example Photo OCR Stanford ML 10 Large Scale Machine Learning Stanford ML 9 Anomaly Detection and Recommender Systems Stanford ML 8 Clustering & Principal Component Analysis Princeton Algorithms P1W5 Balanced Search Trees TAMU Neural Network 2 Learning Processes TAMU Neural Network 1 Introduction Stanford ML 7 Support Vector Machine Stanford ML 6 Evaluate Algorithms Princeton Algorithms P1W4 Priority Queues and Symbol Tables Stanford ML 5 Neural Networks Learning Princeton Algorithms P1W3 Mergesort and Quicksort Stanford ML 4 Neural Networks Basics Princeton Algorithms P1W2 Stack and Queue, Basic Sorts Stanford ML 3 Classification Problems Stanford ML 2 Multivariate Regression and Normal Equation Princeton Algorithms P1W1 Union and Find Stanford ML 1 Introduction and Parameter Learning

Java basics 1

2017-04-05

Hello World

public class HelloWorld {
    public static void main(String []args) {
        System.out.println("Hello World");
    }
}

run

$ javac HelloWorld.java: compile java source code
$ java HelloWorld: run

basic syntax

  • case sensitive
  • the first letter of class name should be capital
  • the first letter of method name should be lower-case
  • source file name should be the same as the class name
  • main program public static void main(String []args)

java identifier

all classes, variables and methods

  • start with letter, dollar sign $, underline _
  • then followed by any combination of all character
  • keyword cannot be identifier
  • case sensitive
  • legal: age, $salary, _value

java decorator

  • access control: default, public, protected, private
  • non-access: final, abstract, strictfp

java variable

  • local
  • calss (static)
  • non-static

java array

java enumerate

limit the values of the variable

class FreshJuice {
   enum FreshJuiceSize{ SMALL, MEDUIM, LARGE }
   FreshJuiceSize size;
}
 
public class FreshJuiceTest {
   public static void main(String []args){
      FreshJuice juice = new FreshJuice();
      juice.size = FreshJuice. FreshJuiceSize.MEDUIM ;
   }
}

java comment

/* multi
    * 
    */
    
// single
       /* single */

java inherit

super class: the class being inheritted
subclass: the class inheritting

objects and classes

object: an instance of a class
class: the template to creat a set of objects

java class

public class Dog{
  String breed;
  int age;
  String color;
  void barking(){
  }
 
  void hungry(){
  }
 
  void sleeping(){
  }
}

variable:

  • local: in method void{}
  • member: in class, outside method
  • class: in class, outside method, must be static

constructor

the name of this method must be the same with the class
must call a constructor while creating an object
can have multiple constructors

public class Puppy{
    public Puppy(){
    }
 
    public Puppy(String name){
        // single params:name
    }
}

create objects

  • declare an object, including name and type
  • use keyword new to create
  • initialize by calling constructor
public class Puppy{
   public Puppy(String name){
      //single params:name
      System.out.println("dog name : " + name ); 
   }
   public static void main(String []args){
      // new a Puppy
      Puppy myPuppy = new Puppy( "tommy" );
   }
}

access variable and method

ObjectReference = new Constructor();
ObjectReference.variableName;
ObjectReference.MethodName();

class example

public class Puppy{
   int puppyAge;
   public Puppy(String name){
      // single:name
      System.out.println("dog name : " + name ); 
   }
 
   public void setAge( int age ){
       puppyAge = age;
   }
 
   public int getAge( ){
       System.out.println("age : " + puppyAge ); 
       return puppyAge;
   }
 
   public static void main(String []args){
      /* create */
      Puppy myPuppy = new Puppy( "tommy" );
      /* set age */
      myPuppy.setAge( 2 );
      /* get age */
      myPuppy.getAge( );
      /*access */
      System.out.println("value : " + myPuppy.puppyAge ); 
   }
}

source declare regulation

  • only one public class in one source file
  • multiple non-public class in on source file
  • the name of the source file should be the same as the public class - if a class is defined in a package, package should be the first line in the source file
  • if there are import command, it should be between package command and the class defination
  • import and package commands are in effect for all classes

java package

package is used to classify the class and connection

import

locate source files or class

import java.io.*;

example

import java.io.*;
 
public class Employee{
   String name;
   int age;
   String designation;
   double salary;
   // Employee 
   public Employee(String name){
      this.name = name;
   }
   // set age
   public void empAge(int empAge){
      age =  empAge;
   }
   /* set designation*/
   public void empDesignation(String empDesig){
      designation = empDesig;
   }
   /* set salary*/
   public void empSalary(double empSalary){
      salary = empSalary;
   }
   /* print */
   public void printEmployee(){
      System.out.println("name:"+ name );
      System.out.println("age:" + age );
      System.out.println("designation:" + designation );
      System.out.println("salary:" + salary);
   }
}

call this example

import java.io.*;
public class EmployeeTest{
 
   public static void main(String args[]){
      /* new two */
      Employee empOne = new Employee("RUNOOB1");
      Employee empTwo = new Employee("RUNOOB2");
 
      // call
      empOne.empAge(26);
      empOne.empDesignation("senior");
      empOne.empSalary(1000);
      empOne.printEmployee();
 
      empTwo.empAge(21);
      empTwo.empDesignation("fresh");
      empTwo.empSalary(500);
      empTwo.printEmployee();
   }
}

basic data type

type var = value;

type:

  • byte: 8 bits; integer; -128 - 127
  • short: 16 bits; integer; to
  • int: 32 bits; integer; to
  • long: 64 bits; integer; to long a = 100L
  • float: single precision; 32 bits; float f1=243.5f
  • double: double precision; 64 bits; double d1=123.4
  • boolean: true or false; boolean one = true
  • char: unicode; char letter = 'A'

cite type

Site site = new Site('example')

java constant

final double PI = 3.14159;

preceding 0 represents octal; 0x is hexa

escaping character:

char meaning
\n newline (0x0a)
\r return (0x0d)
\f new page(0x0c)
\b backspace (0x08)
\s space (0x20)
\t table
" double quote
' single quote
\\ |
\ddd octal char (ddd)
\uxxxx hexa Unicode char (xxxx)

auto type conversion

char c1 = 'a';
int i1 = c1;

low ————————————-> high
byte,short,char—> int —> long—> float —> double

forced type conversion

int i1 = 123;
byte b = (byte) i1;

java variable

int a, b, c;         // :a、 b、c
int d = 3, e, f = 5; // 
byte z = 22;         // 
String s = "runoob";  // 
double pi = 3.14159; // 
char x = 'x';        // 

example

import java.io.*;
public class Employee{
   // visible to son
   public String name;
   // private
   private double salary;
   // set value
   public Employee (String empName){
      name = empName;
   }
   //set salary
   public void setSalary(double empSal){
      salary = empSal;
   }  
   // print
   public void printEmp(){
      System.out.println("name : " + name );
      System.out.println("salary : " + salary);
   }
 
   public static void main(String args[]){
      Employee empOne = new Employee("RUNOOB");
      empOne.setSalary(1000);
      empOne.printEmp();
   }
}

static variable

When the type is public static final, the static variable name must be all capital.

import java.io.*;
 
public class Employee {
    //salary is static private
    private static double salary;
    // DEPARTMENT is a const
    public static final String DEPARTMENT = "developer";
    public static void main(String args[]){
    salary = 10000;
        System.out.println(DEPARTMENT+"mean:"+salary);
    }
}

java decorator

access control

decorator current class same package son class other package
public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N

default(no keyword)

Default var: in the table
Default method: public

protected

class AudioPlayer {
   protected boolean openSpeaker(Speaker sp) {
      //
   }
}
 
class StreamingAudioPlayer {
   boolean openSpeaker(Speaker sp) {
      // the son class reload the method from father class
   }
}

inherit

  • public method(father) must be public in son
  • protected method(father) must be public or protected in son

non access decorator

  • Static: create class method and variable
  • Final: decorate class, method and variable; final class cannot be inheritted; method cannot be redefined by son class; variable is constant
  • Abstract: abstract class and method
  • Synchronized, volatile: thread programming
public class InstanceCounter {
   private static int numInstances = 0;
   protected static int getCount() {
      return numInstances;
   }
 
   private static void addInstance() {
      numInstances++;
   }
 // constructor: name must be the same as the class
   InstanceCounter() {
      InstanceCounter.addInstance();
   }
 
   public static void main(String[] arguments) {
      System.out.println("Starting with " +
      InstanceCounter.getCount() + " instances");
      for (int i = 0; i < 500; ++i){
         new InstanceCounter();
          }
      System.out.println("Created " +
      InstanceCounter.getCount() + " instances");
   }
}

static

the static variables are independent upon objects; only one copy of it no matter how many objects
the static methods are independent upon objects, cannot use non static variables

final

  • final var: the citation cannot be directed to different object, but data in the final object can be changed
  • final method: can be inheritted by son class, cannnot be changed
  • final class: cannot be inheritted

abstract

use it to expand the class in the future

public abstract class SuperClass{
    abstract void m(); //
}
 
class SubClass extends SuperClass{
     //
      void m(){
          .........
      }
}

Synchronized

visited by one thread at one time

Transient

public transient int limit = 55;   // no
public int b; // yes

volatile

force to read the value from RAM every time. So two different thred will read the same value


Creative Commons License
Melon blog is created by melonskin. This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
© 2016-2019. All rights reserved by melonskin. Powered by Jekyll.