常用类小结

编程入门 行业动态 更新时间:2024-10-07 02:24:17

常用类<a href=https://www.elefans.com/category/jswz/34/1769750.html style=小结"/>

常用类小结

常用类

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
}

更多推荐

常用类小结

本文发布于:2024-02-13 07:09:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757338.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小结   常用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!