为了方便操作基本数据类型,将其封装成了对象,在对象中定义了属性和行为丰富了该数据的操作。
public static void main(String[] args) {
Integer i1 = new Integer(123);
Integer i2 = new Integer("123");
Integer i3 = new Integer(128);
System.out.println(i1.equals(i2)); //true
System.out.println(i1); //123 Integer 重写了 toString
System.out.println(i1.compareTo(i3)); //-1
System.out.println(i1.compareTo(i2)); //0
System.out.println(i3.compareTo(i2)); //1
Integer[] iiIntegers = {i1,i2,i3};
//Integer==>int
int a = i1.intValue();
System.out.println(a); //123
//int==>Integer
Integer a1 = Integer.valueOf(a);
System.out.println(a1); //123
//String==>int
int a2 = Integer.parseInt("123");
System.out.println(a2); //123
//int==>String
String s1 = a2+"";
System.out.println(s1); //123
//String==>Integer
Integer a3 = new Integer("123");
System.out.println(a3); //123
//Integer==>String
String s2 = a3.toString();
System.out.println(s2); //123
}
//进制转换
System.out.println(Integer.toBinaryString(60));
System.out.println(Integer.toOctalString(60));
System.out.println(Integer.toHexString(60));
System.out.println(Integer.toString(60, 2));
System.out.println(Integer.parseInt("111100", 2));
详解Java的自动装箱与拆箱(Autoboxing and unboxing)
public static void main(String[] args) {
Integer integer = 23; //装箱 编译器自动调用valueOf方法
int i = new Integer(123); //拆箱 调用了intValue方法
}
Integer integer1 = 127;
Integer integer2 = 127;
Integer integer3 = 128;
Integer integer4 = 128;
System.out.println(integer1==integer2); //true
System.out.println(integer3==integer4); //false
Integer i=100
编译器调用了valueOf()
方法 Integer i=Integer.valueOf(100)
Integer 中的缓存类 IntegerCache Cache 为[-128,127],IntegerCache 有一个静态的 Integer 数 组,在类加载时就将-128 到 127 的 Integer 对象创建了,并 保存在 cache 数组中,一旦程序调用 valueOf
方法,如果取 的值是在-128 到 127 之间就直接在 cache 缓存数组中去取 Integer 对象,超出范围就 new 一个对象。int a=new Integer(100)
编译器为我们添加了 int a=new Integer(100).intValue()
Date date = new Date();
System.out.println(date); //Tue Mar 12 12:57:25 CST 2019
Date date1 = new Date(1001L);
System.out.println(date1); //Thu Jan 01 08:00:01 CST 1970
//比较时间
System.out.println(date1.getTime()>date.getTime()); //false
System.out.println(date1.before(date)); //true
System.out.println(date1.after(date)); //false
System.out.println(date1.compareTo(date)); //-1
System.out.println(date1.equals(date)); //false
//日期转字符串
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDateString = format.format(date);
System.out.println(strDateString); //2019-03-12 13:17:58
//字符串转日期
String string = "2019-03-12 13:17:58";
Date date3 = format.parse(string);
System.out.println(date3); //Tue Mar 12 13:17:58 CST 2019
//获取北京时间
Calendar calendar = Calendar.getInstance(Locale.CHINA);
Date date2 = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = dateFormat.format(date2);
System.out.println(dateString);
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
/**
* 命令行打印日历
* @author yuankun
* @version 0.1
*/
public class PrintCalendar {
public static void main(String[] args) throws ParseException {
//获取日期输入
Scanner scanner = new Scanner(System.in);
System.out.println("请输入日期:(yyyy-mm-dd)");
String inputString = scanner.next();
//获取日历对象
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(inputString);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
//获取输入日期
int inputDate = calendar.get(Calendar.DAY_OF_MONTH);
//获取当月1号的星期数
calendar.set(Calendar.DAY_OF_MONTH, 1);
int dayOfweek = calendar.get(Calendar.DAY_OF_WEEK);
//获取当月天数
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("日\t一\t二\t三\t四\t五\t六");
//打印1号前的空号
for(int i=1;i<dayOfweek;i++) {
System.out.print("\t");
}
//打印日期
for(int i=1;i<=maxDay;i++) {
System.out.print(i);
if(i==inputDate) {
System.out.print("*");
}
System.out.print("\t");
if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY) {
System.out.println();
}
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
//静态导入
import static java.lang.Math.*;
public class TestMath {
public static void main(String[] args) {
//返回1-10的随机数
System.out.println((int)(random()*9)+1);
//四舍五入取整
System.out.println(round(3.1415));
//取最值
System.out.println(max(1, 2.1));
}
}
//创建文件
File file = new File("D:\\a.txt");
File file1 = new File("a.txt");
File file2 = new File("D:/dir");
System.out.println(file.getAbsolutePath());
System.out.println(file1.getAbsolutePath());
System.out.println(file2.getAbsolutePath());
System.out.println(file.getName());
System.out.println(file1.getName());
System.out.println(file2.getName());
System.out.println(file.createNewFile());
System.out.println(file1.createNewFile());
System.out.println(file2.createNewFile());
//创建目录
File file3 = new File("D:\\test\\aa\\bb");
file3.mkdirs();
File parentFile = file3.getParentFile();
file3.delete();
parentFile.delete(); //目录下没有东西才能删除
//遍历目录
File file4 = new File("D:\\");
File[] files = file4.listFiles();
for (File f : files) {
System.out.println(f);
}
System.out.println("-----------------------------");
String[] strFileStrings = file4.list();
for (String string : strFileStrings) {
System.out.println(string); //文件名,没有盘符
}
/**
* 树状结构展示目录
* @author yuankun
*
*/
public class FilesTree {
public static void main(String[] args) {
File file = new File("F:\\my-github\\my-java");
PrintFiles(file, 0);
}
public static void PrintFiles(File file, int level) {
for(int i=0;i<level;i++) {
System.out.print("-");
}
System.out.println(file.getName());
if(file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
PrintFiles(f, level+1);
}
}
}
}
public enum Gender {
男,女;
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
public class Person {
private String name;
private int age;
private Gender gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Person(String name, int age, Gender gender) {
super();
this.name = name;
this.age = age;
this.gender = gender;
}
public Person() {
super();
}
}
public class EnumMain {
public static void main(String[] args) {
Gender sex = Gender.女;
sex.setName("张静");
System.out.println(sex.getName()); //张静
Person person = new Person("旺财", 12, Gender.女);
System.out.println(person.getGender());
}
}