北京综合网站建设报价/威海网站制作
- 包装类
装箱与拆箱
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);}
}
包装类的用法
//包装类的用法
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); //trueInteger integer5 = Integer.valueOf(300);Integer integer6 = Integer.valueOf(300);System.out.println(integer5==integer6); //false}
}
- String类(重点)
==String的创建
//字符串类的创建:
public class CreateTest {public static void main(String[] args) {String a = "hello"; //直接赋值创建对象String aa = "hello";System.out.println(a==aa); //trueString b = new String("hello");String bb = new String("hello");System.out.println(b==bb); //false//一般比较字符串相同,使用equals--比较内容System.out.println(b.equals(bb)); //true}
}
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"); //字符串拼接--helloworldSystem.out.println(b); //helloSystem.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")); //trueSystem.out.println(a.startsWith("hello")); //trueSystem.out.println(" h e ".trim()); //去除左右空格String d = "暴力的黄色小说,很暴力";//replace:完全匹配的替换System.out.println(d.replace("暴力", "xx")); ////replaceAll:正则表达式替换,可以完全匹配也可以按规则;//例如: 手机号替换;规则:1开头,长度11,必须都数字 dd13833388833ffSystem.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); //2147483647System.out.println(Integer.MAX_VALUE+1); //-2147483648BigInteger big1 = new BigInteger(Integer.MAX_VALUE+"");//21474836471BigInteger big2 = new BigInteger("1");System.out.println(big1.add(big2)); //2147483648}
}
5.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.0System.out.println(Math.floor(1.2)); //1.0 向下取整System.out.println(Math.floor(1.8)); //1.0System.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)); //2021System.out.println(calendar.get(Calendar.MONTH)+1); //10 0~11代表月份System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //27calendar.set(calendar.YEAR, 2000); //设置年份System.out.println(calendar.get(Calendar.YEAR)); //2000calendar.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"));}
}