Java—Java中的常用类:Date、Math、Random、String-StringBuffer-StringBulider类

Date–时间日期类

Date: 日期类

SimpleDateFormat:格式化日期类

Calender:日历类

import java.text.ParseException;
import java.util.Calendar;

public class Test {
	/**
	 * 		1.Date + SimpleDateFormat 联合使用,获取我们想要的格式的日期时间信息
	 * 		2.Calendar获取单个日期信息
	 */
	public static void main(String[] args) throws ParseException {
        
        //Date类:获取当前时间
        Date date = new Date();
		//星期  月份   日期 时:分:秒	     时区   年份
		//Thu Mar 02 14:28:31 CST 2023
		System.out.println(date);
        
        
        
        //SimpleDateFormat类:格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		//将日期对象格式化成对应的字符串
		String dateTime = sdf.format(new Date());
		System.out.println(dateTime);
		//将字符串解析成日期对象
		Date date = sdf.parse("2023年03月02日 14:32:47");
		System.out.println(date);
		
        
        
		//获取日历对象
		Calendar c = Calendar.getInstance();
		
		int year = c.get(Calendar.YEAR);//获取当前年
		int month = c.get(Calendar.MONTH) + 1;//获取当前月
		int day = c.get(Calendar.DAY_OF_MONTH);//获取当前月的第几天
		int hour = c.get(Calendar.HOUR);//获取当前小时
		int minute = c.get(Calendar.MINUTE);//获取当前分钟
		int second = c.get(Calendar.SECOND);//获取当前秒
		
		System.out.println(year);
		System.out.println(month);
		System.out.println(day);
		System.out.println(hour);
		System.out.println(minute);
		System.out.println(second);
	}
}

Math类

Math类可以用来做一些数学运算

public class Test01 {
	/**
	 * 知识点:Math类
	 */
	public static void main(String[] args) {
		
		System.out.println("求次方:" + Math.pow(3, 2));//9.0
		System.out.println("求平方根:" + Math.sqrt(9));//3.0
		System.out.println("求绝对值:" + Math.abs(-100));//100
		System.out.println("向上取整(天花板):" + Math.ceil(1.1));//2.0
		System.out.println("向下取整(地板):" + Math.floor(1.9));//1.0
		System.out.println("四舍五入:" + Math.round(1.5));//2
		System.out.println("最大值:" + Math.max(10, 20));//20
		System.out.println("最小值:" + Math.min(10, 20));//10
		System.out.println("随机值0(包含)~1(排他):" + Math.random());
		
		//需求:随机出1~100的数字
		System.out.println((int)(Math.random()*100)+1);
	}
}

Math.abs():求绝对值的方法

该方法有可能出现负数

特殊情况:

Integer.MAX_VALUEInteger.MIN_VALUEint类型的最大和最小取值数;当Math.abs()中参数超出int类型的最大和最小取值范围的时候,Math.abs()就会出现负数的情况。
System.out.println(Math.abs(Integer.MAX_VALUE+1));//-2147483648
System.out.println(Math.abs(Integer.MIN_VALUE)); //-2147483648  

Random–随机类

常用于生成随机数:

public static void main(String[] args) {
		
		Random ran = new Random();
		
		System.out.println("随机出int取值范围内的数字:" + ran.nextInt());
		System.out.println("随机出double取值范围内的数字:" + ran.nextDouble());
		System.out.println("随机出boolean取值范围内的数字:" + ran.nextBoolean());
		System.out.println("随机出0~9的int数字:" + ran.nextInt(10));
	}
Math.random()和new Random()生成随机数的区别:

Math.random()生成的是0~1的double类型的随机数列;new Random()生成的是int取值范围内的随机数列

当new Random()中含有参数的时候,此参数作为种子给Random生成随机数的时候当作参数使用,此时生成的随机数就为固定值。

Math.random()和new Random()两者生成的随机数都是伪随机数,都是通过计算得来的。

String类

String类中的常用方法:

public class Test {
	public static void main(String[] args) {
		
		String str = "123abcDEF";
		
		str = str.concat("123");//在字符串末尾追加,并返回新的字符串
		str = str.substring(2);//从开始下标处截取到字符串末尾,并返回新的字符串
		str = str.substring(1, 7);//从开始下标(包含)处截取到结束下标(排他)处,并返回新的字符串
		str = str.toLowerCase();//转小写,并返回新的字符串
		str = str.toUpperCase();//转大写,并返回新的字符串
		
		str = "   123  ab c DEF   123     ";
		
		str = str.trim();//去除首尾空格,并返回新的字符串
		str = str.replace('c', 'C');//替换字符,并返回新的字符串
		str = str.replaceAll("DEF", "xxyyzz");//替换字符串,并返回新的字符串
		str = str.replaceAll(" ", "");//去除空格
		str = str.replaceFirst("x", "哈哈");//替换第一次出现的字符串,并返回新的字符串
		
		System.out.println("判断两个字符串内容是否相同(区分大小写):" + str.equals("123abC哈哈xyyzz123"));
		System.out.println("判断两个字符串内容是否相同(不区分大小写):" + str.equalsIgnoreCase("123abC哈哈xyyZZ123"));
		
		System.out.println("判断字符串是否以某个字符串开头:" + str.startsWith("123"));//true
		System.out.println("判断字符串是否以某个字符串结尾:" + str.endsWith("123"));//true
		
		System.out.println("获取子字符串第一次出现的下标:" + str.indexOf("123"));//0
		System.out.println("获取子字符串最后一次出现的下标:" + str.lastIndexOf("123"));//13
		
		System.out.println("获取指定下标上的元素:" + str.charAt(5));//C
		
		System.out.println(str);//123abC哈哈xyyzz123

		//将其他类型转换为String类型
		System.out.println(String.valueOf(100));
		System.out.println(String.valueOf(123.123));
		System.out.println(String.valueOf(true));
		System.out.println(String.valueOf('a'));
		System.out.println(String.valueOf(new char[]{'a','b','c'}));
		
		//简化版本
		System.out.println(100 + "");
		System.out.println(123.123 + "");
		System.out.println(true + "");
		System.out.println('a' + "");
		
	}
}

StringBuffer类

StringBuffer类中的常用方法:

public class Test {

	public static void main(String[] args) {
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("123abc");//在末尾追加字符串
		buffer.append("DEF123");
		buffer.insert(6, "C");//在指定位置插入字符
		buffer.setCharAt(5, 'C');//替换指定下标上的字符
		buffer.replace(3, 5, "AB");//替换开始下标到指定下标(排他)的字符串
		buffer.deleteCharAt(5);//删除指定坐标的字符
		buffer.delete(0, 3);//删除开始下标到指定下标(排他)的字符串
		buffer.reverse();//反转字符串
		
		System.out.println(buffer);
	}

}

StringBuilder类

StringBuilder类中的常用方法:和StringBuffer一样

public class Test {

	public static void main(String[] args) {
		
		StringBuilder builder = new StringBuilder();
		builder.append("123abc");//在末尾追加字符串
		builder.append("DEF123");//在末尾追加字符串
		builder.insert(6, "小白");//在指定下标上插入字符串
		builder.setCharAt(5, 'C');//替换指定下标上的字符
		builder.replace(10, 13, "学java");//替换开始下标(包含)处到结束下标(排他)处的字符串
		builder.deleteCharAt(5);//删除指定下标上的字符
		builder.delete(3, 13);//删除开始下标(包含)处到结束下标(排他)处的字符串
		builder.reverse();//反转字符串
		
		System.out.println(builder);
	
	}

}

String–StringBuffer–StringBuilder类区别

String类:

String:是不可变类, 即一旦一个String对象被创建, 包含在这个对象中的字符序列是不可改变的,直至该对象被销毁。String类是final类,不能有子类。

public final class String{ 
    private final char[] value; 
	} 
	String str = "abc";//final char[] value = ['a','b','c'];
	str = "abcd"; //final char[] value = ['a','b','c','d'];

StringBuffer/StringBuilder类:

StringBuffer/StringBuilder:代表可变的字符序列,称为字符串缓冲区

工作原理:预先申请一块内存,存放字符序列,如果字符序列满了,会重新改变缓存区的大小,以容纳更多的字符序列。

  1. String是不可变类,指的是String对象中value数组是常量
  2. 2.StringBuffer继承AbstractStringBuilder,StringBuilder继承AbstractStringBuilder,StringBuffer和StringBuilder所有的逻辑都是交给父类的,所以他们使用是一模一样
  3. StringBuffer、StringBuilder是可变类,指的是他们共同的父类(AbstractStringBuilder)中的values数组是变量(可以扩容)
  4. StringBuffer的方法中使用了synchronized去修饰,所以方法是线程安全的,但是有上锁、解锁的步骤所以比不上锁的StringBuilder效率更低,在多线程的情况下使用

string的面试题:

  1. 请问下列代码创建了几个String对象?
String str1 = "abc";
String str2 = "abc";
//创建了一个String对象(考点:常量池中的数据是唯一的)
  1. 请问下列代码创建了几个String对象?
String str1 = new String("abc");
String str2 = new String("abc");
//三个(new了两个,常量池中有个"abc")

String拼接字符串问题

public class Test {
    
	public static void main(String[] args) {

		String str1 = "abc";
		String str2 = "abc";
		System.out.println(str1 == str2);//true

		//两个常量直接拼接
		String str3 = "ab" + "c";
		System.out.println(str1 == str3);//true
		
		//两个常量直接拼接
		final String s1 = "ab";
		final String s2 = "c";
		String str4 = s1+s2;
		System.out.println(str1 == str4);//true
		
		//有变量字符串拼接的情况:底层会创建StringBuilder对象
		String s3 = "ab";
		String s4 = "c";
		String str5 = s3+s4;//底层实现:new    	 StringBuilder(String.valueOf(s3)).append(s4).toString();
		System.out.println(str1 == str5);//false
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值