java基础之API整理

java基础之API整理

才不是api调用工程师

Object类

概述
1. Object是类层次结构的根类.每个类都使用Object作为超类.所有对象(包括数组)都实现
这个类的方法.随意定义一个类型,不手动显式定义其父类,那么这个类的父类就是Object类.

构造方法:

Object() : 空参构造中,第一行, 没有调用super(), 因为Object没有父类

用途:

1.创建子类对象时使用的方法
2.子类方法,所有子类都会直接或间接访问到这个顶层父类的构造方法
toString
public String toString ()
    返回当前对象的字符串表示.默认Object类的toString方法,,getClass().getName() + '@' + Integer.toHexString(hashCode())这几部分组成
    1)getClass().getName() 表示类的完全限定名. 
    2)hashCode() 表示根据内存地址通过哈希算法生成的哈希码值. 					3)Integer.toHexString() 表示无符号十六进制的字符串表示形式.

  1. 重写
 1. 重写的原则:返回该对象中的所有成员变量的值(对象的属性)
 2. 快捷键生成:alt + insert -> 点击 toString方法
 //举例
 // 重写toString方法
     @Override 
    public String toString() { 
     return "Person{" + 
         "name='" + name + '\'' + 
         ", age=" + age + 
         ", gender='" + gender + '\'' + 
         '}';
 }
equals方法
1. public boolean equals(Object obj)
    1)比较内容的不同: 
		== 可以比较任意数据类型, 既可以比较基本数据类型,也可以比较引用数据类型.
   		 equals 方法只能比较引用数据类型.
    2) 比较规则不同: 
		==在比较基本类型的时候,比较的是数据的值,比较引用类型时,比较的是地址值. 			equals方法在重写之前,比较的是两个对象的地址值,在重写之后,不仅比较地址,还比较的属性值.

String类

String类的特点
1. String类概述: String类就是字符串类型,属于java.lang包,不需要导包. 
2. String类代表字符串.Java程序中的所有字符串字面值("abc")都作为此类的实例实现. 3. 在Java中只要使用双引号引用起来的任何数据它们都是String的一个对象. "", "a", "abc", "cdadfasdfasf" 
4. 字符串字面值(字符串常量)属于常量,存储在方法区的常量池中,在创建之后就无法更改(是一个不可 变的字符序列).但是它们可以被共享.
private final char value[];
5. 字符串在效果上,可以看做是字符序列,但实质上是字符数组. String s = "abc" value = {'a', 'b', 'c'}
String构造方法
1.String()// 初始化一个新的String对象,使其表示一个空字符序列
    //举例
    String s1 = new String(); 
	System.out.println(s1);

2.String(String original)//初始化一个新创建的String对象,使用另一个字符串创建新的字符串对象
    String str = "abc"; 
	String s2 = new String(str); // 是把常量池"abc"复制一份,在堆区开辟空间存储"abc"
   
3.String(byte[] arr)//传入一个字节数组,创建一个字符串对象
    byte[] bs = {97, 98, 99, 100}; 
	String s3 = new String(bs); // 会把字节转为字符,拼接为字符串

 //4创建一个字节数组,从字节数组offset位置开始,向后找length字节转为字符串
4. String(byte[] arr, int offset, int length)
   
5.String(char[] arr) //传入一个字符数组,转为字符串
    //举例
    char[] cs = {'翔', 'd', 's', 'B'}; 
	String s5 = new String(cs);

// 传入一个字符数组,从offset位置开始向后找length字符转为字符串
6. String(char[] arr, int offset, int length)
    
