學習JAVA的同學都知道,sun為我們封裝了很多常用類,本篇就為大家總結一下我們經常使用的類。上一篇博客一位朋友留言問我String是不是引用數據類型?我通過查找資料發現String屬于應用數據類型,現在就讓我們首先了解一下String類吧。
1、String類
java.long.String類代表不可變的字符序列。String類舉例:
public class Str { /** * @param String類舉例 */ public static void main(String[] args) { String a1 = "abc"; String a2 = "abc"; String a3 = "abcd"; System.out.PRintln(a1==a2);//true System.out.println(a1==a3);//false System.out.println(); String b1 = new String("abc"); String b2 = new String("abc"); String b3 = new String("abcd"); System.out.println(b1==b2);//false System.out.println(b1==b3);//false System.out.println(b1.equals(b2));//true System.out.println(b1.equals(b3));//false System.out.println(); char [] c = {'a','b','c','d'}; String c1 = new String(c); String c2 = new String(c,0,3);//c:字符數組;0:數組中的第一個元素;3:c2的長度 System.out.println(c1);//abcd System.out.println(c2);//abc }}
2、String類中的常用方法:
public char charAt(int index):返回字符串中的第index個字符
public int length(): 返回字符串的長度
public int indexOf(String str):返回字符串中第一個出現str的位置
public int indexOf(String str, int fromIndex):返回字符串中從fromIndex后第一個出現str的位置
public boolean equalsIgnoreCase(String another):比較字符串與another是否一樣(忽略大小寫)
public String replace(char lastChar, char newChar):在字符串中用newChar替換lastChar字符
public boollean startsWith(String str):判斷字符串是否以str開始
public boollean endsWith(String str):判斷字符串是否以str結尾
public class Test { public static void main(String [] args){ String a1 = "Sun JAVA"; String a2 = "sun java"; System.out.println("a1的長度:"+a1.length()); System.out.println("a1中第五個字符是:"+a1.charAt(5)); System.out.println("a1中第一個出現JAVA的位置:"+a1.indexOf("JAVA")); System.out.println(a1.equals(a2)); System.out.println(a1.equalsIgnoreCase(a2)); System.out.println(a1.startsWith("Sun"));//判斷是否以指定前綴開始 System.out.println(a1.endsWith("JAVA"));//判斷是否以指定后綴結尾 String a3 = "I Love Play Computer Game!"; String a4 = a3.replace(' ', '-'); System.out.println("a3="+a3); System.out.println("a4="+a4); }}
3、基本數據類型向字符串轉換:
靜態方法public String valueOf(...),可將基本數據類型轉換為字符串。實例:
int b1 = 1234; double b2 = 12.13; boolean b3 = true; //類型轉換 String b4 = String.valueOf(b1); String b5 = String.valueOf(b2); String b6 = String.valueOf(b3);
4、StringBuffer類:
java.long.StringBuffer代表可變的字符序列,實例代碼:
5、Math類:
java.long.Math提供了一系列靜態方法用于科學計算,其方法的參數和返回值類型一般為double型。
abs:絕對值
sqrt:平方根
pow(double a, double b):a的b次冪
log:自然對數
max(double a, double b):a、b中的較大值
min(double a, double b):a、b中的較小值
random():返回0.0到1.0的隨機數
Long round(double a):double型的數據a轉換為Long型(四舍五入)
toDegrees(double angrad):弧度->角度
toRadians(double angdeg):角度->弧度
public class Text { /** * @param Math類 */ public static void main(String[] args) { double a1 = Math.random(); double a2 = Math.random(); System.out.println(Math.sqrt(a1*a1+a2*a2)); System.out.println(Math.pow(a1, 8)); System.out.println(Math.round(a1)); System.out.println(Math.log(Math.pow(Math.E,15))); }}
6、File類:
java.io.File類代表系統文件名(路徑和文件名)
public class file { /** * @param File類 */ public static void main(String[] args) { String separator = File.separator; String fileName = "myfile.txt";//文件名 String directory = "mydir1"+separator+"mydir2";//文件路徑 //String directory = "mydir1/mydir2"; //String directory = "mydir1//mydir2"; File f = new File(directory, fileName); if(f.exists()){//判斷文件是否存在 System.out.println("文件名:"+f.getName()); System.out.println("文件大?。?+f.length()); }else{ f.getParentFile().mkdirs();//創建文件路徑 try { f.createNewFile();//創建文件 } catch (IOException e) { e.printStackTrace(); } } }}
對于JAVA中的常用類,就總結到這里,大家可以多多查閱JAVA的幫助文檔,以便更好的理解JAVA中類的使用。
新聞熱點
疑難解答