常用的包

  • java.lang包 - 该包是Java语言的核心包,并且该包中的所有内容由Java虚拟机自动导入。

如:System类、String类、…

  • java.util包 - 该包是Java语言的工具包,里面提供了大量工具类以及集合类等。

如:Scanner类、Random类、List集合、…

  • java.io包 - 该包是Java语言中的输入输出包,里面提供了大量读写文件相关的类等。

如:FileInputStream类、FileOutputStream类、…

  • java.net包 - 该包是Java语言中的网络包,里面提供了大量网络编程相关的类等。

如:ServerSocket类、Socket类、…

  • java.sql 包 - 该包是Java语言中的数据包,里面提供了大量操作数据库的类和接口等。

如:DriverManager类、Connection接口、…
… …
Java程序员在编程时可以使用大量类库,因此Java编程时需要记的很多,对编程能力本身要求不是
特别的高.

Object类的概述

基本概念

  • java.lang.Object类是Java语言中类层次结构的根类,也就是说任何一个类都是该类的直接或者间接子类。
  • 如果定义一个Java类时没有使用extends关键字声明其父类,则其父类为 java.lang.Object 类。
  • Object类定义了“对象”的基本行为, 被子类默认继承。

    常用方法

    | Object() | 使用无参方式构造对象 | | | —- | —- | —- | | boolean equals(Object
    obj) | 用于判断调用对象是否与参数对象相等。
    该方法默认比较两个对象的地址是否相等,与 == 运算符的结果一致
    若希望比较两个对象的内容,则需要重写该方法。
    若该方法被重写后,则应该重写hashCode方法来保证结果的一致
    性。 | | | int hashCode() | 用于获取调用对象的哈希码值(内存地址的编号)。
    若两个对象调用equals方法相等,则各自调用该方法的结果必须相

    若两个调用对象equals方法不相等,则各自调用该方法的结果应该
    不相同。
    为了使得该方法与equals方法保持一致,需要重写该方法。 | | | String toString() | 用于获取调用对象的字符串形式
    该方法默认返回的字符串为:包名.类名@哈希码值的十六进制
    为了返回更有意义的数据,需要重写该方法
    使用print或println打印引用或字符串拼接引用都会自动调用该方法 | | | Class<?> getClass() | 用于返回调用对象执行时的Class实例,反射机制使用 | |

包装类

包装类的概念

通常情况下基本数据类型的变量不是对象,为了满足万物皆对象的理念就需要对基本数据类型的变量进行打包封装处理变成对象,而负责将这些变量声明为成员变量进行对象化处理的相关类,叫做包装类。
如:
Person p = new Person();
int num = 10;

包装类的分类

包装类 对应的基本类型
java.lang.Byte byte
java.lang.Short short
java.lang.Integer int
java.lang.Long long
java.lang.Float float
java.lang.Double double
java.lang.Boolean boolean
java.lang.Character char

Integer类

  1. public class IntegerTest {
  2. public static void main(String[] args) {
  3. //1.打印Integer类中常用的数值
  4. System.out.println(Integer.MAX_VALUE);
  5. System.out.println(Integer.MIN_VALUE);
  6. System.out.println(Integer.BYTES);
  7. System.out.println(Integer.SIZE);
  8. System.out.println(Integer.TYPE);
  9. System.out.println("-----------------");
  10. //2.使用构造方法来构造Integer类型的对象并打印
  11. Integer it1=new Integer(123);
  12. System.out.println(it1);
  13. Integer it2=new Integer("456");
  14. System.out.println(it2);
  15. //上述已过时,建议使用valueOf方法取代之
  16. //从int类型到Integer类型转换
  17. Integer it3=Integer.valueOf(123);
  18. System.out.println(it3);
  19. //从String类型到Integer类型转换
  20. Integer it4=Integer.valueOf("456");
  21. System.out.println(it4);//自动调用toString方法得到的是String类型
  22. //获取调用对象中的整数数值
  23. //相当于从Integer类型到int类型的转换
  24. int it5 = it4.intValue();
  25. System.out.println(it5);
  26. }
  27. }

