JAVA常用类
基本数据类型的包装类(Wrapper Class)
为什么需要包装类
Java并不是纯面向对象的语言.Java语言是一个面型对象的语言,但是Java中基本数据类型确实不面向对象的.但实际应用中经常需要将基本数据类型转化成对象,便于操作.比如:集合的操作中我们就需要将基本数据类型转化成对象!
包装类均位于java.lang包,包装类和基本数据类型的对应关系:
byte Byte
boolean Boolean
short Short
Char Character
int Integer
long Long
float Float
double Double
自动装箱(1.5之后)
-
基本类型自动的封装成与它相同类型的包装类对象,如:
-
Integer = 100;
-
本质上是编译器编译时为我们添加了:
-
Integer i = new Integer(100);
自动拆箱(1.5之后)
-
包装类对象自动转换成基本类型数据,如:
-
int a = new Integer(100);
-
本质上是编译器编译时为我们添加了:
-
int a = new Integer(100).intValue();
缓存问题
-
当创建Integer对象时,不使用new Integer(int i)语句,大小在-128~127之间,对象存放在Integer常量池中
-
Integer.valueOf()方法
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
- 测试
public class TestInteger {
public static void main(String[] args) {
//jdk1.5后 虚拟机为包装类提供了缓冲池,Integer缓冲池的大小为一个字节(-128~127);
//创建 1 个对象,存放在常量池中。引用c1,c2存放在栈内存中。
Integer c1 = 1;
Integer c2 = 1;
System.out.println("c1 = c2 ? " + (c1 == c2)); //true
//创建 2 个对象,存放在堆内存中。2 个引用存放在栈内存中。
Integer b1 = 130; //130不在(-128~127)之间
Integer b2 = 130;
System.out.println("b1 = b2 ? " + (b1 == b2)); //false
//创建2个对象,存放在堆内存中。
Integer b3 = new Integer(2);
Integer b4 = new Integer(2);
System.out.println("b3 = b4 ? " + (b3 == b4)); //false
//下面两行代码证明了使用new Integer(int i) (i 在-128~127之间)创建对象不会保存在常量池中。
Integer b5 = 2;
System.out.println("b3 = b5 ? " + (b3 == b5)); //false
//Integer的自动拆箱,b3自动转换成数字 2。
System.out.println("b3 = 2 ? " + (b3 == 2)); //true
Integer b6 = 210;
System.out.println("b6 = 210 ? " + (b6 == 210)); //true
}
}
时间处理相关类
Date时间类(java.util.Date)
-
在标准Java类库中包含一个Date类.它的对象表示一个特定的瞬间,精确到毫秒
-
Java中事假的表示说白了也是数字,是从标准纪元1970.1.1 0点开始到某时刻的毫秒数,类型是long
Date d = new Date(1000);
//toGMTString()转换为格林尼治时间,否则为本地时间
System.out.println(d.toGMTString());
- Date类的多数方法都遗弃了,只保留了核心方法,建议使用Calendar类
DateFormat和SimpleDateFormat
-
完成字符串和时间对象的转化
-
format
DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
Date date = new Date();
String str = dateFormat.format(date);
System.out.println(str);
更多参数可以看api,比如w代表第几周
- parse
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = dateFormat.parse("1977-7-3 12:30:13");
System.out.println(date);
Calendar日历类
-
GregorianCalendar是Calendar的一个子类,提供了世界上大多数国家/地区使用的标准日历系统
-
注意:
-
月份:一月是0,二月是1,以此类推
-
星期:周日是1,周一是2...周六是7
-
Calendar c = new GregorianCalendar();
c.set(2017, Calendar.APRIL, 10);
c.set(Calendar.YEAR, 2017);
System.out.println(c.getTimeInMillis());
//加1天,减用负数
c.add(Calendar.DAY_OF_YEAR, 1);
System.out.println(c.getTimeInMillis());
可视化日历
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;
/**
* 可视化日历程序
*/
public class VisualCalendar {
public static void main(String[] args) {
System.out.println("请输入日期(按照格式:2030-3-10):");
Scanner scanner = new Scanner(System.in);
String temp = scanner.nextLine();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = format.parse(temp);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
int day = calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE, 1);
int maxDate = calendar.getActualMaximum(Calendar.DATE);
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i=1;i<calendar.get(Calendar.DAY_OF_WEEK);i++){
System.out.print('\t');
}
for(int i=1;i<=maxDate;i++){
if(i==day){
System.out.print("*");
}
System.out.print(i+"\t");
int w = calendar.get(Calendar.DAY_OF_WEEK);
if(w==Calendar.SATURDAY){
System.out.print('\n');
}
calendar.add(Calendar.DATE, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
File类
java.io.File:文件和目录路径名的抽象形式
通过File对象可以访问文件的属性
-
public boolean canRead()
-
public boolean canWrite()
-
public boolean exists()
-
public boolean isDirectory()
-
public boolean isFile()
-
public boolean isHidden()
-
public boolean lastModified()
-
public boolean length()
-
public boolean getName()
-
public boolean getPath()
通过File对象创建空文件或目录(在该对象所指的文件或目录不存在的情况下)
-
public boolean createNewFile()throws IOException
-
public boolean delete()
-
public boolean mkdir() ,mkdirs()
递归打印目录树状结构
import java.io.File;
public class FileTree {
public static void main(String[] args) {
//找一个自己硬盘上有用的文件夹
File f = new File("d:/aaa");
printFile(f, 0);
}
static void printFile(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 temp : files) {
printFile(temp, level+1);
}
}
}
}