常用类小结-1

常用类

1.封装-拆箱和装箱

封装

		// int 包装类
		Integer a1 = new Integer("123");
		Integer a2 = new Integer(11);
		System.out.println(a1);
		System.out.println(a2);
		
		// byte 包装类
		Byte b1 = new Byte("1");
		Byte b2 = new Byte((byte) 2);
		System.out.println(b1);
		System.out.println(b2);
		
		// short 包装类
		Short c1 = new Short("12");
		Short c2 = new Short((short) 12);
		System.out.println(c1);
		System.out.println(c2);
		
		// long 包装类
		Long d1 = new Long("100");
		Long d2 = new Long(100L);
		System.out.println(d1);
		System.out.println(d2);
		
		// float 包装类
		Float e1 = new Float("11.22");
		Float e2 = new Float(11.22F);
		Float e3 = new Float(11.23);
		System.out.println(e1);
		System.out.println(e2);
		System.out.println(e3);
		
		// double 包装类
		Double f1 = new Double("11.22");
		Double f2 = new Double(11.32);
		System.out.println(f1);
		System.out.println(f2);
		
		// char 包装类
		Character g1 = new Character('a');
		System.out.println(g1);
		
		// boolean 包装类
		Boolean h1 = new Boolean("ture");
		Boolean h2 = new Boolean(false);
		System.out.println(h1);
		System.out.println(h2);

拆箱和装箱
JDK1.5以后允许包装类和基本数据类型混合使用计算,这样书写更为方便
装箱:将基本数据类型自动转换为包装类
拆箱:将包装类自动转换为基本类型

// 拆箱-将对象转换为基本数据类型
public class Devanning {
	public static void main(String[] args) {
		Byte b1 = new Byte("123");
		byte b = b1.byteValue();
		System.out.println(b);
	}
}
// 装箱-将基本数据类型转换为对象
public class Encasement {
	public static void main(String[] args) {
		Byte b1 = Byte.valueOf((byte)110);
		Byte b2 = Byte.valueOf("10");
		System.out.println(b1);
		System.out.println(b2);
	}
}
public class Test {
	public static void main(String[] args) {
		Integer i = new Integer(2);
		Integer i1 = 230; // 将基本数据类型转换为包装类
		int num = i1; // 将包装类自动转换为基本数据类型
	}

2.基本数据类型与字符串的转换

1.将基本数据类型转换为字符串;
2.将字符串转换为基本数据类型;

		String byteStr = Byte.toString((byte)120);
		String shortStr = Short.toString((short)220);
		String intStr = Integer.toString(20);
		String longStr = Long.toString(454623L);
		String floatStr = Float.toString(3.3F);
		String doubleStr = Double.toString(220);
		String booleanStr = Boolean.toString(false);
		byte parseByte = Byte.parseByte("111");
		short parseShort = Short.parseShort("220");
		int parseInt = Integer.parseInt("230");
		long parseLong = Long.parseLong("1245");
		float parseFloat = Float.parseFloat("2356F");
		double parseDouble =Double.parseDouble("2356.0");
		boolean parseBoolean = Boolean.parseBoolean("abc");

3.Math类

// 自然对数
		System.out.println(Math.E);
		// 圆周率
		System.out.println(Math.PI);
		// 取绝对值
		System.out.println(Math.abs(-330));
		// 取最大值
		System.out.println(Math.max(100, 10));
		// 取最小值
		System.out.println(Math.max(99, 22));
		// 取整数
		// 向下取整
		System.out.println(Math.floor(3.56));
		// 向上取整
		System.out.println(Math.ceil(2.88));
		// 四舍五入
		System.out.println(Math.round(2.5));
		System.out.println(Math.round(2.4));
		// 随机数
		System.out.println(Math.random() * 100);

小游戏:猜数字,系统生成一个随机数,用户进行猜测如果猜大了或者猜小了给出提示,重新猜直到猜正确为止,输出猜测的次数和生成数。

Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个整数:");
		int num = sc.nextInt();
		int x = (int)( Math.random() * 100);
		int count = 1;
		while(x != num) {
			if(num > x) {
				System.out.println("输入的数大了请重新输入");
					num = sc.nextInt();
			}
			else {
				System.out.println("输入的数小了请重新输入");
					num = sc.nextInt();
			}
			count++;
		}
		System.out.println(count);
		System.out.println(num);

4.Random类

Random类提供了更多种数据类型随机数据的获取方式

import java.util.Random;
public class TestRandom {
	public static void main(String[] args) {
		Random rand = new Random();
		System.out.println(rand.nextInt());
		System.out.println(rand.nextInt(100));
	
		System.out.println(rand.nextBoolean());
		System.out.println(rand.nextFloat());
		System.out.println(rand.nextDouble());	
		
		Random ran1 = new Random(12);
		Random ran2 = new Random(12);
		System.out.println(ran1.nextInt());
		System.out.println(ran2.nextInt());
	}
}

5.String类

基本String方法

			int [] nums = {1,2,3,4,5};
			// 输出字符串数组的长度
			System.out.println(nums.length);
			String str1 = "abcdefg";
			// 输出字符串的长度
			System.out.println(str1.length());
			String str2 = "abcd";
			String str3 = "aBcD";
			// 比较字符串是相同
			System.out.println(str2.equals(str3));
			// 忽略大小写比较字符串是否相同
			System.out.println(str2.equalsIgnoreCase(str3));
			// 将字符串全部转换为大写
			System.out.println(str2.toUpperCase());
			// 将字符串全部转换为小写
			System.out.println(str3.toLowerCase());
			
			String str4 = "a";
			String str5 = "b";
			// 字符串的连接
			System.out.println(str4 + str5);
			System.out.println(str4.concat(str5));
			
			String str6 = "abcdabcd";
			// 截取字符在字符串中的下标位置(第一次出现的)
			System.out.println(str6.indexOf("f"));
			System.out.println(str6.indexOf(98)); // ASCII 码
			String str7 = "hello中国";
			System.out.println(str7.indexOf(20013)); // Unicode编码
			// 截取字符在字符串中的下标位置(最后一次出现的)
			System.out.println(str6.lastIndexOf("a"));
			System.out.println(str7.lastIndexOf("l"));
			System.out.println(str7.lastIndexOf(20013));
			
			String str8 = "abcdefghijklmn";
			// 从开始位置截取到末尾
			System.out.println(str8.substring(3)); 
			// 包前不包后
			System.out.println(str8.substring(3, 6));
			String str9 = "         a  b  c  d f    ";
			// 去除首尾的空格
			System.out.println(str9.trim());

分割

String song = "长亭外 古道边 芳草碧连天 晚风拂柳 笛声残 夕阳山外山";
		// 拆分字符串
		String [] strs = song.split(" "); 
		// 这里将以空格作为分割线分开后每一部分对应的有下标将以“长亭外”的下标为0以此类推
		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);

6.枚举

枚举的作用在于规范属性的取值。使用enum声明。
switch支持枚举类型

public static void main(String[] args) {
		Week week = Week.MON;
		switch (week) {
		case MON:
			System.out.println("周一");
			break;
		case TUE:
			System.out.println("周二");
			break;
		case WED:
			System.out.println("周三");
			break;
		case THUR:
			System.out.println("周四");
			break;
		case FRI:
			System.out.println("周五");
			break;
		case SAT:
			System.out.println("周六");
			break;
		case SUN:
			System.out.println("周日");
			break;
		default:
			System.out.println("地球的一周只有七天");
			break;
		}
public enum Week {
		MON,TUE,WED,THUR,FRI,SAT,SUN
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值