JAVA:String類、StringBuffer類、Math類。

JAVA:String類、StringBuffer類、Math類。
Java是一種真正的面向對象的語言,即使是開發簡單的程序,也必須設計對象。Java自身也爲我們提供了許多已設計好的類,要想靈活使用Java進行編程,熟悉Java的這些主要類將是必不可少的前提條件之一。

String 類
顧名思義,String是串的意思,這個類是字符串常量的類。相信使用過C語言進行編程的人都知道字符串是怎麼回事,這裏就不再進行贅述了。但有一點要說明的是,Java中的字符串和C語言中的字符串是有區別的。在C語言中,並沒有真正意義上的字符串,C語言中的字符串就是字符數組,使用起來非常的靈活。而在Java中,字符串常量是一個類??String類,它和字符數組是不同的。

下面先介紹一下String類的構造函數

public String()

這個構造函數用來創建一個空的字符串常量。

如:String test=new String();

或:String test;

test=new String();

public String(String value )


這個構造函數用一個已經存在的字符串常量作爲參數來創建一個新的字符串常量。

另外值得注意的是,Java會爲每個用雙引號"......"括起來的字符串常量創建一個String類的對象。如:String k="Hi."; Java會爲"Hi." 創建一個String類的對象,然後把這個對象賦值給k。等同於:

String temp=new String("Hi.");

String k=temp;


這個構造函數的用法如:

String test=new String(k); (注:k是一個String類的對象)

String test=new String("Hello, world.");

public String( char value[] )


這個構造函數用一個字符數組作爲參數來創建一個新的字符串常量。

用法如:

char z[]={'h','e','l','l','o'};

String test=new String(z);

(注:此時test中的內容爲"hello")

public String( char value[], int offset, int count )


這個構造函數是對上一個的擴充,用一句話來說,就是用字符數組value,從第offset個字符起取count個字符來創建一個String類的對象。用法如:

char z[]={'h','e','l','l','o'};

String test=new String(z,1,3);

(注:此時test中的內容爲"ell")

注意:數組中,下標0表示第一個元素,1表示第二個元素……

如果 起始點offset 或 截取數量count 越界,將會產生異常

StringIndexOutOfBoundsException

public String( StringBuffer buffer )


這個構造函數用一個StringBuffer類的對象作爲參數來創建一個新的字符串常量。

String類是字符串常量,而StringBuffer類是字符串變量,是不同的。StringBuffer類將在後面進行介紹。

String類的方法有:

public char charAt( int index )


這個方法用來獲取字符串常量中的一個字符。參數index指定從字符串中返回第幾個字符,這個方法返回一個字符型變量。用法如:

String s="hello";

char k=s.charAt(0);

(注:此時k的值爲'h')

public int compareTo( String anotherString )


這個方法用來比較字符串常量的大小,參數anotherString爲另一個字符串常量。若兩個字符串常量一樣,返回值爲0。若當前字符串常量大,則返回值大於0。若另一個字符串常量大,則返回值小於0。用法如:

String s1="abc";

String s2="abd";

int result=s2.compareTo(s1);

(注:result的值大於0,因爲d在ascII碼中排在c的後面,則s2>s1)

public String concat( String str )


這個方法將把參數??字符串常量str 接在當前字符串常量的後面,生成一個新的字符串常量,並返回。用法如: String s1="How do ";

String s2="you do?";

String ss=s1.concat(s2);

(注:ss的值爲"How do you do?")

public boolean startsWith( String prefix )


這個方法判斷當前字符串常量是不是以參數??prefix字符串常量開頭的。是,返回true。否,返回false。 用法如:

String s1="abcdefg";

String s2="bc";

boolean result=s1.startsWith(s2);

(注:result的值爲false)

public boolean startsWith( String prefix, int toffset )


這個重載方法新增的參數toffset指定 進行查找的起始點。

public boolean endsWith( String suffix )


這個方法判斷當前字符串常量是不是以參數??suffix字符串常量結尾的。是,返回true。否,返回false。用法如:

String s1="abcdefg";

String s2="fg";

boolean result=s1.endsWith(s2);

(注:result的值爲true)

public void getChars( int srcBegin, int srcEnd, char dst[], int dstBegin )


這個方法用來從字符串常量中截取一段字符串並轉換爲字符數組。

參數srcBegin爲截取的起始點,srcEnd爲截取的結束點,dst爲目標字符數組,dstBegin指定將截取的字符串放在字符數組的什麼位置。實際上,srcEnd爲截取的結束點加1,srcEnd-srcBegin爲要截取的字符數,用法如:

String s="abcdefg";

char z[]=new char[10];

s.getChars(2,4,z,0);

(注:z[0]的值爲'c',z[1]的值爲'd',截取了兩個字符 4-2=2)

public int indexOf( int ch )


這個方法的返回值爲 字符ch在字符串常量中從左到右第一次出現的位置。若字符串常量中沒有該字符,則返回-1。用法如:

String s="abcdefg";

int r1=s.indexOf('c');

int r2=s.indexOf('x');

(注:r1的值爲2,r2的值爲-1)

public int indexOf( int ch, int fromIndex )


這個方法是對上一個方法的重載,新增的參數fromIndex爲查找的起始點。用法如:

String s="abcdaefg";

int r=s.indexOf('a',1);

(注:r的值爲4)

public int indexOf( String str )


這個重載方法返回 字符串常量str在當前字符串常量中從左到右第一次出現的位置,若當前字符串常量中不包含字符串常量str,則返回-1。用法如:

String s="abcdefg";

int r1=s.indexOf("cd");

int r2=s.indexOf("ca");

(注:r1的值爲2,r2的值爲-1)

public int indexOf( String str, int fromIndex )


這個重載方法新增的參數fromIndex爲查找的起始點。

 

以下四個方法與上面的四個方法用法類似,只是在字符串常量中從右向左進行查找。

public int lastIndexOf( int ch )

public int lastIndexOf( int ch, int fromIndex )

public int lastIndexOf( String str )

public int lastIndexOf( String str, int fromIndex )

public int length()


這個方法返回字符串常量的長度,這是最常用的一個方法。用法如:

String s="abc";

int result=s.length();

(注:result的值爲3)

public char[] toCharArray()


這個方法將當前字符串常量轉換爲字符數組,並返回。用法如:

String s="Who are you?";

char z[]=s.toCharArray();

public static String valueOf( boolean b )

public static String valueOf( char c )

public static String valueOf( int i )

public static String valueOf( long l )

public static String valueOf( float f )

public static String valueOf( double d )


以上6個方法可將boolean、char、int、long、float和double 6種類型的變量轉換爲String類的對象。用法如:

String r1=String.valueOf(true); (注:r1的值爲"true")

String r2=String.valueOf('c'); (注:r2的值爲"c")

float ff=3.1415926;

String r3=String.valueOf(ff); (注:r3的值爲"3.1415926")


 

StringBuffer 類

String類是字符串常量,是不可更改的常量。而StringBuffer是字符串變量,它的對象是可以擴充和修改的。

String類的構造函數

public StringBuffer()


創建一個空的StringBuffer類的對象。 public StringBuffer( int length )


創建一個長度爲 參數length 的StringBuffer類的對象。

注意:如果參數length小於0,將觸發NegativeArraySizeException異常。

public StringBuffer( String str )


用一個已存在的字符串常量來創建StringBuffer類的對象。

StringBuffer類的方法有:

public String toString()


轉換爲String類對象並返回。由於大多數類中關於顯示的方法的參數多爲String類的對象,所以經常要將StringBuffer類的對象轉換爲String類的對象,再將它的值顯示出來。用法如:

StringBuffer sb=new StringBuffer("How are you?");

Label l1=new Label(sb.toString());

(注:聲明一個標籤對象l1,l1上的內容爲How are you?)

public StringBuffer append( boolean b )

public StringBuffer append( char c )
public StringBuffer append( int i)
public StringBuffer append( long l )

public StringBuffer append( float f )

public StringBuffer append( double d )


以上6個方法可將boolean、char、int、long、float和double 6種類型的變量追加到StringBuffer類的對象的後面。用法如:

double d=123.4567;

StringBuffer sb=new StringBuffer();

sb.append(true);

sb.append('c').append(d).append(99);

(注:sb的值爲truec123.456799)

public StringBuffer append( String str )


將字符串常量str追加到StringBuffer類的對象的後面。

public StringBuffer append( char str[] )


將字符數組str追加到StringBuffer類的對象的後面。

public StringBuffer append( char str[], int offset, int len )


將字符數組str,從第offset個開始取len個字符,追加到StringBuffer類的對象的後面。  

public StringBuffer insert( int offset, boolean b )

public StringBuffer insert( int offset, char c )

public StringBuffer insert( int offset, int i )

public StringBuffer insert( int offset, long l )

public StringBuffer insert( int offset, float f )

public StringBuffer insert( int offset, double d )

public StringBuffer insert( int offset, String str )

public StringBuffer insert( int offset, char str[] )


將boolean、char、int、long、float、double類型的變量、String類的對象或字符數組插入到StringBuffer類的對象中的第offset個位置。用法如:

StringBuffer sb=new StringBuffer("abfg");

sb.insert(2,"cde");

(注:sb的值爲abcdefg)

public int length()


這個方法返回字符串變量的長度,用法與String類的length方法類似。

 

Math 類

數學類包含了許多數學函數,如sin、cos、exp、abs等。Math類是一個工具類,它在解決與數學有關的一些問題是有着非常重要的作用。

這個類有兩個靜態屬性:E和PI。E代表數學中的e 2.7182818,而PI代表派pi 3.1415926。

引用時,用法如:Math.E 和 Math.Pi

這個類的方法有:

public static int abs( int a )

public static long abs( long a )

public static float abs( float a )

public static double abs( double a )


abs方法用來求絕對值。

public static native double acos( double a )


acos求反餘弦函數。

public static native double asin( double a )


asin求反正弦函數。

public static native double atan( double a )


atan求反正切函數。

public static native double ceil( double a )


ceil返回 最小的 大於a的整數。

public static native double cos( double a )


cos求餘弦函數。

public static native double exp( double a )


exp求e的a次冪。

public static native double floor( double a )


floor返回 最大的 小於a的整數。

public static native double log( double a )


log返回lna。

public static native double pow( double a, double b )


pow求a的b次冪。

public static native double sin( double a )


sin求正弦函數。

public static native double sqrt( double a )


sqrt求a的開平方。

public static native double tan( double a )


tan求正切函數。

public static synchronized double random()


返回0到1之間的隨機數。

使用這些方法時,用法爲Math.***** (*****爲方法名)。用法如:

int a=Math.abs(124);

int b=Math.floor(-5.2);

double s=Math.sqrt(7);


本篇文章來源於 :劉志猛博客 原文鏈接:http://www.liuzm.com/article/java/0963d.htm
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章