实用类,拆装箱

Math类

//π
double pi = Math.PI;


//自然对数e
double e = Math.E;


//0-9随机数
double v = Math.random() * 10;


//绝对值
int abs = Math.abs(-12);


//四舍五入
System.out.println(Math.round(12.5));
System.out.println(Math.round(12.4f));


//向下取整
System.out.println(Math.floor(12.9));


//向上取整
System.out.println(Math.floor(12.3));


//除以根号2
System.out.println(Math.sqrt(2));


//取对数
System.out.println(Math.log(e));
System.out.println(Math.log10(1));

拆装箱

//自动装箱成为new Integer(1);
Integer a = 1;
//标准的,使用构造方法
Integer b = new Integer(2);
Integer c = new Integer("3");
//使用valueOf方法
Integer d = Integer.valueOf(4);
Integer e = Integer.valueOf("5");
//使用parseInt方法
Integer.parseInt("7");
//拆箱
int f = new Integer(6);

intValue()是把Integer对象类型变成int的基础数据类型;
parseInt()是把String 变成int的基础数据类型;
Valueof()是把String 转化成Integer对象类型

特殊例子

/**
* byte范围在-128~127之间,会缓存在jvm中
*所以在Integer对象创建时,如果有相同值在缓存中
* 则返回原对象的地址,不会再创建新对象
*/
Integer i = 127;
Integer j = 127;
Integer k = new Integer(127);
Integer m = Integer.valueOf(127);
System.out.println(i==j);//true
System.out.println(i==k);//false
System.out.println(i==m);//true

Boolean

//当字符串为true时才是true(不区分大小写),其他都是false
Boolean boo = new Boolean("tRue");
System.out.println(boo);

Character

Character ch = new Character('a');

Float

/**
* Float
* 5.2f可以认为是float类型
* 但是2L,2l都不能看作是long类型
*/
Float f1 = new Float("5.2");
System.out.println(f1);
Double db = new Double("5.2f");
System.out.println(db);
Long lo = new Long("2");
System.out.println(lo);

没有String类型的构造方法

Date类

//当前时间
Date d1 = new Date();
System.out.println(d1);

SimpleDateFormat类

//用时间戳,从1970年1月1日至今的毫秒数
Date d2 = new Date(1600000000000L);
System.out.println(d2);

SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = fm.format(d1);
System.out.println(time);

Calendar类

Calendar ca = fm.getCalendar();
//获取最近一个使用format的日期
Date date = ca.getTime();
System.out.println(date);
System.out.println(ca.get(Calendar.DAY_OF_WEEK_IN_MONTH));

随机

Random r1 = new Random();
    //范围是int范围
    System.out.println(r1.nextInt());
    //范围是0-100
    System.out.println(r1.nextInt(100));


    //随机因子
    Random r2 = new Random(1);
    System.out.println(r2.nextInt(10));
}

String类

/**
* String类本身用final修饰,不能被继承
* 如果方法上用final修饰,不能被重写
* String类型底层存储,是char类型的常量数组
*/
String a = "abc";
String b = "abc";
String c = new String("abc");


/**
* String类的常用构造方法有,以String作参数,以byte[]数组作参数
* 以char[]数组作参数,char类型数组可以直接打印,默认转成字符串
*/
char[] d = {'a','b','c'};
System.out.println(d);
char[] e = {'a','b','c'};
String x = String.valueOf(e);
System.out.println(x);
System.out.println(a==x);
System.out.println(a.equals(x));
System.out.println("------------");
/**
* valueOf常用于接收数组
* toCharArray转成char数组
* trim左右去空格
* split切分成String数组,匹配正则表达式
* getBytes获取ASCII码值
* contains和startsWith都是是否包含,contains直接输入值,startsWith输入下标
* substring取子集,左闭右开
* indexOf对应字符的索引
* charAt对应下标的索引
* compareTo先按位比较,其次长度比较
* concat拼接字符串,基本等同于+
* replace替换字符
*/
String test = " abc,  123  ,hello ";
System.out.println(test);
System.out.println(test.trim());


String[] str = test.split(",| ");
System.out.println(Arrays.toString(str));


byte[] bytes = test.getBytes();
System.out.println(Arrays.toString(bytes));


System.out.println(test.contains("123"));
System.out.println(test.startsWith(",",5));
System.out.println(test.substring(5));
System.out.println(test.substring(5,10));//左闭右开
System.out.println(test.charAt(15));
System.out.println(test.replace('a', 'm'));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值