字符串的常用方法
String 类的获取功能
1. length() : 获取到一个字符串中的字符个数(字符串的长度) , 返回值类型int 
2. charAt(int index): 将字符串中指定的index索引位置上对应的字符获取到,返回值类型char 
3. subString(int beginIndex): 截取出字符串的一部分,从beginIndex索引开始, 到最后的全部字符序列取出来, 返回值结果,新的字符串String 
4. subString(int beginIndex, int endIndex) : 从beginIndex到endIndex-1之间的字符序列截取成一个新的字符串, 返回值结果String
注意 : JDK的方法中, 如果是对于两个索引位置进行操作, 通常包括开始索引, 不包括结束索引
5. indexOf(String str) : 获取参数字符串str在方法调用字符串中第一次出现的索引位置返回值类型int, 如果没有找到参数str存在位置, 返回-1 
6. indexOf(String str, int formIndex):返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索, 返回值类型int, 如果没有找到参数str存在位置, 返回-1 
7. lastIndexOf(String str) : 获取参数字符串str在方法调用字符串中最后一次出现的索引位置返回值类型int, 如果没有找到参数str存在位置, 返回-1
注意 : indexOf 和 lastIndexOf方法中查找字符串的位置, 无论从哪里开始找, 参数字符串在原有字符
串中的位置都不会发生变化
字符串比较规则
public boolean equals(Object anObject):

String类重写的方法,判断两个字符串的内容(字符序列)是否相等, 常用于比较字符串的内容是否相等
== : 在比较基本类型的时候,比较的是数据的值,比较引用类型时,比较的是地址值是否相等.
对象描述的字符串内容是否相同
    
    //举例
    String s1 = new String("abc"); 
	String s2 = new String("abc"); 									System.out.println(s1.equals(s2)); // true
System.out.println(s1 == s2);//false 因为s1和s2存储的堆区对象地址不一样

	String s3 = "abc"; String s4 = "abc"; 							System.out.println(s3.equals(s4)); // true 						System.out.println(s3 == s4); // true
// "abc"在方法区常量池中存储, 常量池中只会存一个"abc",所以s3和s4的地址一样

String其他常用方法
  • 字符串的常用方法
//判断对象和参数对象描述的字符串内容是否相同
1.public boolean equals(Object anObject)

//忽略大小写,判断对象和参数对象描述的字符串内容是否相同
2.public boolean equalsIgnoreCase(String anotherString)

3.public boolean contains(CharSequence s) //判断对象是否包含了 s这个子串
4.public boolean startsWith(String prefix) //判断对象是否以 prefix 开头
5.public boolean endsWith(String suwix) //判断对象是否以suwix 结尾
6.public boolean isEmpty() //判断对象是否是空串
    
转换功能:
7.public byte[] getBytes() //将当前字符串转成字节数组
8.public char[] toCharArray() //将当前的字符串转成字符数组
9.public String toLowerCase() //将当前的字符串,转成全小写形式
10.public String toUpperCase(Locale locale)//将当前的字符串,转成全大写形式

//将当前对象,和参数 str 进行拼接,返回拼接后的长字符串(不常用,因为更多使用的是运算符 +) 
11.public String concat(String str)
12.public static String valueOf(Object obj) //返回Object参数的字符串表示形式
    //举例
    Object o = new Object(); 
	String s = String.valueOf(o); // 返回的其实就是这个对象toString方法结果
	System.out.println(s);   //java.lang.Object@61bbe9ba
其他功能:
//将对象中包含的所有target替换成replacement 
13.public String replace(CharSequence target,CharSequence replacement)

14.public String[] split(String regex)//使用指定的字符串规则将对象字符串切割成多个子串
15.public String trim() //去掉字符串左右两边的空格,制表符

StringBuilder类

1.StringBuilder类 的作用
概述
StringBuilder是字符串缓冲区(可变字符串),也是字符串的另外一种表现形式。 字符串缓冲区可以理解为是对字符串进行整容的医院。缓冲区就是一个可以对字符串进行增删改的容器 StringBuilder对存储字符串操作,都是在原串的基础进行了,如果改变StringBuilder对象内容,原串就真 的变了.
特点
1、长度可以改变【字符串缓冲区是一个可变的字符串体现形式】有很多append和insert方法, 
2、他的功能都是在同一个字符串对象上发生效果,不会额外生成字符串对象。 
3、长度自动扩容的【自我维护的】
作用
对内部给定的字符串进行修改
StringBuilder类的构造方法
1.public StringBuilder() 创建一个生成器,初始容量为 16 个字符
2.public StringBuilder(int capacity)创建一个生成器,初始容量为 capacity 大小
3.public StringBuilder(String str)创建一个生成器,初始值就是str这些字符,初始大小是str+16
StringBuilder类的常用方法
//在StringBuilder的尾部,可以将任意数据类型,转成字符,拼接到可变字符串中
1.public StringBuilder append(XXX obj) 

