【Java】JDK常用类及其常用方法

一.JDK-API的使用

  1. 参数列表。需要什么参数,传入什么参数
  2. 返回值类型。返回什么类型,用什么类型数据接收

二.Object类

  1. 类 Object 是类层次结构的根类。每个类都使用 Object 作为超类。所有对象(包括数组)都实现这个类的方法。 默认不用声明继承,并可直接调用其方法
  2. 常用方法
    (1)toString
    1)public String toString()
    2)方法说明:返回该对象的字符串表示
    getClass().getName() + ‘@’ + Integer.toHexString(hashCode())
    返回类名.对象名@.当前对象物理地址哈希码
    3)使用:可以通过此方法返回默认内容,也可以通过方法的重写返回自定义内容
    4)代码示例:
//@author:林寒_
//API练习-toString
public class Student {
   String name;
   int age;
   //如果未重写toString方法,默认返回getClass().getName() + '@' + Integer.toHexString(hashCode())
   //重写后根据return的重定义,进行值的相应返回
   public String toString() {
   	return "名字:"+name+", 年龄:"+age;
   }
   public static void main(String[] args) {
   	Student s=new Student();
   	s.name="林寒";
   	s.age=20;
   	//A.声明toString(),并输出相应内容
//		String str=s.toString();
//		System.out.println(str);
   	//B.toString为默认方法,可以不声明直接使用
   	System.out.println(s);
   }	
}

(2)equals
1)public boolean equals(Object obj)
2)方法说明:指示其他某个对象是否与此对象“相等”。注!默认两个非空引用值指向的是物理地址中同一个对象时才能返回true,所以即使看起来内容相同的两个对象,实际物理地址并不是同一个,也会返回false
3)使用:可以通过此方法返回默认内容,也可以通过方法的重写返回自定义内容,例如通过特定设置来判断两个对象属性是否逻辑相等

(3)getClass
1)public final Class<?> getClass()
2)方法说明:返回此对象的当前运行时类(而不是其父类等)
3)使用:通过此方法返回默认内容,该方法不能被重写。注!使用getClass()返回的类时,若想访问其类的对象成员,需要将该类的对象强制转换成当前类(例:Student s=(Student)obj;)
4)代码示例:

//@author:林寒_
//API练习-equals	    boolean equals(Object obj)
//API练习-getClass   obj getClass(Object obj)
//指示其他某个对象是否与此对象相等
public class Student2 {
   String name;
   int age;
   //重写equals方法
   public boolean equals(Object obj) {
   	//判断obj传入的是不是空的,如果为空则返回false
   	if(obj==null) {
   		return false;
   	}else {
   		//判断两个类是否相等
   		if(this.getClass()==obj.getClass()) {
   			//要求name和age相等情况下才返回ture
   			//要求把obj强制转换成Student类型才能访问子类成员
   			Student2 s=(Student2)obj;
   			//下面的equals为String类的方法,判断两个字符串是否相等
   			if(this.name.equals(s.name)&&this.age==s.age) {
   				return true;
   			}
   		}
   	}
   	return false;
   }
   public static void main(String[] args) {
   	Student2 s1=new Student2();
   	s1.name="马克思";
   	s1.age=40;
   	Student2 s2=new Student2();
   	s2.name="恩格斯";
   	s2.age=50;
   	Student2 s3=new Student2();
   	s3.name="马克思";
   	s3.age=40;
   	boolean bo1=s1.equals(s2);
   	System.out.println("s1与s2是否相等呢?答案是:"+bo1);
   	boolean bo2=s1.equals(s3);
   	System.out.println("s1与s3是否相等呢?答案是:"+bo2);
//		s1与s2是否相等呢?答案是:false
//		s1与s3是否相等呢?答案是:true
   }
}

(4)finalize:
1)protected void finalize()throws Throwable
2)方法说明:系统垃圾回收机制。java有2个机制:JVM(java虚拟机)、 GC(垃圾回收机制)。 没有引用指向时进行回收,或者说该回收的时就回收。
3)使用:可以通过此方法进行默认规则的垃圾回收,也可以通过方法的重写对垃圾回收机制进行场景化定义

三.String类

  1. public final class Stringextends Object。字符串类,它的值在创建后不能再更改,字符串缓冲区支持可变的字符串
  2. 使用示例
     System.out.println("abc");
     String cde = "cde";
     System.out.println("abc" + cde);
     String c = "abc".substring(2,3);
     String d = cde.substring(1, 2);
  1. 常用方法
    (1)length()
    1)public int length()
    2)方法说明:返回此字符串的长度。长度等于字符串中 Unicode 代码单元的数量。
    (2)split()
    1)public String[] split(String regex)
    2)方法说明:返回字符串数组。根据给定的正则表达式的匹配拆分此字符串
    (3)charAt()
    1)public char charAt(int index)
    2)方法说明:返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。序列的第一个 char 值位于索引 0 处,第二个位于索引 1 处,依此类推,类似于数组索引。
    (4)replace()
    1)public String replace(char oldChar,
    char newChar)
    2)返回一个新创建的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
    (5)indexOf()
    1)public int indexOf(int ch/String str)
    2)返回指定子字符串在此字符串中第一次出现处的索引
    (6)indexOf()
    1)public String substring(int beginIndex,
    int endIndex)
    2)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
    (7)valueOf()
    1)public static String valueOf(//任何数据类型)
    2)讲输入的数据转换成字符串形式并返回
    (8)equals()
    1)public boolean equals(Object anObject)
    2)将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true。
    3)注!== 和 equals区别?
    ==比较的是对象的地址
    equals 比较对象的内容是否相等
  2. 其它
    String \ StringBuffer\StringBuffer StringBulider的区别
    (1)String是不可改变的
    (2)StringBuffer是 线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。
    (3)StringBulider是线程不安全的可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。
  3. 代码示例
    (1)String
