Section 1 to 4 Basics

Source -- Compile -- Bytecode -- Virtual Machine

一个Hello World的例子:

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

保存为MyFirstApp.java, 类名要和文件名保持一致。

编译:javac MyFirstApp.java

生成MyFirstApp.class bytecode文件

运行: java MyFirstApp


java 的条件判断(conditional test)必须是boolean类型,即true或者false。1和0不能作为boolean的条件判断。


数组声明与定义:

String[] pets = {"Fido", "Zeus", "Bin"};

int[] array = new int[2];   数组初始化为0. 这点和单个变量声明不一样!

boolean[] array = new boolean[2]; 初始化为false

String[] array = new String[3]; 初始化为null


Math类
Ee
PIpi
abs绝对值
ceil最小不小于
floor最大不大于
round四舍五入
random0.0~小于1.0
sqrt平方根
cbrt立方根
hypot(x,y)sqrt(x^2+y^2) 过程不溢出
pow(a,b)a^b
exp(a)e^a
expm1(a)e^a-1
scalb(d,s)dx2^s
logln
log10log10
log1p(x)ln(x+1)
addExact加法,溢出异常
subtractExact减法,溢出异常
multiplyExact乘法,溢出异常
negateExact相反数,溢出异常
incrementExact加1,溢出异常
decrementExact减1,溢出异常

生成range Random:

public static int randInt(int min, int max) {
    Random rand = new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;
    return randomNum;
}
nextInt(bound)生成的是0 到 bound, 且 不含bound. bound要大于等于0。

对三个数进行排序:

public static String getKey(int a, int b, int c) {
	int max;
	int mid;
	int min;
	max = Math.max(Math.max(a, b), Math.max(a, c));
	min = Math.min(Math.min(a, b), Math.min(a, c));
	mid = a + b + c - max - min;
	return "" + min + mid + max;
}


以2为底的对数:

public static double log2(int num) {
	return Math.log(num)/Math.log(2);
}

A class is a blueprint for an object.
An object has instance variables (state of object) and methods. An object is an instance. 

Each object can have its own values for the instance variables.


Object lives in Garbage-Collectible Heap. If there is no reference to the object, this object is eligible for garbage collection.


Variable is a container, it holds something. A primitive variable or a reference variable.`

Primitive represents the actual value.

Primitive
booleantrue/false 
char16 bits0 to 65535
byte8 bits-128 to 127 (2^7)
short16 bits-32768 to 32767 (2^15)
int32 bits-2 billions to 2 billions 
long64 bits 
float32 bits 
double64 bits 
 

2^381010^1Ten
2^101,0241,00010^3Thousand
2^201,048,5761,000,00010^6Million
2^301,073,741,8241,000,000,00010^9Billion


Variable Name:

It must start with a letter, underscore or $. You can't start a name with a number.

Object reference. All references for a given JVM will be the same size.

There is actually no object variable. There's only an object reference variable. An object reference variable holds bits that represent a way to access an object.

1. Declare a reference variable. Tells the JVM to allocate memory space for a reference variable of type Dog.

Dog myDog;

2. Create an object and link the object with the reference. Tells the JVM to allocate memory space for a new Dog object on the heap. Assign the new Dog to the reference variable.

myDog = new Dog();

An array is an object, new an array.

A method uses parameters. A caller passes arguments.


Java is pass-by-value, which means pass-by-copy.

Variable types can be Implicitly promoted or explicit cast. Size matters for primitive. You can't put a larger type into a small type.

Apple A = new Apple();  是A refer到了一个Apple上。

Apple B = A; 是B refer到了A refer的Apple上。此时可以通过dot来修改A refer的Object。

B = new Apple(); 是B refer 到了一个新的Apple上。此时对A没有影响,即A不会指向B的Object。B与A就没关系了。

将一个Variable和一个Object链接,Variable就可以访问这个Object,作为这个Object的代表。


Getters and Setters 是用来保护instance variables的。在Setters的函数里可以判断是否是有效的输入值(valid)。如果不是,则可以拒绝修改。


Instance variables always get a default value.

Integers0
Floating points0.0
Booleansfalse
Referencenull

Local variables are declared within a method without default values, they must be initialized before use. 但是数组会自动初始化。


Compare two primitives if they are the same, use ==.

Compare two reference variables if they refer to a single object on the heap, use ==.

Compare two objects if they are equal, use .equals().

== is used to compare the bit pattern in two variables. Extra 0 doesn't matter. 数组的.equals()和==等价。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值