包装类概述,缓冲字符串,BigDecimal

这篇博客详细介绍了Java中的包装类,重点讲解了Integer类的定义、对象创建及转换方法。同时,深入探讨了字符串String的特性、创建方式及常用方法。此外,还介绍了线程安全的缓冲字符串StringBuffer,以及BigDecimal类在高精度计算中的作用。最后,提到了Math、Random、Date和SimpleDateFormat等实用类的使用。
摘要由CSDN通过智能技术生成

Day 16

一、包装类(掌握)

1.1 定义

  • 基本类型数据的对应的引用类型数据
  • 基本类型数据 有八种,包装类也有八种

1.2 包装类

byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

二、Integer(掌握)

2.1 定义

  • Integer 类在对象中包装了一个基本类型 int 的值。
  • Integer 类型的对象包含一个 int 类型的字段。
  • 该类提供了多个方法,能在 int 类型和 String 类型之间互相转换

2.2 创建对象

构造方法摘要 
Integer(int value) 
          构造一个新分配的 Integer 对象,它表示指定的 int 值。 
Integer(String s) 
          构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。 

package com.qf.pack;

public class Demo02 {
	public static void main(String[] args) {
		/**
		 * 构造方法摘要 
			Integer(int value) 
			          构造一个新分配的 Integer 对象,它表示指定的 int 值。 
			Integer(String s) 
			          构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。 
		 */
		Integer i1 = new Integer(110);
		System.out.println(i1);
		
		Integer i2 = new Integer("220");
		System.out.println(i2);
		System.out.println(i2.getClass().getName());
		
		// 创建Integer对象的时候,如果传入的是字符串,那必须是一个数字字符串
		Integer i3 = new Integer("a");
		System.out.println(i3);
	}
}

2.3 常用的转换的方法

package com.qf.pack;

public class Demo03 {
	public static void main(String[] args) {
		/**
		 * int ==> Integer
		 * Integer ==> int
		 * 
		 * String ==> Integer
		 * Integer ==> String
		 * 
		 * int ==> String
		 * String ==> int
		 */
		// int ==> Integer
		Integer i1 = new Integer(120);
		System.out.println(i1);
		
		Integer i2 = Integer.valueOf(220);
		System.out.println(i2);
		
		// Integer ==> int
		Integer i3 = new Integer(330);
		int ii3 = i3.intValue();
		System.out.println(ii3);
		
		// String ==> Integer
		Integer i4 = new Integer("440");
		System.out.println(i4);
		
		Integer i5 = Integer.valueOf("550");
		System.out.println(i5);
		
		// Integer ==> String
		String s5 = i5.toString();
		System.out.println(s5);
		System.out.println(s5.getClass().getName());
	}
}

2.4 自动装箱和拆箱

package com.qf.pack;

public class Demo05 {
	public static void main(String[] args) {
		// int ==> Integer
		Integer i1 = new Integer(110);
		
		// Integer ==> int
		int ii1 = i1.intValue();
		
		// 自动类型转换,Int==>integer,自动装箱
		Integer i2 = 220;
		System.out.println(i2);
		System.out.println(i2.getClass().getName());
		
		// Integer ==> int,自动拆箱
		int ii2 = i2;
		System.out.println(ii2);
	}
}

三、字符串String(掌握)

3.1 定义

  • java中所有的字符串都是此类的实例
  • 字符串是常量,一旦确定不可修改
package com.qf.str;

public class Demo01 {
	public static void main(String[] args) {
		String str = "abc";
		char data[] = {'a', 'b', 'c'};
		String str1 = new String(data);
		
		System.out.println(str);
		System.out.println(str1);
		
		System.out.println(str.equals(str1));
		System.out.println(str == str1);
		
		// 字符串是常量
		String s1 = "Hello";
		System.out.println(s1.hashCode());
		
		s1 += "World";
		System.out.println(s1.hashCode());
		
	}
}

3.2 创建字符串对象

  • 使用byte数组创建
package com.qf.str;

