Java常用类(详细)总结

👏👏👏 学习的痛苦是一时的,而没有学习的痛苦是一辈子的


目录

一、String类

二、BigDecimal类

三、Date 类

四、Calendar 类

五、SimpleDateFormat 类

六、Math类

七、Random类

八、System类


一、String类

         1.String创建方式。

    String name = "hello"; //    "hello" 常量存储在字符串池中。
	name = "zhangsan";    //"zhangsan" 赋值给name变量,给字符串赋值时,并没有修改数据,而是重新开辟空间。
	String name2 = "zhangsan" ;  //如果有就引用, 没有就重新开辟空间。
	//字符串另一种创建方式 。
	String s = new String("java是最好的编程语言")://在堆和栈里同时创建,栈里的指向堆里的。
	String str = new String("java");
	String str2 = new String("java");
	System.out.println(str==str2);//返回false   new 新建对象。 用equals 比较的数据返回true。

        2.String 常用的方法。

    1. public int length();  //返回字符串的长度。
	2. public char charAt(int index); // 根据下标获取字符。
	3. public boolean contains(String str); // 判断当前字符串中是否包含str。 
	4. public char[] toCharArray(); //将字符串转换成数组
	5. public int indexOf(String str); // 查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1。
	6. public int lastIndexOf(String str); // 查找字符串在当前字符串中最后一次出现的下标索引。
	7. public String trim(); // 去掉字符串前后的空格
	8. public String toUpperCase(); // 将小写转成大写。
	9. public String toLowerCase(); // 将大写转成小写。
	10. public boolean endsWith(String str); // 判断字符串是否以str结尾。
	11. public boolean startsWith(String str); // 判断字符串是否以str开头。
	12. public String replace(char oldChar,char newChar); // 将旧字符串替换成新的字符串。
	13. public String[] split(String str); // 根据str做拆分。
		public String[] split("[a,]"); //表示用a或,拆分。//正则表达式
		public String[] split("[a,]+"); //+表示若连续的a或,也可以当作划分依据。
	14. equalsIgnoreCase  不区分大小写的比较;    s1.compareT o(s2);  //比较的是按字典序,输出s1-s2(比较第一个不相等的字符)。

注意!:    若 String s1 = "abc"; String s2 = "abcxyz"; //若第一个或第二个和另一个除了多余部分都相等,则比较的是两个字符串的大小,返回第一个字符串的长度-第二个字符串的长度。


         3.String案例

已知:String str = "this is a text";

需求:

        1.将str中的单词单独获取出来
        2.将str中的text替换为practice
        3.在text前面插入一个easy
        4.将每个单词的首字母改为大写        

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		String str ="this is a text";
		//1将str中的单词单独提取出来
		String[] arr = str.split(" ");
		System.out.println("---------------1将str中的单词单独提取出来----------------");
		for(String s : arr){
			System.out.println(s);
		}
		//2将str中的text替换为practice
		System.out.println("---------------2将str中的text替换为practice----------------");
		String str2 = str.replace("text","practice");
		System.out.println(str2);
		//3在text前面插入一个easy
		System.out.println("---------------3在text前面插入一个easy----------------");
		String str3 = str.replace("text","easy text");
		//4将每个单词的首字母改为大写
		System.out.println("---------------4将每个单词的首字母改为大写----------------");
		for(int i=0;i<arr.length;i++){
			char first = arr[i].charAt(0);
			//把第一个字符改成大写
			char upperfirst = Character.toUpperCase(first);
			String news = upperfirst+arr[i].substring(1); //从下标为1的位置截取到最后
			System.out.println(news);
		}
	}
}


        4.StringBuffer与StringBuilder

StringBuffer: 可变长字符串,JDK1.0提供,运行效率慢、线程安全
StringBuilder: 可变长字符串,JDK5.0提供,运行效率快、线程不安全 


注:StringBuffer和StringBuilder方法一模一样 

	StringBuffer sb = new StringBuffer();
	1 sb.append(String str);//在字符串末尾追加
	2.sb.insert(int index,String str);//在index位置添加str
	3.sb.replace(int start,int end,String str);//下标为start到下标为end-1的位置替换为str,! 换头不换尾!
	4.sb.delete(int start,int end);//删除下标为start到下标为end-1的位置

        5.StringBuffer与StringBuilder跟String类的对比

                5.1.StringBuffer和StringBuilder 均比String效率高、节省内存

                5.2.验证StringBuffer比String效率高


    long start = System.out.currentTimeMillis();
	String string="";
	for(int i=0;i<99999;i++){
		string+=i;
	}
	System.out.println(string);
	long end = System.out.currentTimeMillis();
	System.out.println("用时:"+(end - start));
    //用时:47475 
    //System.out.currentTimeMillis() 表示从1970年1月1日到现在的毫秒数

    long start = System.out.currentTimeMillis();
	StringBuffer string="";
	for(int i=0;i<99999;i++){
		string+=i;
	}
	System.out.println(string);
	long end = System.out.currentTimeMillis();
	System.out.println("用时:"+(end - start));
    //用时:308

可以得出,明显用StringBuffer效率比String高。


 二、BigDecimal类

        1.前言

    double d1=1.0;
	double d2=0.9;
    System.out.println(d1-d2);
    //输出结果为0.099999999999999998

这是因为double类型存储是双精度近似存储,但是在很多实际应用中需要精确运算,而double不符合要求。


        2.位置 :java.math包中

        3.作用:精确计算浮点数。

        4.创建方式:

BigDecimal bd = new BigDecimal("1.0"); //一定要用字符串

        5.常用的方法:

    BigDecimal bd1=new BigDecimal("0.9");
	BigDecimal bd2=new BigDecimal("1.0");
//减法:substract 加法:add 乘法:multiply
    BigDecimal r1 =bd1.substract(bd2);
	System.out.println(r1);
    //加法和乘法类似
//特殊:除法:divide 如果不能整除会报错 必须指明保留几位小数
    //divide(BigDecimal bd,int scal,RoundingMode mode)
		//参数scal:指定精确到小数点后几位
		//参数mode:
			//指定小数部分的取舍模式,通常采用四舍五入的模式
			//取值为BigDecimal.ROUND_HALF_UP
    BigDecimal r2 = bd1.divide(bd2, 2, RoundingMode.HALF_UP); //保留两位小数,且用四舍五入的方法
    //例如 计算(1.4-0.9)/0.9
    BigDecimal r2 =new BigDecimal("1.4").substract(new BigDecimal("0.5")).divide(new BigDecimal("0.9"));
    

三、Date类

        1.创建:

Date date1 = new Date();

        2.常用的方法:

    //今天的时间    
    Date date1 = new Date();
    System.out.println(date1.toString());//打印日期
        //输出结果:Mon Jun 20 18:31:56 CST 2022
    System.out.println(date.toLocaleString());//打印日期,此方法虽已为过时的方法,但是比toString看起来更符合我们日常。
        //输出结果:2022年6月20日 下午6:32:44
    //昨天的时间
    Date date2 = new Date(date1.getTime()-(60*60*24*1000)); //getTime() 返回从1970年1月1日到现在的毫秒数。
    //after、before 方法
    boolean b1 = date1.after(date2); //返回true 表示 date1 在 date2 后面
    //compareTo方法
    int d=date2.compareTo(date1);//返回-1表示date2比date小;1表示date2比date大;0表示date2和date一样大。
    //equals方法
    //比较是否相等  返回true  or  false。

四、Calendar 类

        1. 构造方法:protected Calendar() 由于修饰符是protected,所以无法直接创建该对象。但是Calendar提供了静态方法。


        2.常用方法:

    Calendar calendar = Calendar.getInstance(); //静态方法
	System.out.println(calendar.getTime().toLocaleString());//输出现在的时间
	System.out.println(calendar.getTimeInMillis()); //返回1970年1月1日到现在的毫秒数
    //获取时间信息
    int year = calendar.get(Calendar.YEAR); //月MONTH  天: DAY_OF_MONTH  小时: HOUR_OF_DAY(24)HOUR12(12) 分钟:MINUTE 秒:SECOND。
    //其中MONTH 范围是0-11,所以用的时候MONTH+1。
    //修改时间
    Calendar calendar2 = Calendar.getInstance()
	calendar2.set(Calendar.DAY_OF_MONTH,5);//表示将天数改为5.
    //add方法修改时间
    calendar2.add(Calendar.HOUR,1); // 加一个小时。
    //获取当前月份中的最大天数和最小天数。
    int max = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH); 
	int min  = calendar2.getActualMinimum(Calendar.DAY_OF_MONTH); 

五、SimpleDateFormat 类

        1. 位置:import java.text.SimpleDateFormat;       

        2.构造及常用方法:

SimpleDateFormat sdf = new SimpleDateFormat("日期格式字符串");
日期格式字符串,如:"yyyy-MM-dd hh:mm:ss"
yyyy    代表4位的年份
MM        代表2位的月份
dd        代表2位的日期
hh/HH    12小时制/24小时制
mm        代表分钟
ss        代表秒钟
a        代表AM/PM
————————————————


  
    SimpleDateFormat sdf =new SimpleDateFormat("yyyy/MM/dd HH-mm-ss");
	//创建Date
	Date date= new Date();
	//格式化date
	String str = sdf.format(date);//输出按构造的格式输出
	//解析(将字符串转成日期)
	Date date2 = sdf.parse("1990/05/01 06-36-25");//必须按照构造的格式来写入。
	//输出date2
    System.out.println(sdf.format(date2.getTime()));

六、Math类

求绝对值    Math.abs(-10) --> 10
四舍五入取整    Math.round(2.6) --> 3
求两个数最大值    Math.max(2,10) --> 10
求两个数最小值    Math.min(2,10) --> 2
随机数    Math.random() --> 0 ~ 1之间任意小数
求平方根    Math.sqrt(16) – > 4
求幂    Math.pow(2,3) – > 8


七、Random类

        1.构造:

    Random random = new Random();

         2.获得随机数:

random.nextInt(100); //0 ~ 100间随机整数

八、System 类

        

    //arraycopy:数组的复制
		//src:原数组
		//srcPos:从哪个位置开始复制
		//dest:目标数组
		//destPos:目标数组的位置
		//length :复制的长度
		arraycopy(src,srcPos,dest,destPos,length);
		int [] arr ={20,18,15,8,35,26,45,90};
		int [] dest=new int [8];
		System.arraycopy(arr,0,dest,0,arr.length);
	//currentTimeMillis()  返回从1970年1月1日 到现在的毫秒数
	//用于计时
 		long start = System.out.currentTimeMillis();
		String string="";
		for(int i=0;i<99999;i++){
			string+=i;
		}
		System.out.println(string);
		long end = System.out.currentTimeMillis();
		System.out.println("用时:"+(end - start));
	//gc() 告诉垃圾回收器回收垃圾
	//System.exit(0) 退出jvm  后面代码不会执行

  • 15
    点赞
  • 153
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Ai_Wu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值