装箱和拆箱机制

  • 从int类型到Integer类型转换—-装箱
  • 相当于从Integer类型到int类型的转换—-拆箱
  • 从java5开始增加了自动装箱和自动拆箱的机制
  • 在Integer类的内部提供了自动装箱池技术,将-128到127之间的整数已经装箱完毕,当程序中使用该范围之间的整数时,无需装箱直接取用自动装箱池中的对象即可,从而提高效率。
    1. Integer it6=100;//直接通过赋值运算符实现自动装箱
    2. int it7=it6;//直接通过赋值运算符实现自动拆箱

    考点:

    ```java //考点 /Integer it8=128; Integer it9=128; Integer it10=new Integer(128); Integer it11=new Integer(128); System.out.println(it8==it9); //地址 false System.out.println(it8.equals(it9)); //内容 true System.out.println(it10==it11); //地址 false System.out.println(it10.equals(it11)); //内容 true/ Integer it8=127; Integer it9=127; Integer it10=new Integer(127); Integer it11=new Integer(127); System.out.println(it8==it9); //地址 true—-说明地址一样的 //因为有个自动装箱池 -128~127 System.out.println(it8.equals(it9)); //内容 true System.out.println(it10==it11); //地址 false System.out.println(it10.equals(it11)); //内容 true
  1. ```java
  2. //5.实现静态方法的调用
  3. int i = Integer.parseInt("200");
  4. // int i = Integer.parseInt("200a");编译OK 运行错误 发生NumberFormatException数字格式异常
  5. //将字符串转换为整数
  6. System.out.println(i);
  7. System.out.println(Integer.toString(i));
  8. System.out.println(Integer.toBinaryString(i));
  9. System.out.println(Integer.toHexString(i));
  10. System.out.println(Integer.toOctalString(i));

Double类

  1. public class DoubleTest {
  2. public static void main(String[] args) {
  3. //1.在java5之前装箱和拆箱的实现
  4. // 实现了从double类型转换为Double类 装箱
  5. Double db1=Double.valueOf(3.14);
  6. System.out.println(db1);
  7. // 实现了从Double类转换为double类型 拆箱
  8. double d1=db1.doubleValue();
  9. System.out.println(d1);
  10. //2.从java5开始实现自动装箱和自动拆箱
  11. Double db2=3.14;
  12. double d2=db2;
  13. //3.实现静态方法和成员方法
  14. double d3 = Double.parseDouble("13.14");
  15. System.out.println(d3);
  16. System.out.println(db2.isNaN());//false
  17. Double db3 = Double.valueOf(0 / 0.0);
  18. System.out.println(db3.isNaN());//true;
  19. }
  20. }

Boolean类

  1. package com.lagou.task11;
  2. /**
  3. * @author lijing
  4. * @date 2020/9/27 15:48
  5. * @description
  6. */
  7. public class BooleanTest {
  8. public static void main(String[] args) {
  9. //1.在java5之前采用方法进行长相
  10. // Boolean bo1=new Boolean(true);
  11. Boolean bo1=Boolean.valueOf(true);
  12. System.out.println(bo1);
  13. boolean b1=bo1.booleanValue();
  14. System.out.println(b1);
  15. System.out.println("----------------");
  16. // 2.在java5之后支持自动装箱拆箱
  17. Boolean bo2=false;
  18. boolean b2=bo2;
  19. System.out.println(b2);
  20. System.out.println("=-----------------");
  21. // 33.实现从String类型到boolean类型转换
  22. boolean b3 = Boolean.parseBoolean("true");
  23. String s1 = Boolean.toString(true);
  24. System.out.println(b3);
  25. System.out.println(s1);
  26. }
  27. }

Character类

  1. package com.lagou.task11;
  2. /**
  3. * @author lijing
  4. * @date 2020/9/27 16:23
  5. * @description
  6. */
  7. public class CharacterTest {
  8. public static void main(String[] args) {
  9. //1.在Java5之前调用方法实现装箱拆箱
  10. Character ca1 = Character.valueOf('C');
  11. char c1 = ca1.charValue();
  12. System.out.println(ca1);
  13. System.out.println(c1);
  14. //2.从java5自动装箱
  15. Character ca2='b';
  16. char c2=ca2;
  17. System.out.println(ca2);
  18. System.out.println(c2);
  19. //3.实现字符类型的判断以及转换
  20. System.out.println(Character.isUpperCase(ca2)); //false
  21. System.out.println(Character.isLowerCase(ca2)); //true
  22. System.out.println(Character.isDigit(ca2)); //false
  23. char c = Character.toUpperCase(ca2); //B
  24. System.out.println(c);
  25. char c3 = Character.toLowerCase(c); //b
  26. System.out.println(c3);
  27. }
  28. }