public class Demo02 {
	public static void main(String[] args) {
		/**
		 * 通过byte数组创建
		 * 通过char数组创建
		 * 通过int数组创建
		 * 通过字符串创建
		 */
		
		byte[] bytes = new byte[] {65,66,67,68,69};
		System.out.println(bytes);
		// 基于数字对应的ASCII表中的字符
		String sb = new String(bytes);
		System.out.println(sb);
		
		String sb1 = new String(bytes, 1, 2);
		System.out.println(sb1);
		
	}
}

  • 使用char数组创建
package com.qf.str;

public class Demo03 {
	public static void main(String[] args) {
		/**
		 * 字符数组==》字符串
		 */
		char[] chars = new char[] {'床','前','明','月','光',','};
		System.out.println(chars);
		
		System.out.println(chars.getClass().getName() + "@" + chars.hashCode());
		
		String sc1 = new String(chars);
		System.out.println(sc1);
		
		String sc2 = new String(chars, 1, 3);
		System.out.println(sc2);
		
	}
}

  • 使用int数组创建
package com.qf.str;

public class Demo04 {
	public static void main(String[] args) {
		/**
		 * 数字数组==》字符串
		 * 
		 * 字符串==》字符串
		 */
		
		int[] arr = {97,98,99,100,101};
		String si1 = new String(arr, 0, arr.length);
		System.out.println(si1);
		
		String ss = new String("Hello");
		System.out.println(ss);
	}
}

3.3 字符串常用方法

package com.qf.str;

import java.util.Arrays;

public class Demo05 {
	public static void main(String[] args) {
		String s = "疑是地上霜。";
		System.out.println(s);
		
		// 会把两个字符拼接产生一个新的字符串
		String ss = s.concat("\n举头望明月,\n低头思故乡");
		System.out.println(s);
		System.out.println(ss);
		
		// 替换
		String str = "日照香炉生紫烟,遥看瀑布挂前川。";
		String str1 = str.replace('挂', '钉');
		System.out.println(str);
		System.out.println(str1);
		
		String str2 = "唧唧复唧唧,木兰当户织。";
		String str22 = str2.replaceAll("唧", "鸡");
		System.out.println(str22);
		
		// 分割字符串
		String str3 = "啦啦啦,啦啦啦,我是卖报的小行家。";
		System.out.println(str3);
		
		String[] ret = str3.split("啦",3);
		System.out.println(Arrays.toString(ret));
		
		// 字符串转换成数组
		String str4 = "ABCDE";
		byte[] bytes = str4.getBytes();
		System.out.println(Arrays.toString(bytes));
		
		char[] cs = str4.toCharArray();
		System.out.println(cs);
		
		// 从字符串中截取内容填充数组
		char[] charss = new char[5];
		str4.getChars(0, 3, charss, 2);
		System.out.println(charss);
	}
}	
package com.qf.str;

public class Demo06 {
	public static void main(String[] args) {
		/**
		 * charAt
		 * indexOf
		 * startsWith
		 * endsWith
		 * toUpperCase
		 * toLowerCase
		 */
		
		String str1 = "张三丰";
		boolean ret = str1.charAt(0)=='张' ? true : false;
		String str11 = str1.charAt(0)=='张' ? "姓张" : "不姓张";
		
		int indexOf = str1.indexOf("三丰");
		System.out.println(indexOf);
		
		System.out.println(str1.startsWith("张"));
		
		String str2 = "aBcDeFg";
		String str22 = str2.toLowerCase();
		System.out.println(str22);
		
		String str222 = str2.toUpperCase();
		System.out.println(str222);
	}
}

四、缓冲字符串(掌握)

4.1 定义

  • 线程安全的可变字符序列。
  • 一个类似于 String 的字符串缓冲区,通过某些方法调用可以改变该序列的长度和内容。
  • StringBuffer 上的主要操作是 appendinsert 方法,可重载这些方法,以接受任意类型的数据。

4.2 创建对象

package com.qf.sb;

public class Demo01 {
	public static void main(String[] args) {
		/**
		 * StringBuffer
		 * 	StringBuffer()
		 * 	StringBuffer(String str)
		 */
		
		StringBuffer sb1 = new StringBuffer();
		System.out.println(sb1);
		System.out.println(sb1.length());
		System.out.println(sb1.capacity());
		
		StringBuffer sb2 = new StringBuffer("床前明月光");
		System.out.println(sb2);
		System.out.println(sb2.getClass().getName());
		System.out.println(sb2.length());
		System.out.println(sb2.capacity());
		
	}
}

