Java

* 基本介绍

  1. ifobj instanceof String)

1、循环

  1. public class Test3 {
  2. public static void main(String[] args) {
  3. String[] arr_1 = {
  4. "a",
  5. "b",
  6. "c"
  7. };
  8. /**
  9. * 语法
  10. for(元素类型t 元素变量x : 遍历对象obj){
  11. x 是引用
  12. }
  13. */
  14. for (String a : arr_1) {
  15. System.out.println(a);
  16. }
  17. }
  18. }

2、switch

  • switch 语句中的变量类型只能为 byte、short、int、char
  • case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量
  • switch 语句可以包含一个 default 分支,default分支不需要break语句

* 数据类型

1、介绍

文章 :http://www.w3cschool.cc/java/java-basic-datatypes.html

  • 内置(基本)数据类型
  • 引用数据类型 (引用只是对象的别名,引用不等于对象)

2、内置(基本)八种基本数据类型

  • 六种数字类型

    • 四个整数型 (byte-8、short-16、int-32、long-64)

      1. 1) byte 数据类型是 8 , 有符号的 , 以二进制补码表示的整数
      2. 默认值是 0
      3. 最小值是-128(-2^7
      4. 最大值是1272^7-1
      5. byte 变量占用的空间只有 int 类型的四分之一
      6. 2) short 数据类型是 16 位、有符号的以二进制补码表示的整数
      7. 默认值是 0
      8. 最小值是 -32768(-2^15
      9. 最大值是 327672^15 - 1
      10. 一个 short 变量是 int 型变量所占空间的二分之一
      11. 3) int 数据类型是 32 位、有符号的以二进制补码表示的整数
      12. 默认值是 0
      13. 最小值是 -2,147,483,648(-2^31
      14. 最大值是 2,147,485,6472^31 - 1
      15. 一般地整型变量默认为 int 类型
      16. 4) long 数据类型是 64 位、有符号的以二进制补码表示的整数
      17. 默认值是 0L
      18. 最小值是 -9,223,372,036,854,775,808(-2^63
      19. 最大值是 9,223,372,036,854,775,807
      20. 这种类型主要使用在需要比较大整数的系统上
    • 两个浮点型 (float-32、double-64)

      1. 5) float 数据类型是单精度、32位、符合IEEE 754标准的浮点数
      2. 默认值是 0.0f
      3. 浮点数不能用来表示精确的值
      4. float 在储存大型浮点数组的时候可节省内存空间
      5. 6) double 数据类型是双精度、64位、符合IEEE 754标准的浮点数
      6. 默认值是 0.0f
      7. 浮点数的默认类型为 double 类型
      8. double 类型同样不能表示精确的值,如货币
  • 一种布尔型 (boolean)

    1. 7) boolean 数据类型表示一位的信息
    2. 默认值是 false
    3. 只有两个取值 : true false
    4. JAVA 语言的字符类型使用的是 Unicode 字符集(支持全球通用的字符)
    5. C 语言的字符类型使用的是ASCII码字符集 (占用一个字节 )
  • 一种字符类型 (char-16)

    1. 8) char 类型是一个单一的 16 Unicode 字符
    2. 最小值是’\u0000’(即为0
    3. 最大值是’\uffff’(即为65,535
    4. char 数据类型可以储存任何字符
  • 位数排序快速查询

    1. byte 8 (-128 ~ 127)
    2. short 16 (-32768 ~ 32767)
    3. char 16 (字符串)
    4. int 32 (-2,147,483,648 ~ 2,147,485,647)
    5. float 32 ('\u0000',即为 0)~ '\uffff' 即为 65,535)
    6. long 64 (-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807)
    7. double 64
    8. 数自动转换
    9. byte(8)->short(16)->int(32)->long(64)->float(32)->double(64)

3、引用数据类型

  • 引用类型变量由类的构造函数创建,可以使用它们访问所引用的对象。这些变量在声明时被指定为一个特定的类型,比 如 Employee、Pubby 等。变量一旦声明后,类型就不能被改变了
  • 对象(object)、数组(array),都是引用数据类型
  • 所有引用类型的默认值都是 (null)
  • 一个引用变量可以用来引用与任何与之兼容的类型

  • 补充类型

    1. 1) 用来表示非常巨大,接近宇宙级别的大数字的基本数据类型
    2. BigInteger
    3. BigDecimal
    4. 2) 字符类型
    5. String 对象

