常用的包
- 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类
public class IntegerTest {public static void main(String[] args) {//1.打印Integer类中常用的数值System.out.println(Integer.MAX_VALUE);System.out.println(Integer.MIN_VALUE);System.out.println(Integer.BYTES);System.out.println(Integer.SIZE);System.out.println(Integer.TYPE);System.out.println("-----------------");//2.使用构造方法来构造Integer类型的对象并打印Integer it1=new Integer(123);System.out.println(it1);Integer it2=new Integer("456");System.out.println(it2);//上述已过时,建议使用valueOf方法取代之//从int类型到Integer类型转换Integer it3=Integer.valueOf(123);System.out.println(it3);//从String类型到Integer类型转换Integer it4=Integer.valueOf("456");System.out.println(it4);//自动调用toString方法得到的是String类型//获取调用对象中的整数数值//相当于从Integer类型到int类型的转换int it5 = it4.intValue();System.out.println(it5);}}
装箱和拆箱机制
- 从int类型到Integer类型转换—-装箱
- 相当于从Integer类型到int类型的转换—-拆箱
- 从java5开始增加了自动装箱和自动拆箱的机制
- 在Integer类的内部提供了自动装箱池技术,将-128到127之间的整数已经装箱完毕,当程序中使用该范围之间的整数时,无需装箱直接取用自动装箱池中的对象即可,从而提高效率。
Integer it6=100;//直接通过赋值运算符实现自动装箱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
```java//5.实现静态方法的调用int i = Integer.parseInt("200");// int i = Integer.parseInt("200a");编译OK 运行错误 发生NumberFormatException数字格式异常//将字符串转换为整数System.out.println(i);System.out.println(Integer.toString(i));System.out.println(Integer.toBinaryString(i));System.out.println(Integer.toHexString(i));System.out.println(Integer.toOctalString(i));
Double类
public class DoubleTest {public static void main(String[] args) {//1.在java5之前装箱和拆箱的实现// 实现了从double类型转换为Double类 装箱Double db1=Double.valueOf(3.14);System.out.println(db1);// 实现了从Double类转换为double类型 拆箱double d1=db1.doubleValue();System.out.println(d1);//2.从java5开始实现自动装箱和自动拆箱Double db2=3.14;double d2=db2;//3.实现静态方法和成员方法double d3 = Double.parseDouble("13.14");System.out.println(d3);System.out.println(db2.isNaN());//falseDouble db3 = Double.valueOf(0 / 0.0);System.out.println(db3.isNaN());//true;}}
Boolean类
package com.lagou.task11;/*** @author lijing* @date 2020/9/27 15:48* @description*/public class BooleanTest {public static void main(String[] args) {//1.在java5之前采用方法进行长相// Boolean bo1=new Boolean(true);Boolean bo1=Boolean.valueOf(true);System.out.println(bo1);boolean b1=bo1.booleanValue();System.out.println(b1);System.out.println("----------------");// 2.在java5之后支持自动装箱拆箱Boolean bo2=false;boolean b2=bo2;System.out.println(b2);System.out.println("=-----------------");// 33.实现从String类型到boolean类型转换boolean b3 = Boolean.parseBoolean("true");String s1 = Boolean.toString(true);System.out.println(b3);System.out.println(s1);}}
Character类
package com.lagou.task11;/*** @author lijing* @date 2020/9/27 16:23* @description*/public class CharacterTest {public static void main(String[] args) {//1.在Java5之前调用方法实现装箱拆箱Character ca1 = Character.valueOf('C');char c1 = ca1.charValue();System.out.println(ca1);System.out.println(c1);//2.从java5自动装箱Character ca2='b';char c2=ca2;System.out.println(ca2);System.out.println(c2);//3.实现字符类型的判断以及转换System.out.println(Character.isUpperCase(ca2)); //falseSystem.out.println(Character.isLowerCase(ca2)); //trueSystem.out.println(Character.isDigit(ca2)); //falsechar c = Character.toUpperCase(ca2); //BSystem.out.println(c);char c3 = Character.toLowerCase(c); //bSystem.out.println(c3);}}
总结:
基本数据类型转换为对应包装类的方式:
调用包装类的构造方法(过时)或静态方法valueOf()—>类名.
对应包装类转换为基本数据类型的方式:
调用包装类的xxxvalue()方法
字符串转换为基本数据类型的方式:
调用parsexxx方式 parseInt
数学处理类(熟悉)
public class MathTest {public static void main(String[] args) {System.out.println(Math.max(10,20));System.out.println(Math.min(10, 20));System.out.println(Math.pow(2, 3));//8.0System.out.println(Math.abs(-5));//5System.out.println(Math.round(3.14));//3System.out.println(Math.sqrt(16));//7System.out.println(Math.random());}}
BigDecimal类
package com.lagou.task11;import java.math.BigDecimal;import java.math.RoundingMode;/*** @author lijing* @date 2020/9/27 16:59* @description*/public class BigDecimalTest {public static void main(String[] args) {//1.构造BigDecimal类型的对象BigDecimal bd1 = new BigDecimal("5.2");BigDecimal bd2 = new BigDecimal("1.3");System.out.println(bd1.add(bd2));//6.5System.out.println(bd1.subtract(bd2));//3.9System.out.println(bd1.multiply(bd2));//6.76System.out.println(bd1.divide(bd2));//4System.out.println("----------------");// 3.实现精确运算System.out.println(0.1+0.2);BigDecimal bd3 = new BigDecimal("0.1");BigDecimal bd4 = new BigDecimal("0.2");System.out.println(bd3.add(bd4));// 4.注意事项BigDecimal bd5 = new BigDecimal("2");BigDecimal bd6 = new BigDecimal("0.3");// System.out.println(bd5.divide(bd6));// Non-terminating decimal expansion; no exact representable decimal result.System.out.println(bd5.divide(bd6, RoundingMode.HALF_UP));}}
BigInteger类
package com.lagou.task11;import java.math.BigInteger;/*** @author lijing* @date 2020/9/27 17:10* @description*/public class BigIntegerTest {public static void main(String[] args) {//1.构造两个BigInteger类型并指定初始值BigInteger bi1=new BigInteger("20");BigInteger bi2 = new BigInteger("8");//2.实现加减乘除System.out.println(bi1.add(bi2));//28System.out.println(bi1.subtract(bi2));//12System.out.println(bi1.multiply(bi2));//160System.out.println(bi1.divide(bi2));//2System.out.println(bi1.remainder(bi2));//4 获取余数System.out.println("--------------");//3.一次性得到商和余数BigInteger[] arr = bi1.divideAndRemainder(bi2);for (int i = 0; i < arr.length; i++) {System.out.println("第"+i+"下标数字为:"+arr[i]);}}}
总结
常用的包(熟悉)
java.lang java.util java.io java.net java.sql
Object(重点)
概念 equals hashCode() toString()
包装类(熟悉)
概念 Integer类 Double类 Boolean类 Character类
数学类(熟悉)
Math类 BigDecimal类 BigInteger类
