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