一、包的结构和功能介绍
Java是一门面向对象的语言,sun公司提供基于面向对象的帮助文档(API Application Program Interface) ,并针对不同的版本生成的API
API中根据不同的功能分如下包 (package)
-
java.applet.* : java的小应用程序
-
java.awt.* 和 java.swing.* : java的图形用户界面(开发单机版的小游戏)
-
java.lang.* : java的语言包
-
java.util.* : java的工具类包、集合框架包
-
java.io.* : java文件读写包(Input 、Output)
-
java.net.* : java的网络编程包(Socket机制相关,URL)
-
java.sql./ javax.sql. : java的数据库操作
-
java.lang.reflect.* 反射相关包
一、Java的lang包
1、包装类
基本数据类型:byte、short、int、long、float、double、char、boolean
包装类型:Byte、Short、Interger、Long、Float、Double、Character、Boolean
问题1: 基本数据类型、包装类以及字符串的相互转换
public static void main(String[] args) {
// 1、byte 的包装类 Byte
// 创建包装类的对象
byte b=123;
Byte obj1 = new Byte(b);
//1、 包装类 转字符串 包装类对象.toString()
String s1 = obj1.toString();
//2、字符串转包装类 new 包装类(s) 或者 包装类.valueOf(s)
String s2="100";
Byte obj2 = new Byte(s2);
// 或者
Byte obj3 = Byte.valueOf(s2);
//3 获取包装类的数值,包装类转基本数据类型 Byte - > byte
// 包装类.valueOf(基本数据类型) 或者 byteValue()
byte b2 = obj2; // 包装类可以直接复制给基本数据类型 ,这个过程 “拆箱”过程
byte b3 = Byte.valueOf(obj2);
// 4、字符串转 基本类型 包装类.paseByte(s)
byte b4 = Byte.parseByte(s2);
byte b5=122;
String s5 = new Byte(b5).toString();
}
问题2: 数据类型的装箱和拆箱
装箱: 将基本数据类型自动转换成 它的包装类,可以使用包装类的方法和属性
拆箱: 将包装类型 自动转成 对应的基本数据类型。
// 自动装箱: 100自动转成 Integer
Integer num1 = 100;
// 自动拆箱: Integer 自动转成 int
int num2 = num1;
面试题:
public static void main(String[] args) {
// 包装类
// 自动装箱: 100自动转成 Integer
Integer num1 = 100;
// 自动拆箱: Integer 自动转成 int
int num2 = num1;
Integer n1 =100;
Integer n2 =100;
System.out.println(n1.equals(n2)); //true
System.out.println(n1 == n2);// 应该true 他们同时指向常量池100
Integer n3 = 150; // 等价于 Integer n3 = new Integer(150);
Integer n4 = 150; // 等价于 Integer n4 = new Integer(150);
System.out.println(n3.equals(n4));//true
System.out.println(n3 == n4);//false
Integer n6 = new Integer(100);// 一定会创建新对象
System.out.println(n6 == n1); // false
}
//结论
//对于 -128 <=Integer的值 <=127 之间(byte范围),
// 装箱时不会创建新对象 而是直接引用 常量池中的值
// 如果超出byte 的返回,则自动创建新对象,各自指向新对象的内存
2、Object类
Object类是lang包提供的 ,对于lang包的类不需要import,所以 Object类无处不在,你不需要自己创建
常用方法
a、getClass: 返回该对象的类型 任何类都有它的类型
b、equals : Java中所有的equals 方式都是重写Object的方法
原生的equals 比较的是 对象的地址 ,我们通常说的 equals比较两个对象的值是因为几乎所有的数据类型(包装类,String)都重写了equals 方法的
c、 hashCode() : 返回该都对象的hash值
d:finalize() 资源回收调用该方法, 当对象地址不在被引用时,会被GC回收 并调用该方法 Object obj = null ;
e:toString() : 返回该对象的字符串表现形式 (通常会被子类重写)
wait():线程等待
notify():唤醒其中一个等待的线程
notifyAll:唤醒所有等待中的线程
总结: 对象之间的比较 ,通常是比较属性值,如果属性值相等,我们可以认为是同一个对象,此时需要重写 equals 和hashcode方法。
为什么要重写HashCode呢?
回答: 在JMV中如果HashCode不相等,一定不能认为是同一个对象
3、System类:
public static void main(String[] args) {
// System 属于系统类
// System.out; // 获取控制台的打印流
// 设置JVM运行时 系统参数
System.setProperty("encoding","UTF-8");
System.out.println("获取:"+System.getProperty("encoding"));
// 时间从 1970-01-01
System.out.println("获取当前系统的时间毫秒数:"+ System.currentTimeMillis());
System.exit(0); // 0 : 表示JVM正常退出 -1 表示非正常退出
}
4、字符串类
String类的特点:
它是一个不可变字符串 ,它的值创建后不能被改变。
String的构造器
// 创建字符串对象
String s1="abc";
String s2 = new String("abc");
//通过字符数组构建
char [] chars = {'a','b','c'};
String s3 = new String(chars); // 或指定长度构建字符串
String s4 = new String(chars,0,2);
//或根据字节数组构建
byte [] byts = {97,98,99};
String s5 = new String(byts);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
// 字符串是一个不可变的对象
// class类被JVM装载时,会分配一个存放字符串的常量池(String Pool)
// 在类加载时 先检查常量池中是否有“abc”常量,如果有,直接将ss1指向该常量
// 如果没有,则创建常量abc
// 创建2个对象
String ss1 = "abc";
// abc常量不能改变, 则再创建 abcd的常量,由ss1重新指向
ss1+="d";
// 创建3个对象
String ss2 ="abcd"; // abcd
String ss3 = "aaa"; // aaa
ss2 += ss3; // abcdaaa 重新创建abcdaaa并由ss2重新指向
String a1="abc";
String b1="abc"; // 两个地址同时指向一个常量 “abc”
System.out.println(a1==b1); // true
System.out.println(a1.equals(b1));
String c1=new String("abc");// 堆内存中 对abc包装后的地址
System.out.println(a1==c1); // false
System.out.println(a1.equals(c1));//true
字符串类常用方法
-
将此字符串与指定对象进行比较:
public boolean equals (Object anObject)
-
将此字符串与指定对象进行比较,忽略大小写:
public boolean equalsIgnoreCase (String anotherString)
获取功能的方法
-
返回字符串的长度:
public int length()
-
将指定的字符串连接到该字符串的末尾:
public String concat (String str)
-
返回指定索引处的char值:
public char charAt (int index)
-
返回指定字符串第一次出现在该字符串内的索引:
public int indexOf(String str)
-
返回一个子字符串,从beginIndex开始截取字符串到字符串结尾:
public String substring (int beginIndex)
-
返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex
public String substring (int beginIndex,int endIndex)
转换功能的方法
-
将字符串转换为新的字符数组:
public char[] toCharArray()
-
使用平台的默认字符集将该String编码转换为新的字节数组:
public byte[] getBytes()
-
将与targer匹配的字符串使用replacement字符串替换
public String rep;ace (CharSequence targer,CharSequence replacement)
分割功能的方法
将字符串按照给定的regex(规则)拆分为字符串数组:public String[] split(String regex)