4、数据类型转换

  1. 低精度<自动>转换为高精度
  2. 高精度必须<强制>转换为低精度
  3. /**
  4. * 数据类型转换
  5. */
  6. public class DataType {
  7. public static void main(String[] args) {
  8. //小的 int 转换为大的 double,自动转换
  9. int n = 100;
  10. double d = n;
  11. System.out.println(d);
  12. //大的 double 转换为小的 int,需要加 (数据类型) 进行强制类型转换
  13. double d2 = 123.656; //不遵循四舍五入
  14. int n2 = (int) d2; //强制转换
  15. System.out.println(n2);
  16. //char 和 int 可以互相转换
  17. char c3 = 'A'; //char(2)
  18. //这里是小的 char(2) 转换为 大的 int(4),是自动的
  19. int n3 = c3; //转换为 ASCII(占用一个字节)
  20. //这里是大的 int(4) 转换为小的 char(2),要强制转换
  21. System.out.println((char)n3); //可以再次互相转换
  22. }
  23. }

5、转义字符

  1. \n 换行 (0x0a)
  2. \r 回车 (0x0d)
  3. \f 换页符(0x0c)
  4. \b 退格 (0x08)
  5. \s 空格 (0x20)
  6. \t 制表符
  7. \" 双引号
  8. \' 单引号
  9. \\ 反斜杠
  10. \ddd 八进制字符 (ddd)
  11. \uxxxx 16进制Unicode字符 (xxxx)
  12. public class DateType2 {
  13. public static void main(String[] args) {
  14. System.out.print("hello world!\n");
  15. System.out.print("hello world!\n");
  16. System.out.print("\"hello world\"\n");
  17. System.out.print("\'hello world\'\n");
  18. }
  19. }

6、进制数制转换

  1. 十进制
  2. 二进制
  3. 八进制
  4. 十六进制
  5. 在线转换工具:http://tool.oschina.net/hexconvert/
  6. public class IntegerTest {
  7. public static void main(String[] args) {
  8. int n = 97;
  9. //转换为 2 进制
  10. String d1 = Integer.toBinaryString(n);
  11. System.out.println(d1);
  12. }
  13. }

* Java 输出、输入

1、System.out

  1. 向标准输出设备输出,向控制台输出(显示器)
  2. System.out.println();
  3. System.out.print();

3、Scanner 获取用户输入

  1. import java.util.Scanner;
  2. public class Input {
  3. public static void main(String[] args) {
  4. int num;
  5. double d;
  6. boolean flag;
  7. String s;
  8. Scanner input = new Scanner(System.in);
  9. System.out.println("请输入一个整数");
  10. num = input.nextInt();
  11. System.out.println("请输入一个小数");
  12. d = input.nextDouble();
  13. System.out.println("请输入一个布尔值");
  14. flag = input.nextBoolean();
  15. System.out.println("请输入一个字符串");
  16. s = input.next();
  17. System.out.println(num);
  18. System.out.println(d);
  19. System.out.println(flag);
  20. System.out.println(s);
  21. }
  22. }

* 算数运算符

1、基本算数运算

  1. public class MathDemo {
  2. public static void main(String[] args) {
  3. //除法
  4. int x1 = 10;
  5. int y1 = 3;
  6. System.out.println(x1/y1); //分子分母都为整数,得到的是整数
  7. int x2 = 10;
  8. double y2 = 3.13;
  9. System.out.println(x2/y2); //分子分母有一个为小数,得到的是小数
  10. //求余数
  11. int x3 = 10;
  12. int y3 = 3;
  13. System.out.println(x3 % y3); //结果为1
  14. }
  15. }

2、位运算

2.1、小技巧

不借助第三方容器,交换 x 和 y 的值

  1. public class MathDemo {
  2. public static void main(String[] args) {
  3. //不借助第三方容器,交换 x 和 y 的值
  4. int x = 10;
  5. int y = 7;
  6. x = x + y; //x = 17
  7. y = x - y; //y = 10
  8. x = x - y; //x = 7
  9. }
  10. }