4.3 插入数据的方法

package com.qf.sb;

public class Demo02 {
	public static void main(String[] args) {
		/**
		 * 增
		 * 	append
		 * 	insert
		 * 删
		 * 改
		 * 查
		 */
		
		StringBuffer sb1 = new StringBuffer();
		sb1.append("锄禾日当午,");
		sb1.append("汗滴禾下土.");
		sb1.append("谁知盘中餐,");
		sb1.append("粒粒皆辛苦.");
		
		System.out.println(sb1);
		System.out.println(sb1.length());
		System.out.println(sb1.capacity());
		
		StringBuffer sb2 = new StringBuffer("浔阳江头夜送客,枫叶荻花秋瑟瑟,主人下马客在船,举杯欲饮无管弦.");
		System.out.println(sb2);
		
		sb2.insert(0, true);
		System.out.println(sb2);
		
		sb2.insert(0, 0.5);
		System.out.println(sb2);
		
		sb2.insert(10, "《琵琶行》");
		System.out.println(sb2);
	}
}

4.4 删除的方法

package com.qf.sb;

public class Demo03 {
	public static void main(String[] args) {
		/**
		 *   StringBuffer delete(int start, int end) 
			          移除此序列的子字符串中的字符。 
			 StringBuffer deleteCharAt(int index) 
			          移除此序列指定位置的 char。 
		 */
		
		StringBuffer sb1 = new StringBuffer("浔阳江头夜送客,枫叶荻花秋瑟瑟,主人下马客在船,举杯欲饮无管弦.");
		System.out.println(sb1);
		
		sb1.delete(24, 32);
		System.out.println(sb1);
		
		sb1.deleteCharAt(0);
		System.out.println(sb1);
	}
}

4.5 修改的方法

package com.qf.sb;

public class Demo04 {
	public static void main(String[] args) {
		/**
		 *   StringBuffer replace(int start, int end, String str) 
			          使用给定 String 中的字符替换此序列的子字符串中的字符。 
			 StringBuffer reverse() 
			          将此字符序列用其反转形式取代。 
			 void setCharAt(int index, char ch) 
			          将给定索引处的字符设置为 ch。 
			 void setLength(int newLength) 
			          设置字符序列的长度。 
			 void trimToSize() 
			          尝试减少用于字符序列的存储空间。 
		 */
		StringBuffer sb1 = new StringBuffer("浔阳江头夜送客,枫叶荻花秋瑟瑟,主人下马客在船,举杯欲饮无管弦.");
		System.out.println(sb1);
		
		sb1.replace(0, 8, "《琵琶行》");
		System.out.println(sb1);
		
		sb1.reverse();
		System.out.println(sb1);
		
		sb1.reverse();
		System.out.println(sb1);
		
		System.out.println(sb1.capacity());
		sb1.trimToSize();
		System.out.println(sb1.capacity());
		
		sb1.setLength(10);
		System.out.println(sb1);
	}
}

4.6 查找的方法

package com.qf.sb;

public class Demo05 {
	public static void main(String[] args) {
		/**
		 *   int capacity() 
			          返回当前容量。 
			 char charAt(int index) 
			          返回此序列中指定索引处的 char 值。 
			 int indexOf(String str, int fromIndex) 
			          从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。 
		 */
		StringBuffer sb1 = new StringBuffer("浔阳江头夜送客,枫叶荻花秋瑟瑟,主人下马客在船,举杯欲饮无管弦.");
		System.out.println(sb1);
		
		System.out.println(sb1.charAt(10));
		
		System.out.println(sb1.indexOf("荻",11));
		
	}
}

4.7 String 和 StringBuffer互转的方法

package com.qf.sb;

public class Demo06 {
	public static void main(String[] args) {
		// 字符串==》字符串缓冲区
		StringBuffer stringBuffer = new StringBuffer("清明上河图");
		
		// 字符串缓冲区===》字符串
		String string = stringBuffer.toString();
	}
}

五、BigDecimal(了解)

