包裝類
將基本類型數據轉化成對象,包裝類位於java.lang包中
提供: 字符串、 基本類型數據、 對象之間互相轉化的方式,包含每種基本數據類型的相關屬性如最大值、 最小值等
基本數據類型
包裝類
byte
Byte
boolean
Boolean
short
Short
char
Character
int
Integer
long
Long
float
Float
double
Double
public class TestWapperedClass {
public static void main ( String[ ] args) {
int i = 100000 ;
Integer a = new Integer ( i) ;
Integer b = Integer. valueOf ( i) ;
int a1 = a;
int a2 = a. intValue ( ) ;
Integer a3 = new Integer ( "1234" ) ;
Integer a4 = Integer. valueOf ( "1234" ) ;
Integer a5 = Integer. parseInt ( "1234" ) ;
String s = a3. toString ( ) ;
System. out. println ( Integer. MIN_VALUE) ;
System. out. println ( Integer. MAX_VALUE) ;
System. out. println ( Integer. max ( 4 , 6 ) ) ;
System. out. println ( Integer. compare ( 6 , 6 ) ) ;
}
}
Integer a = 312 ;
int b = a;
緩存 -128到127之間的數字,緩存是個數組
如果數據在-128~127這個區間,那麼在類加載時就已經爲該區間的每個數值創建了對象,並將這256個對象存放到一個名爲cache的數組中。每當自動裝箱過程發生時(或者手動調用valueOf()時),就會先判斷數據是否在該區間,如果在則直接獲取數組中對應的包裝類對象的引用,如果不在該區間,則會通過new調用包裝類的構造方法來創建對象。
Integer a1 = - 128 ;
Integer a2 = - 128 ;
System. out. println ( a1 == a2) ;
System. out. println ( a1. equals ( a2) ) ;
Integer a3 = 1234 ;
Integer a4 = 1234 ;
System. out. println ( a3 == a4) ;
System. out. println ( a3. equals ( a4) ) ;
字符串String
String 類對象代表不可變的Unicode字符序列。private final char value[];
判斷是不是同一個字符串使用equals
==判斷兩個對象是否爲同一個,equals判斷內容是不是相同
String str1 = "abc" ;
String str2 = "123" ;
String str3 = str1 + str2;
String str4 = "abc" + "123" ;
System. out. println ( str3 == str4) ;
System. out. println ( str4 == "abc123" ) ;
System. out. println ( str3. equals ( str4) ) ;
StringBuilder是可變的字符序列,StringBuffer也是,都是AbstractStringBuilder的子類,前者效率高但是線程不安全,StringBuffer線程安全,效率低
可變字符串,修改時直接修改本身
StringBuilder sb = new StringBuilder ( "abcdefg" ) ;
System. out. println ( Integer. toHexString ( sb. hashCode ( ) ) ) ;
sb. setCharAt ( 2 , 'k' ) ;
System. out. println ( sb) ;
System. out. println ( Integer. toHexString ( sb. hashCode ( ) ) ) ;
StringBuilder sb1 = new StringBuilder ( ) ;
for ( int i = 0 ; i< 26 ; i++ ) {
sb1. append ( ( char ) ( 'a' + i) ) ;
}
System. out. println ( sb1) ;
sb1. reverse ( ) ;
System. out. println ( sb1) ;
sb1. setCharAt ( 2 , 'A' ) ;
System. out. println ( sb1) ;
sb1. insert ( 1 , "中國" ) . insert ( 4 , "萬歲" ) . insert ( 7 , "萬歲" ) ;
System. out. println ( sb1) ;
sb1. delete ( 20 , 23 ) ;
System. out. println ( sb1) ;
sb1. deleteCharAt ( 22 ) ;
陷阱:對String字符串的操作實際上是對其副本(原始拷貝)的操作,原來的字符串一點都沒有改變。for循環中使用str = str + i,會產生大量的無用對象,浪費空間時間,應該使用StringBuilder,append方法。測試兩種情況的對比,第一種會產生大量的字符串,既費空間也耗費時間
public class TestStringTime {
public static void main ( String[ ] args) {
String str = "" ;
long memory1 = Runtime. getRuntime ( ) . freeMemory ( ) ;
long time1 = System. currentTimeMillis ( ) ;
for ( int i= 0 ; i< 5000 ; i++ ) {
str = str + i;
}
long memory2 = Runtime. getRuntime ( ) . freeMemory ( ) ;
long time2 = System. currentTimeMillis ( ) ;
System. out. println ( "String佔用內存:" + ( memory1- memory2) ) ;
System. out. println ( "String佔用時間:" + ( time2- time1) ) ;
StringBuilder sb = new StringBuilder ( ) ;
long memory3 = Runtime. getRuntime ( ) . freeMemory ( ) ;
long time3 = System. currentTimeMillis ( ) ;
for ( int i= 0 ; i< 6000 ; i++ ) {
sb. append ( i) ;
}
long memory4 = Runtime. getRuntime ( ) . freeMemory ( ) ;
long time4 = System. currentTimeMillis ( ) ;
System. out. println ( "StringBuilder佔用內存:" + ( memory3- memory4) ) ;
System. out. println ( "StringBuilder佔用時間:" + ( time4- time3) ) ;
}
}
時間處理類
時間 1970年1月1日0點0分0秒爲原點,格林威治時間
使用long類型表示時間,獲取當前時間long now = System.currentTimeMillis()
Date類
java.util.Date
getTime獲取毫秒數
import java. util. Date;
public class TestDate {
public static void main ( String[ ] args) {
Date date = new Date ( ) ;
System. out. println ( date) ;
long t1 = date. getTime ( ) ;
System. out. println ( t1) ;
Date date0 = new Date ( 0 ) ;
System. out. println ( date0) ;
System. out. println ( date. after ( date0) ) ;
Date d3 = new Date ( 2020 - 1900 , 1 , 3 ) ;
System. out. println ( d3) ;
}
}
DateFormat
DateFormat是抽象類,一般使用具體實現類,SimpleDateFormat
常用parse和format,用於字符串和時間對象的相互轉換
import java. text. DateFormat;
import java. text. ParseException;
import java. text. SimpleDateFormat;
import java. util. Date;
public class TestDateFormat {
public static void main ( String[ ] args) throws ParseException {
SimpleDateFormat df1 = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
SimpleDateFormat df2 = new SimpleDateFormat ( "yyyy年MM月dd日 HH時mm分ss秒" ) ;
String str1 = df1. format ( new Date ( ) ) ;
System. out. println ( str1) ;
System. out. println ( df2. format ( new Date ( ) ) ) ;
String str2 = "2017年1月10日 16時10分38秒" ;
Date d = df2. parse ( str2) ;
System. out. println ( d) ;
SimpleDateFormat df3 = new SimpleDateFormat ( "D" ) ;
String str3 = df3. format ( new Date ( ) ) ;
System. out. println ( str3) ;
}
}
Calendar
Calendar 類是一個抽象類,爲我們提供了關於日期計算的相關功能,比如:年、月、日、時、分、秒的展示和計算。 GregorianCalendar 是 Calendar 的一個具體子類
import java. util. Date;
import java. util. GregorianCalendar;
public class TestCalendar {
public static void main ( String[ ] args) {
GregorianCalendar c = new GregorianCalendar ( 2020 , 11 , 5 ) ;
int year = c. get ( c. YEAR) ;
int month = c. get ( c. MONTH) ;
int day = c. get ( c. DAY_OF_MONTH) ;
int weekday = c. get ( c. DAY_OF_WEEK) ;
System. out. println ( year) ;
System. out. println ( month) ;
System. out. println ( day) ;
System. out. println ( weekday) ;
GregorianCalendar c1 = new GregorianCalendar ( ) ;
c1. set ( c1. YEAR, 2022 ) ;
System. out. println ( c1) ;
c1. add ( c1. YEAR, - 100 ) ;
System. out. println ( c1) ;
Date d1 = c1. getTime ( ) ;
System. out. println ( d1) ;
GregorianCalendar c2 = new GregorianCalendar ( ) ;
c2. setTime ( new Date ( ) ) ;
}
}
package MyPro08;
import java. text. ParseException;
import java. text. SimpleDateFormat;
import java. util. Calendar;
import java. util. Date;
import java. util. GregorianCalendar;
public class PrintCalendar {
public static void main ( String[ ] args) throws ParseException {
String date = "2020-5-20" ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date date1 = sdf. parse ( date) ;
String weekName = "日\t一\t二\t三\t四\t五\t六\n" ;
StringBuilder data = new StringBuilder ( ) ;
GregorianCalendar calendar = new GregorianCalendar ( ) ;
calendar. setTime ( date1) ;
int day = calendar. get ( calendar. DATE) ;
calendar. set ( calendar. DATE, 1 ) ;
int daysBefore1 = calendar. get ( calendar. DAY_OF_WEEK) ;
for ( int i = 0 ; i< daysBefore1 - 1 ; i++ ) {
data. append ( "\t" ) ;
}
int days = calendar. getActualMaximum ( Calendar. DATE) ;
for ( int i = 1 ; i <= days; i++ ) {
;
String dayst = "" ;
if ( calendar. get ( calendar. DATE) == day) {
dayst = calendar. get ( calendar. DATE) + "*" ;
} else {
dayst = calendar. get ( calendar. DATE) + "" ;
}
if ( calendar. get ( calendar. DAY_OF_WEEK) == 7 ) {
data. append ( dayst+ "\n" ) ;
} else {
data. append ( dayst+ "\t" ) ;
}
calendar. add ( calendar. DATE, 1 ) ;
}
System. out. println ( weekName+ data) ;
}
}
Math
java.lang.Math
1. abs 絕對值
2. acos,asin,atan,cos,sin,tan 三角函數
3. sqrt 平方根
4. pow(double a, double b) a的b次冪
5. max(double a, double b) 取大值
6. min(double a, double b) 取小值
7. ceil(double a) 大於a的最小整數
8. floor(double a) 小於a的最大整數
9. random() 返回 0.0 到 1.0 的隨機數
10. long round(double a) double型的數據a轉換爲long型(四捨五入)
11. toDegrees(double angrad) 弧度->角度
12. toRadians(double angdeg) 角度->弧度
Random
import java. util. Random;
public class TestRandom {
public static void main ( String[ ] args) {
Random rand = new Random ( ) ;
System. out. println ( rand. nextDouble ( ) ) ;
System. out. println ( rand. nextInt ( ) ) ;
System. out. println ( rand. nextFloat ( ) ) ;
System. out. println ( rand. nextBoolean ( ) ) ;
System. out. print ( rand. nextInt ( 10 ) ) ;
System. out. print ( 20 + rand. nextInt ( 10 ) ) ;
System. out. print ( 20 + ( int ) ( rand. nextDouble ( ) * 10 ) ) ;
}
}
File
import java. io. File;
import java. io. IOException;
import java. util. Date;
public class TestFile {
public static void main ( String[ ] args) throws IOException {
File f = new File ( "D:\\data\\a.txt" ) ;
f. createNewFile ( ) ;
System. out. println ( f) ;
f. renameTo ( new File ( "D:\\data\\abc.txt" ) ) ;
System. out. println ( System. getProperty ( "user.dir" ) ) ;
File f1 = new File ( "heihei.txt" ) ;
f1. createNewFile ( ) ;
System. out. println ( "File是否存在:" + f. exists ( ) ) ;
System. out. println ( "File是否是目錄:" + f. isDirectory ( ) ) ;
System. out. println ( "File是否是文件:" + f. isFile ( ) ) ;
System. out. println ( "File最後修改時間:" + new Date ( f. lastModified ( ) ) ) ;
System. out. println ( "File的大小:" + f. length ( ) ) ;
System. out. println ( "File的文件名:" + f. getName ( ) ) ;
System. out. println ( "File的目錄路徑:" + f1. getPath ( ) ) ;
System. out. println ( "File的完整路徑:" + f1. getAbsolutePath ( ) ) ;
boolean deleteSuccess = f1. delete ( ) ;
System. out. println ( deleteSuccess) ;
File f2 = new File ( "D:\\data\\test\\a\\b\\c" ) ;
boolean isMkdirSuccess = f2. mkdir ( ) ;
System. out. println ( isMkdirSuccess) ;
File f3 = new File ( "D:\\data\\test\\a\\b\\c" ) ;
boolean isMkDirs = f3. mkdirs ( ) ;
System. out. println ( isMkDirs) ;
File f4 = new File ( "D:\\data\\test\\a" ) ;
boolean idDelete = f4. delete ( ) ;
System. out. println ( idDelete) ;
}
}
import java. io. File;
public class ListFiles {
public static void main ( String[ ] args) {
printFile ( new File ( "D:\\data\\xinganmeng" ) , 0 ) ;
}
static void printFile ( File file, int level) {
for ( int i = 0 ; i < level; i++ ) {
System. out. print ( "-" ) ;
}
System. out. println ( file. getAbsolutePath ( ) ) ;
if ( file. isDirectory ( ) ) {
File[ ] files = file. listFiles ( ) ;
for ( File temp : files) {
printFile ( temp, level + 1 ) ;
}
}
}
}
枚舉
枚舉類型的定義包括枚舉聲明和枚舉體
所有的枚舉類型隱性地繼承自 java.lang.Enum。枚舉實質上還是類!而每個被枚舉的成員實質就是一個枚舉類型的實例,他們默認都是public static final修飾的
import java. util. Random;
public class TestEnum {
public static void main ( String[ ] args) {
System. out. println ( Season. AUTUMN) ;
Season[ ] seasons = Season. values ( ) ;
Random random = new Random ( ) ;
int a = random. nextInt ( 4 ) ;
System. out. println ( a) ;
switch ( seasons[ a] ) {
case SPRING:
System. out. println ( "春天" ) ;
break ;
case SUMMER:
System. out. println ( "夏天" ) ;
break ;
case AUTUMN:
System. out. println ( "秋天" ) ;
break ;
case WINTER:
System. out. println ( "冬天" ) ;
break ;
}
}
}
enum Season{
SPRING, SUMMER, AUTUMN, WINTER
}