Java基礎部分(2)

Java語言中經常使用的類和方法java

  方法格式git

修飾符 返回值類型 方法名(參數類型 參數名1,參數類型 參數名2...){
     函數體;
     return 返回值;  
}
//具體實例
public static void main(String[] args){
     System.out.print("Hello");  
}
//public 和 static 都是修飾符

 

  Java中的修飾符
api

  

用於定義訪問權限修飾符的關鍵字數組

private(表示私有的)app

protected(受保護的)dom

public(共有的)函數

   

用於定義類,函數,變量修飾符的關鍵字工具

abstract(抽象的)優化

final(最終的)spa

static(靜態的)

 

synchronized(方法鎖)

 

特別說明:這些關鍵字與Java語言中的面向對象思想有關,主要特性是:封裝、繼承和多態。不明白的小夥伴請自行百度,在這裏不過過多的講解。

 

  Java中的方法調用

 

//調用方法共分爲兩步
//1.建立對象
  類名   對象名 = new 類名();
  對象名.方法名();

 

  Java中的經常使用類和方法(詳細使用請查看API)JavaAPI網址:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

一、Object類(Java中全部類的父類)

  

public int hasCode() //返回該對象的哈希碼值
public String toString() //返回該對象的字符串表示,通常子類都會重寫該方法
public boolean equals(Object obj) //默認比較對象引用是否相同,通常也會被子類重寫該方法

 

 

 

二、數組

特性1:Java中的數組只能存儲單一數據類型的數據,而且長度不可變。

特性2:獲取數組長度的屬性爲:length

特性3:java是強數據類型語言,數組的數據類型定義:int[] arr

三、Arrays(數組的工具類,提供了排序查找等功能)

//以字符串形式輸出該數組內容
public static String toString(int[] a)
//升序排序
public static void sort(int[] a)
//搜索
public static int binarySearch(int[] a,int key)

 

四、String類(這是各類語言中最經常使用的數據類型)

特別說明:在進行字符串拼接的時候,爲了提升效率。通常都使用StringBuffer類進行拼接。String 和 StringBuffer之間能夠相互轉化

  String類的判斷功能

 

//比較兩個字符串內容,考慮大小寫
boolean equals(Object obj)
//比較兩個字符串內容,不考慮大小寫
boolean equalsIgnoreCase(String str)
//判斷是否包含
boolean contains(String str)
//判斷是否以特定字符串開頭
boolean startsWith(String str)
//判斷是否以特定字符串結尾
boolean endsWith(String str)
//判斷是否爲空字符串
boolean isEmpty()

 

  String類的獲取功能

 1 //獲取字符串長度
 2 int length()
 3 //返回指定索引處的char值
 4 char charAt(int index)
 5 //返回指定字符在此字符串中第一次出現處的索引
 6 int indexOf(int ch)
 7 //返回第一次出現的指定子字符串在此字符串中的索引
 8 int indexOf(String str)
 9 //從指定的索引開始搜索,返回在此字符串中第一次出現指定字符處的索引
10 int indexOf(int ch,int fromIndex)
11 //從指定的索引處開始,返回第一次出現的指定子字符串在此字符串中的索
12 int indexOf(String str,int fromIndex)
13 //從索引處開始截取到末尾
14 String substring(int start)
15 //截取指定一段
16 String substring(int start,int end)

  String類的轉換功能

//將字符串轉換成字節數組(ASCII形式)
byte[] getBytes()
//將字符串轉換成字符數組
char[] toCharArray()
//將字符數組轉換爲字符串
static String valueOf(char[] chs)
//將整型轉換爲字符串
static String valueOf(int i)
//全小寫
String toLowerCase()
//全大寫
String toUpperCase()
//字符串拼接
String concat(String str)

  String類的其餘功能

//替換功能
String replace(char old,char new)
String replace(String old,String new)
//去除字符串兩空格    
String trim()
//按字典順序比較兩個字符串  
int compareTo(String str)
int compareToIgnoreCase(String str) 

 

 

五、StringBuffer類(主要是對String類的部分優化和補充)

 

//構造方法
public StringBuffer() 
public StringBuffer(String str)
//添加功能
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)

 

 

 

六、基本類型包裝類

說明1:將基本數據類型封裝成對象的好處在於能夠在對象中定義更多的功能方法操做該數據。

說明2:經常使用的操做之一,就是用於基本數據類型與字符串之間的轉換。

說明3:基本類型和包裝類的對應:Byte,Short,Integer,Long,Float,Double,Character,Boolean。

說明4:JDK5的新特性,自動拆裝箱。(自行百度)

  Integer類

 

//構造方法
public Integer(int value)
public Integer(String s)
//數據類型轉換
public int intValue()
public static int parseInt(String s)
public static String toString(int i)
public static Integer valueOf(int i)
public static Integer valueOf(String s)
//十進制到其餘進制
public static String toBinaryString(int i)//二進制
public static String toOctalString(int i)//八進制
public static String toHexString(int i)//十六進制

 

 

 

  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)

 

 

七、Math類

說明:Math類包含用於執行基本數學運算的方法,如:指數、對數、平方根和三角函數等。

  經常使用方法:

//絕對值
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) //min本身查找
//冪運算
public static double pow(double a,double b)
//大於或等於 0.0,小於 1.0的隨機數
public static double random()
//四捨五入
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類

說明:System類包含一些有用的類字段和方法。它不能被實例化。

  經常使用方法:

//運行垃圾回收器
public static void gc()
// 終止當前正在運行的 Java 虛擬機。
public static void exit(int status)
//返回以毫秒爲單位的當前時間
public static long currentTimeMillis()
//從指定源數組中複製一個數組,複製從指定的位置開始,到目標數組的指定位置結束。
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

 

十、Date類

說明1:Data類,表示特定的瞬間,精確到毫秒。

說明2:Date類常與SimpleDateFormat類一塊兒使用,來格式化日期和時間

  經常使用方法

 

//構造方法
public Date()
public Date(long date)//參數說明:添加的是時間的毫秒值,系統時間毫秒值能夠經過System類的內置方法獲取

//返回此Date對象表示的毫秒數
public long getTime()
//設置此對象的毫秒數
public void setTime(long time)

 

 

 

十一、SimpleDateFormat類

說明:它是日期/時間格式化的操做類。

  經常使用方法:

//構造方法
public SimpleDateFormat()
public SimpleDateFormat(String pattern)


//將Date類型格式化爲日期/時間字符串
public final String format(Date date)
//將格式化的日期/時間解析爲Date類型
public Date parse(String source)

 

十二、Calendar類

說明:Calendar類是一個抽象類,我習慣稱它爲日曆類。爲操做日曆字段提供了一些方法。

  經常使用方法:

//使用默認時區和語言環境得到一個日曆。
public static Calendar getInstance()
//返回給定日曆字段的值
public int get(int field)
//根據日曆的規則,爲給定的日曆字段添加或減去指定的時間量。
public void add(int field,int amount)
//設置日曆字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。
public final void set(int year,int month,int date)

 

 

特別說明1:以上的這些類在其餘語言中幾乎也都都,這些都是最基礎和通用的一些類和方法。在其餘語言中可能類的名字和方法會略有不一樣。不過主要功能都差很少。

特別說明2:下一篇我會繼續再列出Java中特有的一些類,這些類有些語言有有些語言沒有。不過也是在開發中最經常使用的一些類和方法。

相關文章
相關標籤/搜索