Object
類層次結構的跟類,每個類都使用Object作為超類,每個類都直接或者間接繼承于Object
構造方法 public Object()
成員方法 :
public int hashCode():回傳該物件的哈希碼值,哈希值是根據哈希演算法計算出來,于地址值有關
public final Class getClass(): 回傳此Object類的運行時類
toString() 回傳物件的字串表示,默認是由類的全路徑+'@'+哈希值的十六進制表示, 這個表示其實是沒有意義的,一般子類都會重寫該方法, 可以自動生成
equals()比較兩個物件是否相同,默認情況下,比較的是地址值是否相同,
而比較地址值是沒有意義的,所以,一般子類也會重寫該方法,可以自動生成,
finalize() 用于垃圾回收,在不確定的時間
clone() 可以實作物件的克隆,包括成員變數的資料復制,但是它和兩個參考指向同一個物件是有區別的,
直接輸出一個物件名稱,其實默認呼叫了該物件的toString()方法,
面試題
==和equals()的區別?
A:==
基本型別:比較的是值是否相同
參考型別:比較的是地址值是否相同
B:equals()
只能比較參考型別,默認情況下,比較的是地址值是否相同,
但是,我們可以根據自己的需要重寫該方法,
String
構造方法:public String()
public String(byte[] bytes)
public String(byte[] bytes,int offset,int length)
public String(char[] value)
public String(char[] value,int offset,int count)
public String(String original)
面試題:
字面值作為字串物件和通過構造方法創建物件的不同
String s = new String("hello");和String s = "hello"的區別?
前者創建兩個物件后者創建一個物件
1 /* 2 * 看程式寫結果 3 */ 4 public class StringDemo3 { 5 public static void main(String[] args) { 6 String s1 = new String("hello"); 7 String s2 = new String("hello"); 8 System.out.println(s1 == s2);// false 9 System.out.println(s1.equals(s2));// true 10 11 String s3 = new String("hello"); 12 String s4 = "hello"; 13 System.out.println(s3 == s4);// false 14 System.out.println(s3.equals(s4));// true 15 16 String s5 = "hello"; 17 String s6 = "hello"; 18 System.out.println(s5 == s6);// true 19 System.out.println(s5.equals(s6));// true 20 } 21 }
1 /* 2 * 看程式寫結果 3 * 字串如果是變數相加,先開空間,在拼接, 4 * 字串如果是常量相加,是先加,然后在常量池找,如果有就直接回傳,否則,就創建, 5 */ 6 public class StringDemo4 { 7 public static void main(String[] args) { 8 String s1 = "hello"; 9 String s2 = "world"; 10 String s3 = "helloworld"; 11 System.out.println(s3 == s1 + s2);// false 字串拼接是先開了一個空間(X002)然后在進行拼接而不是直接指向s3所指向的那個位置(X001)(方法區的常量池中).所以地址值不一樣,false, 12 System.out.println(s3.equals((s1 + s2)));// true 13 14 System.out.println(s3 == "hello" + "world");// false 這個我們錯了,應該是true 15 System.out.println(s3.equals("hello" + "world"));// true 16 17 // 通過反編譯看原始碼,我們知道這里已經做好了處理, 18 // System.out.println(s3 == "helloworld"); 19 // System.out.println(s3.equals("helloworld")); 20 } 21 }
與判斷功能相關的方法
boolean equals(object obj):
boolean equalsIgnoreCase(String str):
boolean contains(String str):
boolean startwith(String str):
boolean endswith(String str):
boolean isEmpty();
與獲取相關的方法
int length();獲取字串的長度,
char charAt(int index):獲取指定索引位置的字符
int indexOf(int ch):回傳指定字符在此字串中第一次出現處的索引,
int indexOf(String str):回傳指定字串在此字串中第一次出現處的索引,
int indexOf(int ch, int fromIndex):回傳指定字符在此字串中從指定位置后第一次出現處的索引,
int indexOf(String str, int fromIndex):回傳指定字串在此字串中從指定位置后第一次出現處的索引,
String substring(int start):從指定位置開始截取字串,默認到末尾,
String substring(int start, int end):從指定位置開始到指定位置結束截取字串,
字串的遍歷:
1. charAt()獲取每個字符,length()用來回圈
2. toCharArray() 轉換成字符陣列,在對陣列進行遍歷
與轉換相關的方法
byte[] getBytes():把字串轉換為位元組陣列,
char[] toCharArray():把字串轉換為字符陣列,
static String valueOf(char[] chs):把字符陣列轉成字串,
static String valueOf(int i):把int型別的資料轉成字串,
String toLowerCase():把字串轉成小寫,
String toUpperCase():把字串轉成大寫,
String concat(String str):把字串拼接,
其他常用方法
String replace(char old, char new)
String replace(String old, String new)
String trim()
int compateTo(String str)
int compareToIgnoreCase(String str)
StringBuffer
執行緒安全的可變字串
StringBuffer和String的區別? 前者長度和內容可變,后者不可變,如果使用前者做字串的拼接,不會浪費太多的資源,
構造方法:StringBuffer的構造方法:
public StringBuffer():
public StringBuffer(int capacity):
public StringBuffer(String str):
StringBuffer的方法:
public int capacity():
public int length():
添加:public StringBuffer append(String str): 把任意型別資料添加到字串緩沖區里,并回傳字串緩沖區本身,
public StringBuffer insert(int offset, String str): 在指定位置把任意型別資料插入到字串緩沖區里面,并回傳字符緩沖區本身,
洗掉:public StringBuffer deleteCharAt(int index):
public StringBuffer delete(int start, int end):
替換:public StringBuffer replace(int start, int end, String str):
反轉:public StringBuffer reverse()
截取:public String substring(int start):
public String substring(int start, int end):
String和StringBuffer相互轉換:
StringBuffer的構造方法 public StringBuffer(String str):
StringBuffer的append方法;
String的構造方法 public String(StringBuffer buffer):
StringBuffer的toString方法;
StringBuilder
執行緒不安全的可變字串
面試題:
String, StringBuffer, StringBuilder的區別?
String是內容不可變的,而StringBuffer,StringBuilder都是內容可變的
StringBuffer是同步的,資料安全,效率低;StringBuilder是不同步的,資料不安全,效率高
StringBuffer和陣列的區別?
兩者都可以看出是一個容器,裝其他的資料
但是StringBuffer的資料最終是一個字串資料
而陣列可以放置多種資料,但是必須是同一種資料型別
1 /* 形式引數問題 2 * String作為引數傳遞 3 * StringBuffer作為引數傳遞 4 * 5 * 形式引數: 6 * 基本型別:形式引數的改變不影響實際引數 7 * 參考型別:形式引數的改變直接影響實際引數 8 * 9 * 注意: 10 * String作為引數傳遞,雖然String是參考型別但是效果和基本型別作為引數傳遞是一樣的, 11 * SreingBuffer運用賦值符號時形式引數的改變不影響實際引數,運用append()方法時,形式引數的改變直接影響實際引數 12 */ 13 public class StringBufferDemo { 14 public static void main(String[] args) { 15 String s1 = "hello"; 16 String s2 = "world"; 17 System.out.println(s1 + "---" + s2);// hello---world 18 change(s1, s2); 19 System.out.println(s1 + "---" + s2);// hello---world 20 21 StringBuffer sb1 = new StringBuffer("hello"); 22 StringBuffer sb2 = new StringBuffer("world"); 23 System.out.println(sb1 + "---" + sb2);// hello---world 24 change(sb1, sb2); 25 System.out.println(sb1 + "---" + sb2);// hello---worldworld 26 27 } 28 29 public static void change(StringBuffer sb1, StringBuffer sb2) { 30 sb1 = sb2; 31 sb2.append(sb1); 32 } 33 34 public static void change(String s1, String s2) { 35 s1 = s2; 36 s2 = s1 + s2; 37 } 38 }
Arrays
針對陣列進行操作的工具類
public static String toString(int[] a):
public static void sort(int[] a): 底層快排實作
public static int binarySearch(int[] a, int key):
基本型別包裝類
Integer
構造方法:public Integer (int value)
public Integer (String s)
1 /* 2 * int型別和String型別的相互轉換 3 * 4 * int -- String 5 * String.valueOf(number) 6 * 7 * String -- int 8 * Integer.parseInt(s) 9 */ 10 public class IntegerDemo { 11 public static void main(String[] args) { 12 // int -- String 13 int number = 100; 14 // 方式1 15 String s1 = "" + number; 16 System.out.println("s1:" + s1); 17 // 方式2 18 String s2 = String.valueOf(number); 19 System.out.println("s2:" + s2); 20 // 方式3 21 // int -- Integer -- String 22 Integer i = new Integer(number); 23 String s3 = i.toString(); 24 System.out.println("s3:" + s3); 25 // 方式4 26 // public static String toString(int i) 27 String s4 = Integer.toString(number); 28 System.out.println("s4:" + s4); 29 System.out.println("-----------------"); 30 31 // String -- int 32 String s = "100"; 33 // 方式1 34 // String -- Integer -- int 35 Integer ii = new Integer(s); 36 // public int intValue() 37 int x = ii.intValue(); 38 System.out.println("x:" + x); 39 //方式2 40 //public static int parseInt(String s) 41 int y = Integer.parseInt(s); 42 System.out.println("y:"+y); 43 } 44 }
面試題
1 /* 2 * 看程式寫結果 3 * 4 * 注意:Integer的資料直接賦值,如果在-128到127之間,會直接從緩沖池里獲取資料,否則就創建一個新的空間 5 */ 6 public class IntegerDemo { 7 public static void main(String[] args) { 8 Integer i1 = new Integer(127); 9 Integer i2 = new Integer(127); 10 System.out.println(i1 == i2); 11 System.out.println(i1.equals(i2)); 12 System.out.println("-----------"); 13 14 Integer i3 = new Integer(128); 15 Integer i4 = new Integer(128); 16 System.out.println(i3 == i4); 17 System.out.println(i3.equals(i4)); 18 System.out.println("-----------"); 19 20 Integer i5 = 128; 21 Integer i6 = 128; 22 System.out.println(i5 == i6); //false 23 System.out.println(i5.equals(i6)); //true 24 System.out.println("-----------"); 25 26 Integer i7 = 127; 27 Integer i8 = 127; 28 System.out.println(i7 == i8); //true 29 System.out.println(i7.equals(i8)); //true 30 31 // 通過查看原始碼,我們就知道了,針對-128到127之間的資料,做了一個資料緩沖池,如果資料是該范圍內的,每次并不創建新的空間 32 // Integer ii = Integer.valueOf(127); 33 }
Byte
Short
Long
Float
Double
Character
構造方法:public Character(char value)
public static boolean isUpperCase(char ch):判斷給定的字符是否是大寫字符
public static boolean isLowerCase(char ch):判斷給定的字符是否是小寫字符
public static boolean isDigit(char ch):判斷給定的字符是否是數字字符
public static char toUpperCase(char ch):把給定的字符轉換為大寫字符
public static char toLowerCase(char ch):把給定的字符轉換為小寫字符
Boolean
正則運算式:
Pattern類:正則運算式的編譯表示形式
正則運算式規則:
A:字符 x 字符 x,舉例:'a'表示字符a \\ 反斜線字符("\"), \n 新行(換行)符 ('\u000A') \r 回車符 ('\u000D') B:字符類 [abc] a、b 或 c (3者選其一)(簡單類) [^abc] 任何字符,除了 a、b 或 c(否定) [a-zA-Z] a到 z 或 A到 Z,兩頭的字母包括在內(范圍) [0-9] 0到9的字符都包括 C:預定義字符類 . 任何字符,我的就是.字符本身,怎么表示呢? \. \d 數字:[0-9] \w 單詞字符:[a-zA-Z_0-9] 在正則運算式里面組成單詞的東西必須有這些東西組成 D:邊界匹配器 ^ 行的開頭 $ 行的結尾 \b 單詞邊界 就是不是單詞字符([a-zA-Z_0-9])的地方, 舉例:hello world?haha;xixi E:Greedy 數量詞 X? X,一次或一次也沒有 X* X,零次或多次 X+ X,一次或多次 X{n} X,恰好 n 次 X{n,} X,至少 n 次 X{n,m} X,至少 n 次,但是不超過 m 次
判斷:String類的public boolean matches(String regex):
分割:String類的public String[] split(String regex):
替換:String類的public String replaceAll(String regex,String replacement)
Pattern和matcher的使用
Math
成員變數:
public static final double PI
public static final double E
成員方法
public static int abs(int a):絕對值
public static double ceil(double a ):向上取整
public static double floor(double a):向下取整
public static int max(int a, int b):回傳最大值
public static double pow(double a, double b):平方根
public static double random():產生0到1亂數
public static int round(float a):四舍五入
public static double sqrt(double a):正平方根
Random
構造方法:public Random(): 沒有給種子,用的是默認種子,是當前時間的毫秒值
public Random(long seed): 給出指定的種子 給定種子后隨機出來的數是固定的
成員方法:public int nextInt():
public int nextInt(int n):
System
無構造方法,不能被實體化
成員方法:public static void gc(): 運行垃圾回收器
public static void exit(int status): 終止當前正在運行的java虛擬機,引數用作狀態碼;根據慣例,非 0 的狀態碼表示例外終止,即表示0狀態碼表示正常終止,
public static long currentTimeMillis():回傳以毫秒為單位的當前時間[可以用來計算程式運行的時間]
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length): 用于將一個陣列的元素快速拷貝到另一個陣列
BigInteger
構造方法:BigInteger(String val)
成員方法:
public BigInteger add(BigInteger val):加
public BigInteger subtract(BigInteger val):減
public BigInteger multiply(BigInteger val):乘
public BigInteger divide(BigInteger val):除
public BigInteger[] divideAndRemainder(BigInteger val):回傳商和余數的陣列
BigDecimal
由于在運算的時候,float型別和double很容易丟失精度,為了能精確的表示、計算浮點數,Java提供了BigDecimal,BigDecimal類:不可變的、任意精度的有符號十進制數,可以解決資料丟失問題,
構造方法:public BigDecimal(String val)
成員方法:public BigDecimal add(BigDecimal augend)
public BigDecimal subtract(BigDecimal subtrahend)
public BigDecimal multiply(BigDecimal multiplicand)
public BigDecimal divide(BigDecimal divisor)
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,幾位小數,如何舍取
Date
構造方法:Date():根據當前的默認毫秒值創建日期物件
Date(long date):根據給定的毫秒值創建日期物件
成員方法:public long getTime(): 獲取時間,以毫秒為單位
public void setTime(long time):設定時間
DateFormat
Date -- String(格式化)
public final String format(Date date)
String -- Date(決議)
public Date parse(String source)
DateForamt:可以進行日期和字串的格式化和決議,但是由于是抽象類,所以使用具體子類SimpleDateFormat,
SimpleDateFormat的構造方法:
SimpleDateFormat():默認模式
SimpleDateFormat(String pattern):給定的模式(該模式為:年 y月 M 日 d時 H分 m秒 s)
Calendar
抽象類,使用Calender.getInstance()獲取子類物件
public int get (int field): 回傳給定日歷欄位的值,日歷類中的每個日歷欄位都是靜態的成員變數,并且是int型別,
public void add(int field,int amount):根據給定的日歷欄位和對應的時間,來對當前的日歷進行操作,
public final void set(int year,int month,int date):設定當前日歷的年月日
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/39689.html
標籤:Java