5.1 定义

  • 不可变的、任意精度的有符号十进制数。
  • 解决运算中出现的小数位不准确的问题

5.2 创建和使用

package com.qf.bd;

import java.math.BigDecimal;

public class Demo03 {
	public static void main(String[] args) {
		BigDecimal bd1 = new BigDecimal("1.0");
		BigDecimal bd2 = new BigDecimal("0.9");
	
		System.out.println(bd1.add(bd2));
		System.out.println(bd1.subtract(bd2));
		System.out.println(bd1.multiply(bd2));
		// 需要传入参数,保留的小数位数,取小数值的模式
		System.out.println(bd1.divide(bd2,2,BigDecimal.ROUND_HALF_UP));
	}
}

package com.qf.bd;

import java.math.BigDecimal;

public class Demo02 {
	public static void main(String[] args) {
		BigDecimal bd1 = new BigDecimal(1.0);
		BigDecimal bd2 = new BigDecimal(0.9);
		
		System.out.println(bd1);
		System.out.println(bd2);
		
		System.out.println(bd1.add(bd2));
		System.out.println(bd1.subtract(bd2));
		System.out.println(bd1.multiply(bd2));
		// System.out.println(bd1.divide(bd2));
		
		System.out.println("============================");
		
		BigDecimal bd3 = new BigDecimal("1.0");
		BigDecimal bd4 = new BigDecimal("0.9");
		System.out.println(bd3);
		System.out.println(bd4);
		System.out.println(bd3.add(bd4));
		System.out.println(bd3.subtract(bd4));
		System.out.println(bd3.multiply(bd4));
		
	}
}

六、Math(掌握)

6.1 定义

  • Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
  • Math类没有构造方法,但是Math类中的方法基本都是静态的方法,可以直接调用

6.2 Math常用方法

package com.qf.math;

public class Demo01 {
	public static void main(String[] args) {
		System.out.println(Math.abs(3.3));
		System.out.println(Math.abs(-3.3));
		
		System.out.println(Math.ceil(6.6));
		System.out.println(Math.floor(6.6));
		
		System.out.println(Math.sqrt(9));
		System.out.println(Math.cbrt(8));
		
		System.out.println(Math.round(5.5));
		
		for (int i = 0; i < 100; i++) {
			System.out.println(Math.random());
		}
		
		// 输出33-66之间的随机数
		
	}
}

七、Random(掌握)

7.1 定义

  • 此类的实例用于生成伪随机数流

7.2 常用方法

  • nextInt(int i)
    • 获取0–i之间的随机数,不包含i
package com.qf.math;

import java.util.Random;

public class Demo02 {
	public static void main(String[] args) {
		Random  r = new Random();
		for (int i = 0; i < 10; i++) {
			System.out.println(r.nextInt(100));
		}
		
		// 输出31--82之间的随机数
	}
}

八、Date(掌握)

8.1 定义

  • Date 表示特定的瞬间,精确到毫秒。
package com.qf.date;

import java.util.Date;

public class Demo01 {
	public static void main(String[] args) {
		/**
		 *  Date() 
          		分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。
          	Date(long date) 
          		分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数。
		 */
		System.out.println(System.currentTimeMillis());
		
		Date date = new Date();
		System.out.println(date);
		
		Date date2 = new Date(1591602993205L);
		System.out.println(date2);
		
		System.out.println(date.getYear());
		System.out.println(date.getMonth());
		System.out.println(date.getHours());
	}
}

九、SimpleDateFormat(掌握)

9.1 定义

  • SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类

9.2 案例

package com.qf.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo02 {
	public static void main(String[] args) throws ParseException {
		/**
		 * 	SimpleDateFormat() 
			          用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。 
			SimpleDateFormat(String pattern) 
			          用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。 
		 */
		Date date = new Date();
		System.out.println(date);
		
		// 2020年6月8日 16时08分08秒
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		// 格式化date对象,得到字符串
		String time = sdf.format(date);
		System.out.println(time);
		
		// 解析字符串,得到日期对象
		Date date2 = sdf.parse("2020年6月8日 16时08分08秒");
		System.out.println(date2);
		System.out.println(date2.getTime());
		
		// 请输入你的生日,计算你已经活了多少天
		
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值