public final Class getClass():表示当前正在运行的类
返回值类型为class
Class类中有一个:getName():
public String getName():以字符串展示字节码文件对象所表示实体
获取一个类的字节码文件对象有几种方式?
三种:
1) Object类中的getclass()方法 ----> class 包名.类名
2) java类型的clas属性 .class ----> class 包名.类名
3) Class类中的 forName() 方法 ----> class 包名.类名
// 代码实现举例: publicclassTest{publicstaticvoidmain(String[] args){
Class c1=null;try{
c1=Class.forName("java.lang.String");//第一种}catch(ClassNotFoundException e){
e.printStackTrace();}
String s="abc";
Class c2= s.getClass();//第二种
Class c3=String.class;//第三种
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);}}
分析重写的hashCode()方法和equals()方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) { // s2 相当于:Object obj = new Student() ;
if (this == obj) // 判断如果 s1==s2
return true;
if (obj == null) //传递进来对象是否为null
return false;
if (getClass() != obj.getClass()) //this.getClass !=s2.getClass() :比较两个对象的字节码文件不相同的
return false;
Student other = (Student) obj; //向下转型子类的引用: 具体Student
if (age != other.age) //this.age != s2.age
return false;
if (name == null) { //当前(s1)this.name 如果是null
if (other.name != null)
return false;
} else if (!name.equals(other.name)) // name--->String 比较两个对象姓名的内容是否相同
//String类:里面重写了Object类中equals
return false;
return true; //返回的结果:两个对象内容相同
}
Scanner – 文本扫描器
java.util.Scanner
构造方法
public Scanner(InputStream source)
形式参数:
抽象类:java.io.InputStream:字节输入流(用来读取字节数)
如果方法的形式参数是一个抽象类,那么需要传递该抽象类的子类对象
public static final InputStream in:成员变量 :标准输入流
public static final PrintStream out:标准输出流
Scanner类中提供了一些判断功能:
public boolean hasNextXXX():判断当前是否是执行XXX类型 int,short,float,double...
public boolean haxNextInt() :判断接收的类型是否是int类型
public XXX nextXXX() :接收指定XXX类型
public int nextInt() : 接收指定的int类型的数据
Scanenr:键盘录入的细节问题:
先录入int类型,在录入String类型 (nextLine()) 出现问题: 回车符号本身就是字符串,会漏值
解决方案: 在下一次录入String类型前,再次创建一个Scanner类型对象
为了方便使用--> 全部使用String类型接收
public boolean equals(Object anObject):比较两个字符串内容是否相同
public boolean equalsIgnoreCase(String anotherString):不区分大小写比较
public boolean startsWith(String prefix):判断是否以指定的子串开头
public boolean endsWith(String suffix):判断是否以指定的子串结尾
public boolean isEmpty():判断字符串是否为空
String类型的获取功能
int length():获取字符串长度
public char charAt(int index):获取指定索引处的字符
public String concat(String str):字符串拼接功能:使用当前字符串和指定str字符串进行拼接
public int indexOf(int ch):获取指定字符在此字符串中第一次出现索引值
public int indexOf(String str):获取指定字符串在此字符串中第一次出现索引值
String类的分割功能
public String[] split(String regex):通过指定的字符串进行分割,返回值字符串数组
String类转换功能
public char[] toCharArray():将字符串转换成字符数组
public byte[] getBytes() :将字符串----变成字节数组 (过程:编码)
public static String valueOf(XXX xx):可以将任何类型的数据转换成String
举例:
int i = 100 ;---->int --- String
String.valueOf(i) ;---->"100"
public String toUpperCase():将此字符串转换成大写
public String toLowerCase():将此字符串转换成小写
String类的截取功能
public String substring(int beginIndex):从指定位置开始截取,默认截取到末尾
public String substring(int beginIndex,int endIndex)
从指定位置开始截取,到指定位置结束,从beginIndex 到 endIndex-1 处
public String replace(char oldChar,char newChar):将以前的字符用心的字符替换掉
public String replace(String oldStr,char newStr):将以前的字符用心的字符替换掉
public String trim()去除两端空格
compareTo – 按照字段顺序比较
public int compareTo(String anotherString)
按字典顺序比较两个字符串。该比较基于字符串中各个字符的 Unicode 值。
String s1 = "abc";
String s2 = "hello" ;
//没有相同的字符, 'a' - 'h' = 97- 104 = -7
int num = s1.compareTo(s2) ;
System.out.println("num:"+num); // -7
String s1 = "hello" ;
String s2 = "hel" ;
int num = s1.compareTo(s2) ; //有相同的字符,为 5-3
System.out.println("num:"+num); //2
Integer类在对象中包装了一个基本类型 int的值。
Integer类型的对象包含一个int类型的字段。
此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。
构造方法:
public Integer(int value)构造一个新分配的 Integer 对象,它表示指定的 int 值。
public Integer(String s) throws NumberFormatException
构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
注意事项:
String是一个数字字符串,否则出现异常!
成员变量(字段)
public static final int MIN_VALUE:最小值
public static final int MAX_VALUE:最大值
方法:
public static String toBinaryString(int i)
以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。
public static String toOctalString(int i)
以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。
public static String toHexString(int i)
以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
public static String toString(int i)
返回一个表示指定整数的 String 对象。
public String toString()
返回一个表示该 Integer 值的 String 对象。
覆盖/重写:类 Object 中的 toString
import java.util.Scanner;/*
* 键盘录入字符串,统计字符串中大写字母字符,小写字母字符,数字字符的个数(不考虑其他字符)
*使用Character类的判断功能
* */publicclassTest{publicstaticvoidmain(String[] args){//统计变量int a =0;//大写int b =0;//小写int c =0;//数字//创建键盘录入对象
Scanner sc =newScanner(System.in);//提示并录入数据
System.out.println("请输入字符串:");
String line = sc.nextLine();//将字符串内容:直接遍历for(int i =0; i < line.length(); i++){//将每一个字符串里面字符获取到char ch = line.charAt(i);//使用Character类的判断功能if(Character.isUpperCase(ch)){
a ++;}elseif(Character.isLowerCase(ch)){
b++;}elseif(Character.isDigit(ch)){
c++;}}
System.out.println("大写字母字符有:"+a+"个");
System.out.println("小写字母字符有:"+b+"个");
System.out.println("数字字符有:"+c+"个");}}
Math
Math类的常用方法
Math 类包含用于执行基本数学运算的方法,
public static final double PI
比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。
public static final double E
比任何其他值都更接近 e(即自然对数的底数)的 double 值。
public static XXX abs(XXX a)
返回XXX类型的绝对值
public static double pow(double a,double b)
返回第一个参数的第二个参数次幂的值。
public static double sqrt(double a)
返回正确舍入的 double 值的正平方根。
Random
Random类的常用方法
java.util.Random:随机数生成器
构造方法
public Random() :(推荐),产生的随机数是变化的
public Random(long seed):每次产生的随机数 是固定的!
成员方法:
public int nextInt():获取的是int范围内的随机数
public int nextInt(int n):获取0到n之间的随机数[0,n)
获取随机数两种方式:
Math类的random()方法: 返回值double类型
Random类空参构造+nextInt(int n):获取随机数
Calendar
Calendar类的常用方法
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法
Calendar不能直接实例化!
某一个类如果不能直接实例化:
首先里面会存在一些静态功能,静态功能的返回值就是该类本身!
public static Calendar getInstance()
public int get(int field):获取的日历字段的值!
参数:传递日历字段
public abstract void add(int field,int amount)
日历对象.add(Calendar.DATE,-5):当前日历时间的5天前
设置当前年月日,日历时间
public final void set(int year,int month, int date)
// 1.键盘录入出生的年月日,算一下你来到这个世界多少天? (提示:Date日期和String文本转换)import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Scanner;publicclassTest1{publicstaticvoidmain(String[] args)throws ParseException {
Scanner sc =newScanner(System.in);
System.out.println("请输入出生日期-- 格式为 2020-1-1: ");
String s = sc.nextLine();//使用方法stringDate将String文本类型转换为Date日期类型
Date d =stringDate(s,"yyyy-MM-dd");//获取输入的日期和1970年1月1日之间所产生的毫秒long time = d.getTime();//获取当前日期和1970年1月1日之间所产生的毫秒long now =newDate().getTime();//计算差值long day = now - time ;//通过毫秒计算出天数
System.out.println(day/1000/3600/24);}//String类型转换为Date类型publicstatic Date stringDate(String s,String pattern)throws ParseException {
SimpleDateFormat sdf =newSimpleDateFormat(pattern);
Date d =sdf.parse(s);return d;}}
System
System类的常用方法
System 类包含一些有用的类字段和方法。它不能被实例化。
字段:
public static final InputStream in :标准的"输入流"
public static final PrintStream out :标准的"输出流"
键盘录入:
Scanner(InputStream in) ---->Scanner(System.in)
输出语句:
System.out.println("xxx")----->System.out--->PrintStream ps (字节打印流)
ps.println("x") : 字节打印流调用方法名println(String str)
成员方法:
public static void gc() 运行垃圾回收器(手动开启)
实际开发中,开发者不需要开启
运行垃圾回收器,它会调用Obejct类finalize(),回收没有更多引用的对象
public static void exit(int status):参数状态为0时 表示正常终止jvm
public static long currentTimeMillis():获取当前系统时间的毫秒值
在System中手动开启垃圾回收器 – 重写finalize
//定义一个类, publicclassPerson{
String name ;int age ;publicPerson(){super();}publicPerson(String name,int age){super();this.name = name;this.age = age;}@Overridepublic String toString(){return"Person [name="+ name +", age="+ age +"]";}//重写Object类中finalize()方法@Overrideprotectedvoidfinalize()throws Throwable {
System.out.println("垃圾回收器将回收这个对象"+this);super.finalize();}}publicclassTest{publicstaticvoidmain(String[] args){//public static void gc() 运行垃圾回收器(手动开启)://回收不用的对象//创建一个Person对象
Person p =newPerson("高圆圆",41);
System.out.println(p);//Person [name=高圆圆, age=41]//重写赋值
p = null ;//手动开启垃圾回收器
System.gc();//垃圾回收器将回收这个对象Person [name=高圆圆, age=41]}}
BigDecimal
BigDecimal类的常用方法
java.math.BigDecimal
描述:对浮点类型的数据进行精确计算的!
构造方法:
public BigDecimal(String val) :字符串"数字字符串"
"1.0"
"0.01.."
成员方法:
public BigDecimal add(BigDecimal augend) :求和
public BigDecimal subtract(BigDecimal subtrahend):减法
public BigDecimal multiply(BigDecimal multiplicand):乘法
public BigDecimal divide(BigDecimal divisor)除法