public class StringTest {
	public static void main(String[] args) {
		String str="sjkfwej1234,4,dkd,k";
//		 求出来 字符串的长度
		 System.out.println(str.length());
//		按照逗号分隔成不同的子串
		 String [] strs=str.split(",");
		 for(int i=0;i<strs.length;i++){
			 System.out.print(strs[i]+"---");
		 }
//		 把这串里面的d 求出来一共有几个 ?
		 for(int i=0;i<str.length();i++){
			 int d=str.charAt(i);
			 if('d'==d){
				 System.out.println((char)d);
			 }
			
		 }
//		把4替换成5
		 String strNew=str.replace("4", "5");
		 System.out.println(strNew);
//		判断下 这个串是不是以k结束的?
		 System.out.println(str.endsWith("k"));	
//		求s的位置
		 System.out.println(str.indexOf("s"));
//		单独把1234输出到控制台
		 System.out.println(str.substring(7, 11));
		 
		 // 把  下面的int 类型换成 字符串
		 int i=2234;
		 String si=String.valueOf(i);
		 System.out.println(si.length());
		 //==与equals区别
		 String s="abc";
		 String s1="abc";
		 String s2=new String("abc");
		 String s3=new String("abc");
		 System.out.println(s==s1);//
		 System.out.println(s2==s3);// 
		 System.out.println(s2.equals(s3));//
	}
}

(2)String与StringBuffer比较

//StringBuffer与String
public class StringBufferTest {
	public static void main(String[] args) {
		//StringBuffer创建新字符串,
		StringBuffer a=new StringBuffer("abc");
		//String创建字符串
		String b="abc";
		//获取系统当前毫秒数
		long l=System.currentTimeMillis();
		for(int i=0;i<5000;i++) {
			//在StringBuffer字符串中加入新字符
			a.append("h");
		}
		System.out.println(l);
		l=System.currentTimeMillis();
		System.out.println(l);
		for(int j=0;j<5000;j++) {
			//在String字符串中加入新字符
			b+="h";
		}
		l=System.currentTimeMillis();
		System.out.println(l);
//		1574846027689
//		1574846027693
//		1574846027726
	}
}

四.Math类

  1. public final class Mathextends Object。Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
  2. 常用方法:
    (1)random()
    1)public static double random()
    2)方法说明:返回带正号的 double 值,该值大于等于 0.0 且小于 1.0,返回值是一个伪随机选择的数,在该范围内(近似)均匀分布。
    3)使用:第一次调用该方法时,它将创建一个新的伪随机数生成器 java.util.Random
    更改随机取值范围:Math.random()*10+20
    此处20为左区域,右区域为20+10=30

(2)floor()
1)public static double floor(double a)
2)方法说明:返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数
3)代码示例

//@author:林寒_
import java.util.*;
public class MathTest {
	public static void main(String[] args) {
//  	double floor(double a) 
//      返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数
		double i=Math.floor(11.9);
		double j=Math.floor(-11.9);
		System.out.println(i);
		System.out.println(j);
//		double random() 
//      返回带正号的 double 值,该值大于等于 20且小于 30的数
		System.out.println((int)(Math.random()*10+20));
	}
//	11.0
//	-12.0
//	21
}

五.Date类

  1. java.util.Date 时间类,获取当前时间信息
    多数方法已过时,由Calendar取代,可使用SimpleDateFormat对Date进行String转化,进而格式化显示

  2. 常用方法
    (1)getTime
    public long getTime()
    返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
    (2)getMonth
    返回表示月份的数字,该月份包含或开始于此 Date 对象所表示的瞬间。返回的值在 0 和 11 之间,值 0 表示 1 月。

  3. 代码示例

//@author:林寒_
//API-Date练习
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateTest{
	public static void main(String[] args) {
		Date  d =new Date();
		System.out.println(d.getTime());   //1574846848963
		System.out.println(d.getMonth());  //10(默认月份数要加一,当前为11月)
	// 日期转换格式的类
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
	// 重点,把日期转换成字符串
		String str=sdf.format(d);
		System.out.println(str);  //2019-11-27
		// 从 字符串 转成成日期
		String b="1999-09-05";
		try {
			Date d1= sdf.parse(b);
			System.out.println(d1.getMonth());
		} catch (ParseException e) {
			e.printStackTrace();
		}	
	}
}

六.包装类

8种基本数据类型都对应着一个包装类类型(byte->Byte),特例是int->Interger、 char->Character,用以实现数据 类型间的转换

public class IntegerTest {
	public static void main(String[] args) {
		// int ==》String 
		int i=12345;
		String si=String.valueOf(i);
		String si1=i+"";// 字符串和任何数据类型 相连,结果都是字符串
		// 字符串 转换成 int 
		String s="3333";
		int is=Integer.parseInt(s);
		System.out.println(is+1);
		
		// int ===>Integer 之间的互相转换
		 int i1 =898;
		 Integer it1=i1;// jdk1.5 之后可以,拆箱
		 Integer  it2 =new Integer(i1);
		 // Integer ==>int
		 Integer it3=new Integer(10);
		 int  i2=it3.intValue();
		 
		 // String <==>	Integer
		 String str="333";
		 Integer  it4 =new Integer(str);
		 String  str1=it4.toString();
	} 
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值