//TestUsb.java publicclassTestUsb{ psvm(String[] args){ /* Usb usb = new Mouse();//创建接口类型变量 usb.service();//成功连接电脑,Mouse开始工作 */
/* //局部内部类 class Fan implements Usb{ @Override public void service(){ sout("成功连接电脑,Fan开始工作") } } //使用局部内部类创建对象 Usb usb = new Fan(); usb.service(); //成功连接电脑,fan开始工作 */
//上面使用局部内部类,用完之后就不再用了,没必要定义一个带名字的类 //使用匿名内部类优化(相当于创建了一个局部内部类) //Usb usb = new Usb(); //编译报错 interface 不能实例化 Usb usb = new Usb(){ //Usb() 可以是 interface, 抽象类, 父类 @Override publicvoidservice(){ sout("成功连接电脑,xxx开始工作") } }; usb.service(); //成功连接电脑,xxx开始工作 } }
//在Student.java内重写toString() classStudent{ ... // 重写 alt + enter + s @override public String toString(){ return"Student [name = " + name + ", age = " + age + "]"; } ... }
但我觉得这两个方法判断的效果是不是不相同啊。 举个例子,Object——Person——Student这样的继承关系 Object obj = new Student(); Person person = new Person(); System.out.println(person.equals(obj)); 如果用if(obj instanceof Person),那应该是True,因为obj是Student的实例,与Person有继承关系。
# li @ evpower in ~/humble/blog/source/_posts/java on git:master x [18:27:56] $ cat Hello.java public class Hello{ public static void main(String[] args){ System.out.print("Hello World!"); } }
# li @ evpower in ~/humble/blog/source/_posts/java on git:master x [18:28:02] $ javac Hello.java
# li @ evpower in ~/humble/blog/source/_posts/java on git:master x [18:28:12] $ ls Hello.class Hello.java
# li @ evpower in ~/humble/blog/source/_posts/java on git:master x [18:28:13] $ java Hello Hello World!% # li @ evpower in ~/humble/blog/source/_posts/java on git:master x [18:28:18] $
Java数据类型分类
基本数据类型primitive type
1 2 3 4 5 6 7 8
byte num1 = 20; short num2 = 20; int num4 = 20; long num5 = 20L; //可以用L或l,但l容易跟1弄混 float num6 = 50.1F; double num7 = 3.141592653; char num3 = 'a'; boolean flag = true; //false
引用数据类型reference type(类,接口,数组)
1
String name = "Lion";
进制
1 2 3
0b10010010 //二进制(0b) 050 //八进制(0) 0x1A //十六进制(0x)
避免使用浮点数比较,建议用类(BigDecimal)
1 2 3 4 5 6 7
float f = 0.1F; double d = 1.0/10; if(f == d) //false
//byte,short,char -> int -> long -> float -> double //低->高 (高->低:强制类型转换 低->高:自动类型转换) int i = 128; byte b = (byte)i; //-128 强制转换 内存溢出 double d = i; //128 自动转换 int j = (int)23.7; //23 int k = (int)-45.89f; //-45 int money = 10_0000_0000; //1000000000, JDK7新特性,可以用下划线分割数字 long total = money * 12; //int型溢出,改为 long total = (long)money * 12L;
注意:
不能对boolean进行转换
不能把对象类型转成不相干的类型(把猪转人-no,把男转女-yes)
把高容量转低容量时,强制转换
转换时可能存在内存溢出或精度问题
变量
类变量:(static)初始默认值是null
实例变量:从属于对象,若不初始化,默认值是 0/0.0
局部变量:必须声明和初始化
1 2 3 4 5 6 7 8
public class Variable{ static int allClicks = 0; //类变量 String str="hello world"; //实例变量
public void method(){ int i=0; //局部变量 } }
常量
1 2 3 4 5
final double PI=3.14; //常量名建议使用全大写
/* 修饰符关键字不分先后顺序 */ static final double PI=3.14; final static double PI=3.14;
变量的命名规范
类成员变量:首字母小写驼峰命名法
局部变量:首字母小写驼峰命名法
常量:大写字母和下划线
类名:首字母大写驼峰命名法
方法名:首字母小写驼峰命名法
运算符
算数运算符+,-,*,/,%,++,--
赋值运算符=
关系运算符>,<,>=,<=,==,!=,instanceof
逻辑运算符&&,||,!
位运算符&,|,^,~,>>,<<,>>>
条件运算符? :
扩展赋值运算符+=,-=,*=,/=
IDEAtips:Ctrl+d是复制当前行到下一行
短路运算
1 2
int c=5; boolean d = (c<4)&&(c++<4); //d是false,c是5
public class Hello { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println("arg[" + "] is " + args[i]); } } }
public class Hello { public static void main(String[] args) { Person person = new Person(); System.out.println(person.name); //未定义所以打印 null Hello.change(person); //传了对象(引用传递,址传递) System.out.println(person.name); //打印出 world }
public static void change(Person person){ person.name = "world"; } }
public class Application { public static void main(String[] args) { //Person humble = new Person(); //Person()实际上是构造方法 Person humble = new Person("Humble"); System.out.println(humble.name); } }
public class Hello { public static void main(String[] args) { //new会在堆的new分配区申请一片内存并返回地址 //person变量保存在栈,它的值是一个指向堆的地址 Person person = new Person(); System.out.println(person.name); //未定义所以打印 null Hello.change(person); //传了对象(引用传递,址传递) System.out.println(person.name); //打印出 world }
public class Application { public static void main(String[] args) { Student s1 = new Student(); System.out.println(s1.money); //1000000000 s1.say(); //I am a Person } }
IDEAtips:Ctrl+h可以打开继承树
java中所有的类都默认直接或间接继承Object类
super
super()调用父类的构造方法,必须在构造方法的第一行
super只能出现在’子类’的方法或构造方法中
super和this不能同时调用构造方法
用super调用父类
1 2 3 4 5 6 7
/* Filename: Person.java */ package com.oop.demo;
public class Person { //如果修饰改为private,那么子类就无法用super直接调用 protected String name = "person1"; public void print(){ System.out.println("Person.print"); } }
public class Person { public Person(){ System.out.println("Person.Person"); } public Person(String name){ System.out.println("Person.Person:"+name); } }
public class Student extends Person { public Student() { //隐藏调用,默认会先调用父类构造器super()。 //如果想显式调用super()则必须放在子类构造器第一行。 //若父类构造器有参,那么调用super()要带参,除非父类构造器重载 //super(); super("hello"); System.out.println("Student.Student"); } }
public class B { public /*static*/ void test(){ System.out.println("B.test"); } }
1 2 3 4 5 6 7 8 9 10
/* Filename: A.java */ package com.oop.demo;
public class A extends B { @Override //@表示注解,是有功能的注释。Override表示重写 public void test() { //super.test(); //默认是调用父类的方法,现在重写 System.out.println("A.test"); } }
public class Application { public static void main(String[] args) { Student s1 = new Student(); Person s2 = new Student(); Object s3 = new Student(); //String s4 = new Student(); //报错,引用只能是同类或父类
public class Application { public static void main(String[] args) { Person p = new Person(); //Student conversion2s1 = p; //编译报错,因为堆内的对象为Person(父),不能用栈内Student(子)引用去指向它,需要强转 //Student conversion2s2 = (Student) p; //编译通过但运行报错,原因同上
Student s = new Student(); Person conversion2p = s; //子类转父类 //conversion2p.go(); //编译报错,因为栈内引用Person没有go方法,也就是丢失了Student(子)的方法go Student reset2s = (Student)conversion2p; reset2s.go(); //打印Student.go,证明转为父类会丢失子类方法go,再转回子类又恢复了方法go } }
static
属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Filename:Student.java */ package com.oop.demo;
public class Student { private static int age; private double score;
public static void main(String[] args) { Student s1 = new Student();
/* Filename:Application.java */ package com.humble.oop; public class Application { public static void main(String[] args) { System.out.println(11/0); //Exception in thread "main" java.lang.ArithmeticException: / by zero } }
public class Test2 { public static void main(String[] args) { try { new Test2().test(1,0); } catch (ArithmeticException e) { e.printStackTrace(); } }
//注意 throw 和 throws 两关键字 public void test(int a, int b) throws ArithmeticException{ if (b == 0) { throw new ArithmeticException(); //throw 一般是在方法内处理不了,于是主动往更高级抛出异常 } System.out.println(a / b); } }
public class Test { static void test(int a) throws MyException { System.out.println("a = " + a); if (a > 10){ throw new MyException(a);//抛出 } System.out.println("Test.test"); }
./Configure linux-aarch64 --cross-compile-prefix=aarch64-himix100-linux- --prefix=/tmp/result && make && make install #./Configure linux-armv4 --cross-compile-prefix=arm-himix200-linux- --prefix=/tmp/result && make && make install
若报错
1 2 3 4
crypto/aes/aes-x86_64.s: Assembler messages: crypto/aes/aes-x86_64.s:2: Error: unrecognized symbol type"" crypto/aes/aes-x86_64.s:3: Error: alignment too large: 15 assumed crypto/aes/aes-x86_64.s:5: Error: bad instruction `xorl 0(%r15),%eax'
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [18:32:28] $ gcc -Wall creat1.c -o creat1 -lpthread creat1.c: In function ‘func’: creat1.c:18:18: warning: passing argument 1 of ‘pthread_exit’ makes pointer from integer without a cast [-Wint-conversion] 18 | pthread_exit(3); | ^ | | | int In file included from creat1.c:12: /usr/include/pthread.h:207:33: note: expected ‘void *’ but argument is of type ‘int’ 207 | extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__)); | ~~~~~~^~~~~~~~ creat1.c: In function ‘main’: creat1.c:44:23: warning: passing argument 2 of ‘pthread_join’ from incompatible pointer type [-Wincompatible-pointer-types] 44 | pthread_join(tid, &retval); //类似进程的wati() | ^~~~~~~ | | | int * In file included from creat1.c:12: /usr/include/pthread.h:215:49: note: expected ‘void **’ but argument is of type ‘int *’ 215 | extern int pthread_join (pthread_t __th, void **__thread_return); | ~~~~~~~^~~~~~~~~~~~~~~
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [18:40:25] $ ./creat1 begin main func 2 end 3
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [18:40:27] $
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:07:58] $ gcc -Wall cleanup.c -o cleanup -lpthread #demo0
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:08:00] $ ./cleanup begin pthread func is working push over cleanup3 end
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:08:04] $ vi cleanup.c
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:08:13] $ gcc -Wall cleanup.c -o cleanup -lpthread #demo1
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:08:24] $ ./cleanup begin pthread func is working push over cleanup3 cleanup2 cleanup1 end
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:08:26] $
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [21:08:38] $ gcc -Wall primer0.c -o primer0 -lpthread primer0.c: In function ‘main’: primer0.c:25:66: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] 25 | err = pthread_create(tid + (i - LEFT), NULL, thr_primer, (void *)i); //如果把i的地址则会有竞争 | ^ primer0.c: In function ‘thr_primer’: primer0.c:45:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 45 | i = (int)p; | ^
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [21:08:40] $ ./primer0 30000059 is a primer 30000071 is a primer 30000083 is a primer 30000023 is a primer 30000137 is a primer 30000001 is a primer 30000041 is a primer 30000199 is a primer 30000193 is a primer 30000037 is a primer 30000149 is a primer 30000049 is a primer 30000109 is a primer 30000169 is a primer 30000163 is a primer 30000167 is a primer 30000133 is a primer 30000079 is a primer
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [21:09:12] $
# li @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [11:20:10] C:142 $ gcc -Wall abcd.c -o abcd -lpthread abcd.c: In function ‘thr_abcd’: abcd.c:29:13: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 29 | int n = (int)p; | ^ abcd.c:30:19: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 30 | int c = 'a' + (int)p; | ^ abcd.c: In function ‘main’: abcd.c:49:55: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] 49 | err = pthread_create(tid + i, NULL, thr_abcd, (void *)i); | ^
# li @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [11:21:54] $ ./abcd abcdabcdabcdabcdabcdabcdabcda... [1] 20197 alarm ./abcd
# li @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [15:10:33] $ gcc -Wall primer0_pool.c -o primer0_pool -lpthread primer0_pool.c: In function ‘main’: primer0_pool.c:30:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] 30 | err = pthread_create(tid + i, NULL, thr_primer, (void *)i); | ^ primer0_pool.c: In function ‘thr_primer’: primer0_pool.c:115:45: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 115 | printf("[%d] %d is a primer\n", (int)p, i); | ^
# li @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [15:17:54] $ ./primer0_pool [0] 30000001 is a primer [1] 30000023 is a primer [3] 30000041 is a primer [2] 30000037 is a primer [4] 30000049 is a primer [0] 30000059 is a primer [4] 30000083 is a primer [3] 30000071 is a primer [2] 30000109 is a primer [1] 30000079 is a primer [4] 30000137 is a primer [3] 30000149 is a primer [0] 30000133 is a primer [4] 30000169 is a primer [3] 30000193 is a primer [2] 30000163 is a primer [1] 30000167 is a primer [0] 30000199 is a primer
# li @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [15:17:56] $
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix/mytbf_mt on git:master x [19:17:09] C:2 $ tree . ├── main.c ├── Makefile ├── mytbf.c └── mytbf.h
0 directories, 4 files
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix/mytbf_mt on git:master x [19:17:10] $ make cc -Wall -c -o main.o main.c cc -Wall -c -o mytbf.o mytbf.c gcc -Wall main.o mytbf.o -o mytbf -lpthread
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix/mytbf_mt on git:master x [19:17:16] $ ./mytbf /etc/services # Network services, Internet style # # Note that i^C
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix/mytbf_mt on git:master x [19:17:29] C:130 $
上面的程序经过测试,发现 CPU 正在满负荷工作,说明程序中出现了忙等。就是 mytbf_fetchtoken() 函数获得锁的时候采用了忙等的方式。异步程序有两种处理方式,一种是通知法,一种是查询法。我们这里用的就是查询法,下面改成通知法实现。