2.public StringBuilder insert(int index,XXX obj) //可以将任意数据类型,添加到指定的位置
说明: (1)index 的范围是 0~ 当前缓冲区的大小; (2)插入数据之后,数据本身的索引就是参数中指定的
index 
3.public StringBuilder deleteCharAt(int index)  //删除指定索引的字符

//删除指定范围的字符,被删除的部分包含头不包含尾
4.public StringBuilder delete(int start,int end)

//将字符串缓冲区中的从 start 开始到 end-1 这部分内容,替换成 str
5.public StringBuilder replace(int start,int end,String str)

6.public StringBuilder reverse()  //将原有字符序列进行反转
7.public String toString()  //将StringBuilder对象转换成字符串

基本类型包装类

1.包装类概述分类
基本数据类型包装类
byteByte
shortShort
intInteger [特殊]
charCharacter [ 特殊 ]
floatFloat
longLong
doubleDouble
booleanBoolean
Integer类常用构造方法
1.public Integer(int value)  //将一个基本类型的int数值,转换成Integer类型的对象
    //举例
    Integer i1 = new Integer(10); 
	System.out.println(i1); //10
    
//将一个字符串类型的数字,转换成 Integer类型的对象【字符串必须是数字字符串】
2.public Integer(String s)  
    //举例
    Integer i2 = new Integer("321"); 
	System.out.println(i2);//321
Integer类常用方法
1.public int intValue()  //可以将Integer类型的对象,转成其他的基本数据
2.类型: 例如: byteValue()floatValue() 
    
3.public static int parseInt(String s) //将s以十进制的方式解读为一个int数 
    //举例
    String num = "10"; // 参数必须是整数字符串 
	int x = Integer.parseInt(num); 
	System.out.println(x);  //10
4.public static String toBinaryString(int i) //使用二进制的表示方式表示数字i 
5.public static String toOctalString(int i) //使用八进制的表示方式表示数字i 
6.public static String toHexString(int i) //使用十六进制的表示方式表示数字i 
    
  //使用指定的radix进制,表示数字s   
7.public static int parseInt(String s,int radix)throws NumberFormatException
  //将s进行解析,封装为一个 Integer对象
8.public static Integer valueOf(String s)throws NumberFormatException 
    //举例
    Integer i7 = Integer.valueOf("100"); 
	System.out.println(i7);//100
  
Integer类的常量
1.public static final int MAX_VALUE int 类型的最大值
2.public static final int MIN_VALUE int 类型的最小值
3.public static final int SIZE int 类型在内存中的比特位数
4.public static final Class TYPE int 类型在方法区中的字节码对象,int.class
自动拆箱和自动装箱
// 手动装箱 
// 把基本数据类型的数据存放到对应的包装类类型的对象中的过程就叫做装箱 
	Integer i = new Integer(10); // 10是int类型, 把它装到了Integer对象中 
	Integer i2 = Integer.valueOf(100); // 100是int类型, 把它装到了Integer对象中

// 手动拆箱 
// 把包装类的对象中的值存放到对应的基本数据类型变量中的过程就叫做拆箱
	int a = i.intValue(); 
	System.out.println(a);

// 自动装箱 直接使用基本类型数据,给对应包装类型对象赋值 
	Integer i3 = 100; // new Intger(100) 
	Float f = 3.14f; // new Float(3.14f);

// 自动拆箱 直接可以使用包装类型对象给对应基本类型变量赋值
	int i5 = new Integer(100); // new Integer(100).intValue()
	float f2 = f; // f.floatValue()

MAth类及其常用方法

Math类及其常用方法
private Math(){}

