Java高級特性 實用類

枚舉enum   java

枚舉是一個類,就是一個引用數據類型面試

枚舉類中根據需求定義多個枚舉值,正則表達式

枚舉值一旦被定義,不容許被改變,靜態的常量數組

  01.在Student類中定義Gender類型的sex私有屬性:緩存

   private Gender sex;安全

   02.建立Gender的枚舉 多線程

public enum Gender{app

MAN("男"),WOMEN("女");   dom

private String sex;函數

private Gender(String sex){
this.sex=sex;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

   }

  03.在測試類中調用

stu.setSex(Gender.MAN)

String sex = stu.getSex().getSex();
System.out.println(sex);//控制檯輸出--->男

1.咱們是用的封裝不能從根本上禁止用戶的輸入

2.使用枚舉,從根本上限定用戶的輸入

3.全部的枚舉值都是靜態常量,能夠經過枚舉類.枚舉值

4.枚舉類中構造方法,必須是私有的 private

基本數據類型的包裝類

1.集合中的泛型,<>不容許出現基本數據類型,包裝類能夠

2.定義了一個基本數據類型的變量,變量名能點出來東西

3.基本數據類型不能轉換成對象,包裝類能夠

4.全部的包裝類都是由final修飾的,不容許被繼承

5.在基本數據類型須要轉換成對象的時候,使用包裝類

6.jdk1.5之後,容許基本數據類型和包裝類進行混合運算,底層有裝箱和拆箱操做

基本數據類型    包裝類
byte          Byte
short         Short
int          Integer
long          Long
float         Float
double        Double
上面的六種都是數值類型!都是 extends Number implements Comparable<T>

public abstract class Number implements java.io.Serializable 支持 序列化


char Character
boolean Boolean

上面兩種都是 implements java.io.Serializable, Comparable<T>

好比說:
public Serializable getNum(Serializable s){
}

咱們調用getNum(參數能夠是8種包裝類中的任何一個)
返回值也能夠是 8種包裝類中的任何一個

 

裝箱和拆箱--->包裝類和基本數據類型的轉換

    01.裝箱:把基本數據類型轉換成對應的包裝類Integer num=1

    02.拆箱:把包裝類轉換成對應的基本數據數據類型 int num2=num

基本數據類型轉換:

   01.自動類型轉換

   02.強制類型轉換

引用數據類型轉換

  01.向下轉換

  02.向上轉換

/**
* 1.全部的包裝類都有對應的基本數據類型做爲參數,來構造本身的實例
*/
@SuppressWarnings("unused")
@Test
public void Test01(){
Byte b=new Byte((byte) 1);
Short s=new Short((short) 1);
Integer i=new Integer(1);
Long l=new Long(1);
Float f=new Float(1.0);
Double d=new Double(1.0);

Character c=new Character('1');
Character c2=new Character((char) 20);
Boolean bo=new Boolean(true);
}
/**
* 1.Float有三種實例化的方法參數分別是double float 和String
* 2.除了Character之外的的7種包裝類都有將String 做爲參數 來構建本身的實例
* 6種數值類型的的包裝類都繼承了Number
* 因此在使用String做爲參數來建立本身的實例時
* 若是參數不能轉換成數值 則拋出NumberFormatException
*/
@SuppressWarnings("unused")
@Test
public void Test02(){
Byte b=new Byte("1");
Short s=new Short("1");
Integer i=new Integer("1");
Long l=new Long("1");
Float f=new Float("1");
Double d=new Double("1");

//Character c=new Character("1");
Boolean bo=new Boolean("1");
Boolean bo2=new Boolean(true);
System.out.println(bo+"\n"+bo2);
}
/**
* 1.除了Character之外的的7種包裝類都有parseXxx(String s)
* 好比說Byte b=new Byte("1");
* b.parseByte(String);
* 01.4種整型對應的包裝類都是parseXxx(String s,int radix)radix進制轉換
* 02.其餘4種沒有parseXxx(String s,int radix)
* 03.Character壓根沒有parseXxx()
*/
@SuppressWarnings("unused")
@Test
public void Test03(){
Byte b=new Byte("1");
Short s=new Short("1");
Integer i=new Integer("1");
Long l=new Long("1");

Float f=new Float("1");
Double d=new Double("1");
Character c=new Character('1');
Boolean bo=new Boolean("true");

System.out.println(bo);
}
/**
* 4.進制轉換 須要學習位運算
*/
@Test
public void Test04(){
System.out.println("2進制的10對應的數據----->"+Integer.toBinaryString(10));
System.out.println("8進制的10對應的數據----->"+Integer.toOctalString(10));
System.out.println("16進制的10對應的數據----->"+Integer.toHexString(10));
}
/**
* 5.valueOf
* 把基本數據類型轉換成對應的包裝類---->裝箱
* xxxValue 8中包裝類型都有
* 把xxx類型轉換成對應的基本數據類型--->拆箱
*/
@Test
public void Test05(){
int num=3;
Integer i=Integer.valueOf(num);
num=i.intValue();
}
/**
* 6.經典的面試題
* 由於Integer.valueOf(num)會緩存-128~127之間的數據
* 若是咱們的數據在這個區間,不回去建立新的對象,而是從緩存中獲取
* 不然會new Integer()
*/
@Test
public void Test06() {
int num1=127;
int num2=127;
System.out.println(num1==num2);
Integer a1=new Integer(127);
Integer b1=new Integer(127);
System.out.println(a1==b1);
Integer a=127;
Integer b=127;
System.out.println(a==b);
Integer c=128;
Integer d=128;
System.out.println(c==d);
System.out.println("1"+1+1);
}

/**
* 111
* 211
*/

@Test
public void Test07() {
System.out.println("1"+1+1);
System.out.println(1+1+"1"+1);
}

 

Math
01.就是一個算術類
02.是final修飾
03.除了構造方法以外全部的方法都是靜態方法,方便咱們使用

天花板函數:
ceil 天 向上取整 3.1 ==》4 3.0 ==》3
floor 地 向下取整 3.9 ==》3

四捨五入
round(2.5)==>3
round(2.498)==>2

Random
隨機的boolean 數值    random.nextBoolean()

隨機0-1之間的小數     random.nextDouble()

隨機int類型的整數     random.nextInt()

隨機int類型指定的整數  random.nextInt(10)// 返回的是0-10之內  不包含10