* 面向对象

1、介绍

关键字

  • this 指向本类
  • super 指向父类
  • final 定义常量
  • static 定义静态资源
  • package 定义包作用域
  • import 引入定义过的包

修饰符

  • 可访问修饰符 : default, public , protected, private
  • 不可访问修饰符 : final, abstract, strictfp

Java变量

  • 局部变量
  • 类变量 (静态变量)
  • 成员变量

源文件声明规则

  • 一个源文件中只能有一个 public 类
  • 一个源文件可以有多个非 public 类
  • 源文件的名称应该和 publi c类的类名保持一致
  • 如果一个类定义在某个包中,那么package语句应该在源文件的首行

2、构造方法

  • 名字与类完全相同,没有返回类型的方法 (void 也是一种返回类型,构造方法不存在返回类型)
  • 构造方法一般的作用是初始化
  • 类中没有构造方法,系统会自动生成一个没有参数的构造方法

  • 关键字

    • this(); 调用本类构造方法
    • super(); 调用父类
  1. public class Test2 {
  2. private int age;
  3. private String sname;
  4. Test2 (int _age,String _sname) {
  5. this.age = _age;
  6. this.sname = _sname;
  7. }
  8. public String getSname() {
  9. String a = this.sname;
  10. return a;
  11. }
  12. public static void main(String[] args) {
  13. //(类型) (引用) (赋值) (-------对象--------)
  14. Test2 obj_2 = new Test2(1,"jason");
  15. System.out.println(obj_2.getSname());
  16. }
  17. }

3、方法重载

  1. public class Test3 {
  2. private String name;
  3. private int age;
  4. Test3 () {
  5. }
  6. //方法重载
  7. Test3 (String name,int age) {
  8. this.name = name;
  9. this.age = age;
  10. }
  11. public static void main(String[] args) {
  12. Test3 test3 = new Test3("Jason",20);
  13. test3.thinking();
  14. }
  15. public void thinking () {
  16. System.out.println(this.name+",年纪:"+this.age);
  17. }
  18. }

4、静态属性方法

  • 静态属于类,而不属于对象
  • 推荐用类名访问
  • <静态的属性和方法,是被所有的对象共享的>
  1. public class Test3 {
  2. private static int num = 0;
  3. Test3 () {
  4. System.out.println("Im,Test3");
  5. }
  6. //方法重载
  7. Test3 (int num) {
  8. this(); //本类构造方法
  9. Test3.num = num;
  10. }
  11. public static void main(String[] args) {
  12. Test3 obj_1 = new Test3();
  13. obj_1.thinking();
  14. System.out.println(Test3.num);
  15. Test3 obj_2 = new Test3();
  16. obj_2.thinking();
  17. System.out.println(Test3.num);
  18. }
  19. public void thinking () {
  20. Test3.num++; //这里做运算被所有的对象共享
  21. }
  22. }

5、静态代码块

  • 静态代码块,会优先执行,在所有的构造函数前,并且不管实例多少次,只会执行一次。
  1. public class Test3 {
  2. private static int num = 0;
  3. /**
  4. * 静态代码块,会优先执行,在所有的构造函数前,并且不管实例多少次,只会执行一次。
  5. */
  6. static {
  7. System.out.println("静态代码块");
  8. }
  9. Test3 () {
  10. System.out.println("Im,Test3");
  11. }
  12. public static void main(String[] args) {
  13. Test3 obj_1 = new Test3();
  14. obj_1.thinking();
  15. System.out.println(Test3.num);
  16. Test3 obj_2 = new Test3();
  17. obj_2.thinking();
  18. System.out.println(Test3.num);
  19. }
  20. public void thinking () {
  21. Test3.num++; //这里做运算被所有的对象共享
  22. }
  23. }

6、抽象方法

  • abstract 抽象 class 的 abstract function,不能有方法体,等价于接口
  1. import java.util.*;
  2. abstract class Test1 {
  3. int x;
  4. abstract void shape(int y);
  5. }

7、Final 修饰符

  • Final 变量能被显式地初始化并且只能初始化一次
  • 被声明为 final 的对象的引用不能指向不同的对象。但是 final 对象里的数据可以被改变。也就是说 final 对象的引用不能改变,但是里面的值可以改变

  • Final 常量 : 通常和 static 修饰符一起使用来创建类常量

    1. public class Test {
    2. final int value = 10;
    3. public static final int BOXWIDTH = 6;
    4. static final String TITLE = "Manager";
    5. public static void main (String [] arg) {
    6. System.out.println(Test.TITLE);
    7. }
    8. }
  • final 方法 :类中的Final方法可以被子类继承,但是不能被子类修改

    1. class Test2 {
    2. public final String bbbbb () {
    3. //方法体
    4. return "aaa";
    5. }
    6. }
  • Final 类 :不能被继承,没有类能够继承 final 类的任何特性

    1. public final class Test {
    2. // 类体
    3. }

8、Synchronized 修饰符

  • Synchronized 关键字声明的方法同一时间只能被一个线程访问。Synchronized 修饰符可以应用于四个访问修饰符
  1. public synchronized void showDetails(){
  2. .......
  3. }

9、Transient 修饰符

  • 序列化的对象包含被 transient 修饰的实例变量时,java 虚拟机 (JVM) 跳过该特定的变量
  • 该修饰符包含在定义变量的语句中,用来预处理类和变量的数据类型
  1. public transient int limit = 55; // will not persist
  2. public int b; // will persist

10、volatile 修饰符

  • java 线程每次访问成员变量时,都强制从内存读取
  • 当成员变量发生变化时,再强制写会内存中
  1. public class MyRunnable implements Runnable {
  2. private volatile boolean active;
  3. public void run() {
  4. active = true;
  5. while (active) // line 1
  6. {
  7. // 代码
  8. }
  9. }
  10. public void stop() {
  11. active = false; // line 2
  12. }
  13. }

11、内部类

  1. public class Inner {
  2. public static void main(String[] args) {
  3. System.out.println("-- 通过外部类成员方位内部类成员 --");
  4. School a = new School();
  5. a.output();
  6. System.out.println("-- 直接方位内部类成员 --");
  7. School.Student b = new School().new Student("金融学院", "李四", 23);
  8. b.output();
  9. }
  10. }
  11. class School {
  12. public String name;
  13. /**
  14. *1.内部类可以随意使用外部类的成员变量(包括私有)而不用生成外部类的对象,这也是内部类的唯一优点
  15. *
  16. *2.必须先有外部类的对象才能生成内部类的对象,因为内部类的作用就是为了访问外部类中的成员变量
  17. */
  18. public class Student {
  19. public String name;
  20. public int age;
  21. public Student (String schoolName,String studentName,int newAge) {
  22. School.this.name = schoolName;
  23. this.name = studentName;
  24. this.age = newAge;
  25. }
  26. public void output() {
  27. System.out.println("学校:" + School.this.name);
  28. System.out.println("姓名:" + this.name);
  29. System.out.println("年龄:" + this.age);
  30. }
  31. }
  32. //直接调用内部类
  33. public void output () {
  34. Student stu = new Student("金融学院","张三",24);
  35. stu.output();
  36. }
  37. }

12、继承 和 接口

  1. //继承、接口
  2. interface Achievemet {
  3. public float average();
  4. }
  5. class Person1 {
  6. public String name;
  7. public int age;
  8. public Person1 (String newName,int newAge) {
  9. this.name = newName;
  10. this.age = newAge;
  11. }
  12. public void introduce () {
  13. System.out.println("你好,我是" + this.name + ",今年" + this.age + " 岁");
  14. }
  15. }
  16. class Student1 extends Person1 implements Achievemet {
  17. public int Chinese;
  18. public int Math;
  19. public int English;
  20. public Student1(String newName, int newAge) {
  21. super(newName, newAge);
  22. }
  23. public void setScore (int c,int m,int e) {
  24. this.Chinese = c;
  25. this.Math = m;
  26. this.English = e;
  27. }
  28. public float average() {
  29. return (this.Chinese + this.Math + this.English) / 3;
  30. }
  31. }
  32. public class JieKou {
  33. public static void main(String[] args) {
  34. Student1 s1 = new Student1("张三",16);
  35. s1.introduce();
  36. s1.setScore(80, 90, 80);
  37. System.out.println("我的平均分是" + s1.average());
  38. }
  39. }

* 内存 栈内存、堆内存

1、介绍

  • 栈内存:数据结构
  • 堆内存:离散结构
  • 内存分配:
    • 基本数据类型 : 在<栈>内存分配
    • 对象 : 在<堆>内存分配

* 包 package

1、包的一些命名规则

  • package 包名;
  • 小写自字母,域名倒写
  • 使用包的优点

    • 防止命名冲突
    • 便于阻止管理
  • import 路径;

2、包的案例

  1. A.java
  2. package cn.com.sina; //这里是三层目录了 cn/com/sina;
  3. public class A {
  4. public String[] data;
  5. public String[] getDataTwo(String[] data) {
  6. this.data = data;
  7. return this.data;
  8. }
  9. }
  10. Index.java
  11. package cn.com.sina.run;
  12. import cn.com.sina.A;
  13. public class Index {
  14. public static void main(String[] args) {
  15. //引入包的方式
  16. A a = new A();
  17. String[] a1 = {"A","B","C"};
  18. System.out.println(a.getDataTwo(a1)[0]);
  19. //直接调用包的方式
  20. cn.com.sina.A b = new cn.com.sina.A();
  21. String[] a2 = {"A","B","C"};
  22. System.out.println(b.getDataTwo(a2)[0]);
  23. }
  24. }

* 泛型方法

1、介绍

  • 泛型方法 : 调用时可以接收不同类型的参数
  • 参数 : 注意类型参数只能代表引用型类型,不能是原始类型(像int,double,char的等)。
  • 返回值类型 : 类型参数能被用来(声明返回值类型),并且能作为泛型方法得到的实际参数类型的占位符。

2、案例

2.1、一般泛型案例

  1. public class GenericMethodTest {
  2. public static < E > void printArray ( E[] inputArray ) {
  3. // 输出数组
  4. for ( E element : inputArray ) {
  5. System.out.printf( "%s ", element );
  6. }
  7. System.out.println();
  8. }
  9. public static void main(String[] args) {
  10. //创建不同类型的数组
  11. Integer [] intArray = {1,2,3,4,5};
  12. Double [] doubleArray = {1.1,2.2,3.3,4.4,5.5};
  13. Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
  14. System.out.println( "Array integerArray contains:" );
  15. GenericMethodTest.printArray( intArray ); // 传递一个整型数组
  16. System.out.println( "\nArray doubleArray contains:" );
  17. GenericMethodTest.printArray( doubleArray ); // 传递一个双精度型数组
  18. System.out.println( "\nArray characterArray contains:" );
  19. GenericMethodTest.printArray( charArray ); // 传递一个字符型型数组
  20. }
  21. }

2.2、有界类型的案例

  • 比如一个操作数字的方法可能只希望接受Number或者Number子类的实例
  1. public class MaximumTest {
  2. // 定义 T 泛型,结构集成 Comparable < T > 把 T 泛型作为返回类型了
  3. public static <T extends Comparable < T >> T maximum(T x,T y,T z) {
  4. T max = x; // 假设x是初始最大值
  5. //compareTo 父泛型类方法
  6. if ( y.compareTo( max ) > 0 ){
  7. max = y; //y 更大
  8. }
  9. if ( z.compareTo( max ) > 0 ){
  10. max = z; // 现在 z 更大
  11. }
  12. return max; // 返回最大对象
  13. }
  14. public static void main(String[] args) {
  15. System.out.printf( "Max of %d, %d and %d is %d\n\n",
  16. 3, 4, 5, maximum( 3, 4, 5 ) );
  17. System.out.printf( "Maxm of %.1f,%.1f and %.1f is %.1f\n\n",
  18. 6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );
  19. System.out.printf( "Max of %s, %s and %s is %s\n","pear",
  20. "apple", "orange", maximum( "pear", "apple", "orange" ) );
  21. }
  22. }

3、泛型类案例

  1. public class Box<T> {
  2. private T t;
  3. //设置泛型变量
  4. public void add(T t) {
  5. this.t = t;
  6. }
  7. //T 泛型 ,作为返回类型
  8. public T get() {
  9. return t;
  10. }
  11. public static void main(String[] args) {
  12. //申明泛型类
  13. Box<Integer> integerBox = new Box<Integer>();
  14. Box<String> stringBox = new Box<String>();
  15. integerBox.add(new Integer(10));
  16. stringBox.add(new String("Hello World"));
  17. System.out.printf("Integer Value :%d\n\n", integerBox.get());
  18. System.out.printf("String Value :%s\n", stringBox.get());
  19. }
  20. }

* 多线程

1、案例

  1. class ThreadB extends Thread {
  2. public int count = 1;
  3. public int num;
  4. public ThreadB (int newNum) {
  5. this.num = newNum;
  6. System.out.println("创建线程" + this.num);
  7. }
  8. public void run () {
  9. while (true) {
  10. System.out.println("线程" + this.num + ":计数" + this.count);
  11. this.count++;
  12. if (this.count == 3) {
  13. break;
  14. }
  15. }
  16. }
  17. public static void main (String[] args) {
  18. Thread a1 = new Thread(new ThreadB(1));
  19. Thread a2 = new Thread(new ThreadB(2));
  20. Thread a3 = new Thread(new ThreadB(3));
  21. a1.start();
  22. a2.start();
  23. a3.start();
  24. System.out.println("主方法 main() 运行结束!");
  25. }
  26. }

* 环境变量

1. SystemProperty

  • 获取 java 运行时的环境变量
  1. 1. 获取 java 自身的环境变量
  2. System.out.println("java_vendor:" + System.getProperty("java.vendor"));
  3. System.out.println("java_vendor_url:"
  4. + System.getProperty("java.vendor.url"));
  5. System.out.println("java_home:" + System.getProperty("java.home"));
  6. System.out.println("java_class_version:"
  7. + System.getProperty("java.class.version"));
  8. System.out.println("java_class_path:"
  9. + System.getProperty("java.class.path"));
  10. System.out.println("os_name:" + System.getProperty("os.name"));
  11. System.out.println("os_arch:" + System.getProperty("os.arch"));
  12. System.out.println("os_version:" + System.getProperty("os.version"));
  13. System.out.println("user_name:" + System.getProperty("user.name"));
  14. System.out.println("user_home:" + System.getProperty("user.home"));
  15. System.out.println("user_dir:" + System.getProperty("user.dir"));
  16. System.out.println("java_vm_specification_version:"
  17. + System.getProperty("java.vm.specification.version"));
  18. System.out.println("java_vm_specification_vendor:"
  19. + System.getProperty("java.vm.specification.vendor"));
  20. System.out.println("java_vm_specification_name:"
  21. + System.getProperty("java.vm.specification.name"));
  22. System.out.println("java_vm_version:"
  23. + System.getProperty("java.vm.version"));
  24. System.out.println("java_vm_vendor:"
  25. + System.getProperty("java.vm.vendor"));
  26. System.out.println("java_vm_name:" + System.getProperty("java.vm.name"));
  27. System.out.println("java_ext_dirs:"
  28. + System.getProperty("java.ext.dirs"));
  29. System.out.println("file_separator:"
  30. + System.getProperty("file.separator"));
  31. System.out.println("path_separator:"
  32. + System.getProperty("path.separator"));
  33. System.out.println("line_separator:"
  34. + System.getProperty("line.separator"));
  35. 2. 获取 java 运行时, 自定义的环境变量
  36. java -Dkey1=value1 -Dkey2=value2 \
  37. -jar ./xxx.jar
  38. 类中读取:
  39. System.out.println(System.getProperty("key1"));
  40. System.out.println(System.getProperty("key2"));

2. SystemEnv

  • 读取 java 运行环境的环境变量, 例如 export 设置的环境变量
  1. export PROJECT_HOME=/opt/xxx
  2. System.out.println(System.getenv("PROJECT_HOME"));