生活发生的一切,才会促使着我继续前行,今天继续更新哦,看书中的代码练习。
例子1 引入net.mindview.util.Print.* ,方便打印结果。
package com.date0529;
import java.util.Date;
//需要引入类库,并且配置ClassPath环境变量中添加该代码目录树的根目录
import static net.mindview.util.Print.*;
public class Test_0529_Demo01 {
public static void main(String[] args) {
print("Hello. it's: ");
print(new Date());
}
}
例子2 运算符号的优先级
public class Test_0529_Demo02 {
public static void main(String[] args){
int x=1,y=2,z=3;
int a=x+y-2/2+z;//(1)
int b=x+(y-2)/(2+z);//(2)
System.out.println("a="+a+" b="+b);
}
}
运算结果如下:
例子3 对象的赋值 t1赋值,之前的t1对对象的引用已经被垃圾回收清除掉,现在t1,t2指向的是相同的对象。如果要保留t1,t2独立,可以t1.level = t2.level 这样赋值
public class Test_0529_Demo03 {
public static void main(String[] args){
Tank t1 = new Tank();
Tank t2 = new Tank();
t1.level = 9;
t2.level = 51;
System.out.println("1: t1.level "+t1.level+" t2.level "+t2.level );
t1 = t2;
System.out.println("2: t1.level "+t1.level+" t2.level "+t2.level );
t1.level = 23;
System.out.println("3: t1.level "+t1.level+" t2.level "+t2.level );
t2.level = 100;
System.out.println("3: t1.level "+t1.level+" t2.level "+t2.level );
}
}
运算结果如下:
例子4 方法调用中的别名
package com.date0529;
class Letter{
char c;
}
public class Test_0529_Demo04 {
/**
* 方法调用的别名问题,这里f()方法实际上只是传递了一个引用
* @param y
*/
static void f(Letter y){
y.c = 'z';
}
public static void main(String[] args){
Letter x = new Letter();
x.c = 'a';
System.out.println("1: x.c: " +x.c );
f(x);
System.out.println("2: x.c: " +x.c );
}
}
运算结果如下:
例子5 算数操作符 自增和自减要好好理解下,自增在前则是先执行加同理在后后执行加
package com.date0529;
import java.util.Random;
/**
* 算数操作符
*/
public class Test_0529_Demo05 {
public static void main(String[] args){
//创造一个随机数
Random rand = new Random(47);
int i, j, k;
//选择一个1到100之间的数
j = rand.nextInt(100) + 1;
System.out.println("j: " + j);
k = rand.nextInt(100) + 1;
System.out.println("k: " + k);
i = j + k;
System.out.println("j+k: " + i);
i = j - k;
System.out.println("j-k: " + i);
i = j / k;
System.out.println("j/k: " + i);
i = j * k;
System.out.println("j*k: " + i);
i = j % k;
System.out.println("j%k: " + i);
j %= k;
System.out.println("j %= k: " + j);
float u, v, w;
v = rand.nextFloat();
System.out.println("v: " + v);
w = rand.nextFloat();
System.out.println("w: " + w);
u = rand.nextFloat();
System.out.println("u: " + u);
u = v + w;
System.out.println("v+w: " + u);
u = v - w;
System.out.println("v-w: " + u);
u = v / w;
System.out.println("v/w: " + u);
u = v * w;
System.out.println("v*w: "+ u);
u += v;
System.out.println("u +=: " + u);
u -= v;
System.out.println("u -=: " + u);
u *= v;
System.out.println("u *=: " + u);
u /= v;
System.out.println("u /=: " + u);
int m = 1;
System.out.println("m: " + m);
System.out.println("m++: "+ m++);
System.out.println("++m: " + ++m);
System.out.println("--m: " + --m);
System.out.println("m--: " + m--);
System.out.println("m: " + m);
}
}
运算结果如下: