2021-10-27 迈向程序猿的第十五步

目录

一.包装类

1.1 装箱与拆箱

1.2 包装类的用法

二. String类

2.1 String的创建

2.2 String的常用方法

 三.可变字符串

四.BigInteger与BigDecimal

4.1 BigDecimal

4.2 BigInteger

五.Math与Random

5.1 Math类

5.2 Random类

六.日期类

6.1 Date类(重点)

6.2 日历类

6.3 日期格式类

七. System类

       


一.包装类

1.1 装箱与拆箱

//java语言特性: 面向对象
//为了承诺java中一切皆为对象,八大基本类型需要有对应的包装类的类型,
//包装类是一个引用类型,具备了面向对象的特点
//int--->Integer
//char-->Character
//其余只需首字母大写就变成了包装类

//基本类型与包装类的转换
public class Test1 {
	public static void main(String[] args) {
		//------装箱:基本类型转包装类-------
		int a = 3;
		Integer integer = new Integer(a);  //构造器方式
		integer = Integer.valueOf(a);      //静态方法方式
		System.out.println(integer);       //Integer重写了toString
		
		//-------拆箱:包装类转基本类型--------
		a = integer.intValue();
		System.out.println(a);
		
		//---------自动装箱------------
		Integer integer2 = a;
		int b = integer2;    //自动拆箱
		//注意:自动装箱和拆箱的本质依然需要通过手动装箱和拆箱,只不过系统帮我们做了
		//查看反编译工具
		
		
		//其他类型与数字字符串的操作
		//1.其他类型转字符串
		String s = integer2+"";
		
		//2.字符串转基本类型
		int c = Integer.parseInt(s);
		double d = Double.parseDouble(s);
		
		//3.字符串转包装类型
	 	Integer integer3 = new Integer(s);
		
	}
}

1.2 包装类的用法

//包装类的用法
public class Test2 {
	public static void main(String[] args) {
		Integer integer = new Integer(10);
		Integer integer2 = new Integer(10);
		System.out.println(integer==integer2);  //false  比较地址
		
		//valueOf:传入的参数为-128~127之间,那么预先给定了空间;所以每次传的值一致,则是同一个地址
		Integer integer3 = Integer.valueOf(10);
		Integer integer4 = Integer.valueOf(10);
		System.out.println(integer3==integer4); //true
		
		Integer integer5 = Integer.valueOf(300);
		Integer integer6 = Integer.valueOf(300);
		System.out.println(integer5==integer6); //false
	}
}

二. String类

2.1 String的创建

//字符串类的创建:
public class CreateTest {
	public static void main(String[] args) {
		String a = "hello";  //直接赋值创建对象
		String aa  = "hello";
		System.out.println(a==aa);  //true
		
		String b = new String("hello");
		String bb = new String("hello");
		System.out.println(b==bb);  //false
		
		//一般比较字符串相同,使用equals--比较内容
		System.out.println(b.equals(bb)); //true
		
	}
}

2.2 String的常用方法

//String类: 不可变字符串
//不可变字符串:调用任何方法,不会改变原对象的值
public class MethodTest {
	public static void main(String[] args) {
		String a = "hello";
		System.out.println(a.charAt(0));  //获取该下标的字符,下标从0开始
		
		String b = a.concat("world");  //字符串拼接--helloworld
		System.out.println(b);  //hello
		
		System.out.println(a.contains("hel"));    //是否包含子串
		
		char[] c = a.toCharArray();  //将字符串转字符数组返回
		System.out.println(Arrays.toString(c));
		
		System.out.println(a.length());  //求长度
		System.out.println(a.indexOf("ll"));  //根据传入的子串,得到下标
		System.out.println(a.toUpperCase());  //小写转大写
		System.out.println(a.startsWith("h")); //true
		System.out.println(a.startsWith("hello"));  //true
		System.out.println("  h e ".trim());  //去除左右空格
		
		String d = "暴力的黄色小说,很暴力";
		//replace:完全匹配的替换
		System.out.println(d.replace("暴力", "xx")); //
		//replaceAll:正则表达式替换,可以完全匹配也可以按规则;
		//例如: 手机号替换;规则:1开头,长度11,必须都数字   dd13833388833ff
		System.out.println(d.replaceAll("暴力", "xx")); //
		
		String[] dd = d.split(",");  //按某个字符串进行分割,返回字符串数组
		System.out.println(Arrays.toString(dd));
		
		System.out.println(a.substring(1)); //从下标1开始提取子串到最后
		System.out.println(a.subSequence(1, 3)); //包括起始下标,不包括终止下标
	}
}

 三.可变字符串

//可变字符串(StringBuffer、StringBuilder)
//调用任何方法,可以改变原对象的值
//有了不可变字符串,为什么需要可变字符串?   提升性能

//StringBuffer、StringBuilder区别(扩展先了解)
//在使用上都是一样的,只是StringBuffer加了锁,更安全,性能低;
//StringBuilder没加锁,不安全,性能高
//应用场景:
//在单线程(一条执行通道)中倾向用StringBuilder,性能更好,因为不存在不安全的情况
//在多线程中倾向用StringBuffer,考虑安全为主
public class Test1 {
	public static void main(String[] args) {
		//可变字符串的创建
		StringBuilder sb = new StringBuilder("hello");
		//StringBuilder sb2 = "dd"; //注意:不能直接赋值字符串
		sb.append("world");  //字符串追加
		System.out.println(sb);
		
		//StringBuilder与String性能PK---通过反编译工具查看
		long start = System.currentTimeMillis();
		//String a = "";
		for(int i=0;i<10000;i++) {
			//a += i;
			sb.append(i);
		}
		System.out.println(System.currentTimeMillis()-start);
		
		System.out.println(sb.toString()); //转字符串返回
		
		//System.out.println(sb.reverse());  //字符串反转
		
		System.out.println(sb.delete(3, 6)); //删除指定起始下标和终止下标字符串,不包括终止下标
		
	}
}

四.BigInteger与BigDecimal

4.1 BigDecimal

//BigDecimal:用于存储比double更精确的值
public class BigDecimailTest {
	public static void main(String[] args) {
		double a = 0.1;
		double b = 0.09;
		System.out.println(a-b);
		
		BigDecimal big1 = new BigDecimal("0.1");
		BigDecimal big2 = new BigDecimal("0.09");
		
		System.out.println(big1.subtract(big2));  //减法
		System.out.println(big1.add(big2));       //加法
		System.out.println(big1.multiply(big2));  //乘法
		
		//1.1111111111111
		//注意:除法有除不尽的情况,一定后面再加2个参数; 1.保留几位  2.取值模式
		//除法取值模式:有向上取,向下取,四舍五入
		System.out.println(big1.divide(big2, 2, BigDecimal.ROUND_HALF_UP));    //除法
	}
}

4.2 BigInteger

//扩展了解:BigInteger比int存储的值更大
public class BigIntegerTest {
	public static void main(String[] args) {
		System.out.println(Integer.MAX_VALUE);  //2147483647
		System.out.println(Integer.MAX_VALUE+1);  //-2147483648
		
		BigInteger big1 = new BigInteger(Integer.MAX_VALUE+"");//21474836471
		BigInteger big2 = new BigInteger("1");
		System.out.println(big1.add(big2));  //2147483648
	}
}

五.Math与Random

5.1 Math类

//Math类: 数据的工具类 ,里面提供了很多数学计算方面的静态方法
public class MathTest {
	public static void main(String[] args) {
		System.out.println(Math.ceil(1.2));  //2.0  向上取整
		System.out.println(Math.ceil(1.8));  //2.0
		
		System.out.println(Math.floor(1.2)); //1.0 向下取整
		System.out.println(Math.floor(1.8)); //1.0
		
		System.out.println(Math.round(1.2)); //1  四舍五入
		System.out.println(Math.round(1.8)); //2
		
		
		//Math.random()---0.0~1.0之间,不包括1.0
		//案例:随机出来75~100的数
		int num =(int)(Math.random()*26)+75;
		System.out.println(num);
	}
}

5.2 Random类

//Random类: 专业求随机数的类
public class RandomTest {
	public static void main(String[] args) {
		
		Random random = new Random();  //真随机
		//Random random = new Random(2); //伪随机
		//根据传入参数进行随机:例如 5--0到4之间随机
		for(int i=0;i<5;i++) {
			System.out.print(random.nextInt(5)+"\t");
		}
		
		//案例:随机出来75~100的数
		int num = random.nextInt(26)+75;
		System.out.println(num);
		
	}
}

六.日期类

6.1 Date类(重点)

//Date类: 日期类
//打印日期为格林威治时间格式
public class DateTest {
	public static void main(String[] args) {
		Date date = new Date();
		System.out.println(date);
		System.out.println(date.getTime());
		
		//Date date2 = new Date(1000); //从1970年开始+1秒
		Date date2 = new Date(date.getTime());
		System.out.println(date2);
	}
}

6.2 日历类

//日历类: abstract抽象类     用于求时间的类
public class CalendarTest {
	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		//System.out.println(Calendar.YEAR); //2021?
		
		System.out.println(calendar.get(Calendar.YEAR));  //2021
		System.out.println(calendar.get(Calendar.MONTH)+1); //10  0~11代表月份
		System.out.println(calendar.get(Calendar.DAY_OF_MONTH));  //27
		
		calendar.set(calendar.YEAR, 2000);  //设置年份
		System.out.println(calendar.get(Calendar.YEAR));  //2000
		
		calendar.add(Calendar.YEAR, 20);  //给年份增加数量
		System.out.println(calendar.get(Calendar.YEAR));  //2020
		
	}
}

6.3 日期格式类

//SimpleDateFormat:带格式的日期类---配合Date使用
public class SimpleDateFormatTest {
	public static void main(String[] args) throws ParseException {
		//实例化日期格式类,传入日期格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//传入Date--->转字符串输出
		System.out.println(sdf.format(new Date()));
		
		String strDate = "2009-08-09 08:10:08";
		//传入字符串-->根据日期格式返回Date对象
		Date date = sdf.parse(strDate);
		System.out.println(date);
	}
}

七. System类

//System类: 系统类,用于进行系统操作的工具类---静态方法
public class Test1 {
	public static void main(String[] args) {
		//System.exit(0);   //退出
		
		int[] a = {1,3,5};
		int[] b = new int[a.length+3];  //扩容输入
		System.arraycopy(a, 0, b, 0, a.length);
		System.out.println(Arrays.toString(b));
		
		//获取系统的属性对象:键值对形式出现的
		Properties properties = System.getProperties();
		System.out.println(properties); //{key=value,key2=value,,,}
		
		System.out.println("========================");
		
		//可以根据key获取value,key往往为已知且是字符串形式
		System.out.println(properties.getProperty("sun.boot.library.path"));
	}
}

       

(=-=,今天各种常规函数套餐,直接吃饱饱啊!得好好消化消化下~)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值