总结:

基本数据类型转换为对应包装类的方式:
调用包装类的构造方法(过时)或静态方法valueOf()—>类名.
对应包装类转换为基本数据类型的方式:
调用包装类的xxxvalue()方法
字符串转换为基本数据类型的方式:
调用parsexxx方式 parseInt

数学处理类(熟悉)

  1. public class MathTest {
  2. public static void main(String[] args) {
  3. System.out.println(Math.max(10,20));
  4. System.out.println(Math.min(10, 20));
  5. System.out.println(Math.pow(2, 3));//8.0
  6. System.out.println(Math.abs(-5));//5
  7. System.out.println(Math.round(3.14));//3
  8. System.out.println(Math.sqrt(16));//7
  9. System.out.println(Math.random());
  10. }
  11. }

BigDecimal类

  1. package com.lagou.task11;
  2. import java.math.BigDecimal;
  3. import java.math.RoundingMode;
  4. /**
  5. * @author lijing
  6. * @date 2020/9/27 16:59
  7. * @description
  8. */
  9. public class BigDecimalTest {
  10. public static void main(String[] args) {
  11. //1.构造BigDecimal类型的对象
  12. BigDecimal bd1 = new BigDecimal("5.2");
  13. BigDecimal bd2 = new BigDecimal("1.3");
  14. System.out.println(bd1.add(bd2));//6.5
  15. System.out.println(bd1.subtract(bd2));//3.9
  16. System.out.println(bd1.multiply(bd2));//6.76
  17. System.out.println(bd1.divide(bd2));//4
  18. System.out.println("----------------");
  19. // 3.实现精确运算
  20. System.out.println(0.1+0.2);
  21. BigDecimal bd3 = new BigDecimal("0.1");
  22. BigDecimal bd4 = new BigDecimal("0.2");
  23. System.out.println(bd3.add(bd4));
  24. // 4.注意事项
  25. BigDecimal bd5 = new BigDecimal("2");
  26. BigDecimal bd6 = new BigDecimal("0.3");
  27. // System.out.println(bd5.divide(bd6));
  28. // Non-terminating decimal expansion; no exact representable decimal result.
  29. System.out.println(bd5.divide(bd6, RoundingMode.HALF_UP));
  30. }
  31. }

BigInteger类

  1. package com.lagou.task11;
  2. import java.math.BigInteger;
  3. /**
  4. * @author lijing
  5. * @date 2020/9/27 17:10
  6. * @description
  7. */
  8. public class BigIntegerTest {
  9. public static void main(String[] args) {
  10. //1.构造两个BigInteger类型并指定初始值
  11. BigInteger bi1=new BigInteger("20");
  12. BigInteger bi2 = new BigInteger("8");
  13. //2.实现加减乘除
  14. System.out.println(bi1.add(bi2));//28
  15. System.out.println(bi1.subtract(bi2));//12
  16. System.out.println(bi1.multiply(bi2));//160
  17. System.out.println(bi1.divide(bi2));//2
  18. System.out.println(bi1.remainder(bi2));//4 获取余数
  19. System.out.println("--------------");
  20. //3.一次性得到商和余数
  21. BigInteger[] arr = bi1.divideAndRemainder(bi2);
  22. for (int i = 0; i < arr.length; i++) {
  23. System.out.println("第"+i+"下标数字为:"+arr[i]);
  24. }
  25. }
  26. }

总结

常用的包(熟悉)
java.lang java.util java.io java.net java.sql
Object(重点)
概念 equals hashCode() toString()
包装类(熟悉)
概念 Integer类 Double类 Boolean类 Character类
数学类(熟悉)
Math类 BigDecimal类 BigInteger类