Math对外没有提供构造方法, 意味着不能创建Math对象.这个类中提供的方法,全部都是静态
的方法,提供属性也都是静态属性.都可以通过类名访问.
Math类及其常用方法
1.public static double abs(数字类型)  //返回该参数的绝对值
2.public static double cbrt(double a)  //返回 a 的开立方根
3.public static double sqrt(double a)  //返回 a 的正平方根
4.public static double ceil(double a)  //返回 a的向上取整
5.public static double floor(double a)  //返回 a的向下取整
6.public static int max(int a,int b)  //返回 a、b 的较大值
7.public static int min(int a,int b)  //返回 a、b 的较小值
8.public static double pow(int a,int b)  //返回 a 的 b 次幂
9.public static double random()  //返回值为double的正值,[0.0,1.0) 
10.public static long round(double a)  //返回 a四舍五入的结果【只针对小数点第一位】

System类及其常用方法

1. System类的概述: System 类包含一些有用的属性和方法。它不能被实例化。
 private System(){}
System类常用属性
1.public static final InputStream in  //标准输入流,默认关联到键盘上
2.public static final PrintStream out  //标准输出流,默认关联到控制台上
3.public static final PrintStream err //标准错误输出流,默认关联到控制台上,打印错误信息
System类常用方法
1.public static void exit(int status) //退出JVM,零表示正常终止
    
//返回当前时间的毫秒值,表示的是从 1970 年1月 1日0 时0 分0 秒开始到现在经历的毫秒值
2.public static long currentTimeMillis()

3.public static void gc() //强制垃圾回收器回收内存中的垃圾
    
4.public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
//快速数组复制
//五个参数分别为:
//源数组, 起始索引, 目的数组, 起始索引, 拷贝元素个数
Scanner
Scanner类的概述: 一个可以录入文本信息,解析为基本类型和字符串的扫描器。

常用构造方法: Scanner(InputStream input) 传入一个标准输入创建一个扫描器对象
   

Scanner的功能:

  • nextXxx(): 录入基本数据类型数据到内存.出了char之外,所有的基本数据类型都可以接受

  • Xxx就是对应的基本数据类型的名称 比如:录入int整数—nextInt() 录入: nextBoolean() 这些方法也是碰到空格结束扫描

  • next(): 录入不含空格的字符串 【读取的时候遇到空格结束】

  • nextLine():录入一整行字符串【以回车符号为标准,读取的时候遇到回车键的符号不读取】

大型数据类型

BigInteger类及其常用方法
  • BigInteger概述:用来处理大于最大值整数类型(long)范围的数据. 在java.math包下,使用时要导包
BigInteger 类及其常用方法
1.public BigInteger(String val) //将指定字符串转换成BigInteger对象
	//举例
    BigInteger b1 = new BigInteger("9223372036854775808"); 							System.out.println(b1);  //9223372036854775808

//根据指定的radix进制,将指定字符串转换成BigInteger对象
2.public BigInteger(String val,int radix) 
    //举例
    BigInteger b2 = new BigInteger("100", 8); 
	System.out.println(b2); //64
BigInteger类常用方法
1.public BigInteger abs()  //返回BigInteger对象的绝对值
2.public BigInteger negate() //取该对象的反数
3.public BigInteger add(BigInteger val) //加法
4.public BigInteger subtract(BigInteger val)  //减法
5.public BigInteger multiply(BigInteger val)  //乘法
6.public BigInteger divide(BigInteger val)   //除法
BigDecimal类及其常用方法
  • .BigDecimal概述:用来处理大于最大值小数类型double范围的数据. 在java.math包下,使用时要导包
BigDecimal类及其常用方法
1.public BigDecimal(double val) //将double类型的数据转换成BigDecimal对象
2.public BigDecimal(String val) //将String类型的数据转换成BigDecimal对象
    
    BigDecimal类常用方法
    
1.public BigDecimal add(BigDecimal augend) //加法
2.public BigDecimal subtract(BigDecimal subtrahend) //减法
3.public BigDecimal multiply(BigDecimal multiplicand) //乘法
4.public BigDecimal divide(BigDecimal divisor) //除法
5.public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode) //除法
三个参数分别表示: 除数、精确小数位、舍入模式
常用舍入模式: BigDecimal.ROUND_UP  向上取整
    		BigDecimal.ROUND_FLOOR  向下取整法
			BigDecimal.ROUND_HALF_UP  四舍五入
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值