奉天承运,博主诏曰:
枚举类型
代码演示
首先新建一个枚举类Course
public enum Course {
L1,L2,L3;
}
然后建一个测试类TestCourse实现并调用枚举
public class TestCourse {
public void show(Course course){
switch (course){
case L1:
System.out.println("大数据开发工程师");
break;
case L2:
System.out.println("大数据挖掘工程师");
break;
case L3:
System.out.println("大数据架构师");
break;
default:
System.out.println("输入有误");
}
}
public static void main(String[] args) {
TestCourse ts=new TestCourse();
ts.show(Course.L1);
ts.show(Course.L2);
ts.show(Course.L3);
}
}
包装类
代码演示
/*包装类型
* 基本数据类型byte,short,int,long,float,double,char boolean
* 对应包装类型Byte,Short,Integer,Long,Float,Double,Character Boolean
*/
public class TestPackages {
public static void main(String[] args) {
Integer a=1;//int a=1;自动装箱成为new Integer(1);
Integer b=new Integer(1);//标准的,使用构造方法
Integer c=new Integer("1");
Integer d=Integer.valueOf(1);//使用valueOf方法
Integer e=Integer.valueOf("1");
Integer g=Integer.parseInt("1");
int f=new Integer(1);//拆箱
//特殊的例子:
// 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
//byte范围在-128~127之间,会缓存在jvm中
//所以在Integer对象创建时,如果有相同的值在缓存中
//则返回原对象地址,不会再创建新对象
Integer i=128;//过了byte常量池的范围
Integer j=128;
Integer k=new Integer(128);
Integer m=Integer.valueOf(128);
System.out.println(i==j);//false
System.out.println(i==k);//false
System.out.println(i==m);//false
Boolean boo=new Boolean("tRUe");//不分大小写
System.out.println(boo);
Character ch=new Character('a');
Float f1=new Float("5.2");
System.out.println(f1);
Double db=new Double("5.2f");//5.2f可以认为是float类型
System.out.println(db);
Long lo=new Long("2L");//2L在这里不能看作Long类型的2,所以会报错
System.out.println(lo);
}
}
Math类
代码演示
public class TestMath {
public static void main(String[] args) {
double pi=Math.PI;
double e=Math.E;
double v=Math.random();//0~1之间的随机数
int a =Math.abs(-123);//绝对值
long b=Math.round(12.5);//四舍五入
int c=Math.round(12.5f);
Math.floor(12.9);//向下取整
Math.ceil(12.3);//向上取整
Math.sqrt(2);//根号
Math.log(e);//对数,以e为底
Math.log10(1);
}
}
Date和Calendar类
代码演示
import java.util.Calendar;
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
Date d=new Date();//获取当前时间
System.out.println(d);
Date d1=new Date(1608336000000L);//用时间戳(1970年1月1日至今的毫秒数)
System.out.println(d1);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=format.format(d);
System.out.println(time);
long timestamp = d.getTime();//获取时间戳
long yes = timestamp-24 * 3600 * 1000;
Date d2=new Date(yes);
System.out.println(format.format(d2));
Calendar calendar=format.getCalendar();
Date date=calendar.getTime();//获取最近一个使用format的日期
System.out.println(date);
System.out.println(Calendar.APRIL);
}
}
钦此。