 String

/**
* 大小寫轉換
* 字符串的長度 比較equals  忽略大小寫的比較equalsIgnore

*/

@Test
public void test01(){
String str1="hello";
String str2="HELLO";
System.out.println("小寫變成大寫"+str1.toUpperCase());
System.out.println("大寫變成小寫"+str2.toLowerCase());
System.out.println("字符串的長度"+str1.length());
System.out.println("hello.equals.HELLO----->"+str1.equals(str2));
System.out.println("hello.equalsIgnoreCase.HELLO----->"+str1.equalsIgnoreCase(str2));
}
/**
* 字符串轉換成char類型的數組
*/
@Test
public void test02(){
String str1="h e l l o";
char[] strs = str1.toCharArray();
System.out.println(strs.length);//9
for (char c : strs) {
System.out.println(c);
}
}
/**
* 字符串拆分 String regex 正則表達式
*/
@Test
public void test03(){
String str1="h1+e2+l3+l4+o5";
String[] strs = str1.split("\\+");//有+號的地方進行拆分,不顯示+號
for (String c : strs) {
System.out.println(c);
}
}
/**
* 查詢指定字符的位置 下標從0開始
*/
@Test
public void test04(){
String str1="123456@qq.com.cn";
System.out.println("@出現的位置"+str1.indexOf("@"));
System.out.println(".最後一次出現的位置"+str1.lastIndexOf("."));
}
/**
* 截取字符串 指向拿到qq.com
*/
@Test
public void test05(){
String str1="123456@qq.com.cn";
int begin = str1.indexOf("@");
int end = str1.lastIndexOf(".");
str1 = str1.substring(begin+1, end);//begin包含當前位置,end不包含當前位置
System.out.println(str1);
}
/**
* 替換字符串 把qq換成163
*/
@Test
public void test06(){
String str1="123456@qq.com.cn";
str1 = str1.replace("qq", "163");
System.out.println(str1);
}
/**
* 返回字符串中指定位置的字符
*/
@Test
public void test07(){
String str1="123456@qq.com.cn";
char s=str1.charAt(6);
System.out.println(s);
}
/**
* 鏈接字符串concat(String)
*/
@Test
public void test08(){
System.out.println("1"+11+1);//1111
System.out.println(11+1+"1"+5.0+"a");//1215.0a
String str1="a";
String str2="bc";
System.out.println(str1.concat(str2));
}
/**
* 判斷某個字符串中是否包含另外一個完整的字符串
*/
@Test
public void test09(){
String str1="abcdef";
boolean b = str1.contains("def");
System.out.println(b);
}

/**
* String StringBuffer StringBuilder
* String str1 = "abcdefg";
str1 = "abc"; // 每次都是一個新對象

01.String對象不可變
02.StringBuffer StringBuilder對象可變
03.StringBuffer線程安全 可是 效率比StringBuilder低 ,適合於多線程的狀況下使用
04.StringBuilder 線程不安全,可是效率是最高的! 適合於單線程的狀況下使用
*/

@Test
public void test10(){
String str1="hello";// 定義一個變量
int num = 1000000; // 定義操做字符串的次數
// 設置開始時間
long begin = System.currentTimeMillis();
for (int i = 0; i < num/100; i++) {
str1+="bye";
}
long end = System.currentTimeMillis();
System.out.println("String操做1w次執行的時間是:"+(end-begin));
// 使用StringBuffer
str1="hello";
StringBuffer sb=new StringBuffer(str1);
//設置開始時間
begin=System.currentTimeMillis();
for (int i = 0; i < num; i++) {
sb.append("bye");
}
end=System.currentTimeMillis();
System.out.println("StringBuffer操做100w次執行的時間是:"+(end-begin));
// 使用StringBuilder
str1="hello";
StringBuilder sb2=new StringBuilder(str1);
//設置開始時間
begin=System.currentTimeMillis();
for (int i = 0; i < num; i++) {
sb2.append("bye");
}
end=System.currentTimeMillis();
System.out.println("StringBuilder操做100w次執行的時間是:"+(end-begin));
}

相關文章
相關標籤/搜索