導航:首頁 > 編程語言 > java語句類型

java語句類型

發布時間:2022-10-05 14:01:22

java語句

String : 字元串類型

一、構造函數
String(byte[ ] bytes):通過byte數組構造字元串對象。
String(char[ ] value):通過char數組構造字元串對象。
String(Sting original):構造一個original的副本。即:拷貝一個original。
String(StringBuffer buffer):通過StringBuffer數組構造字元串對象。
例如:
byte[] b = {'a','b','c','d','e','f','g','h','i','j'};
char[] c = {'0','1','2','3','4','5','6','7','8','9'};
String sb = new String(b); //abcdefghij
String sb_sub = new String(b,3,2); //de
String sc = new String(c); //0123456789
String sc_sub = new String(c,3,2); //34
String sb_ = new String(sb); //abcdefghij
System.out.println("sb:"+sb);
System.out.println("sb_sub:"+sb_sub);
System.out.println("sc:"+sc);
System.out.println("sc_sub:"+sc_sub);
System.out.println("sb_:"+sb_);
輸出結果:sb:abcdefghij
sb_sub:de
sc:0123456789
sc_sub:34
sb_:abcdefghij

二、方法:

說明:①、所有方法均為public。
②、書寫格式: [修飾符] <返回類型><方法名([參數列表])>

例如:static int parseInt(String s)
表示此方法(parseInt)為類方法(static),返回類型為(int),方法所需要為String類型。

1. char charAt(int index) :取字元串中的某一個字元,其中的參數index指的是字元串中序數。字元串的序數從0開始到length()-1 。
例如:String s = new String("abcdefghijklmnopqrstuvwxyz");
System.out.println("s.charAt(5): " + s.charAt(5) );
結果為: s.charAt(5): f
2. int compareTo(String anotherString) :當前String對象與anotherString比較。相等關系返回0;不相等時,從兩個字元串第0個字元開始比較,返回第一個不相等的字元差,另一種情況,較長字元串的前面部分恰巧是較短的字元串,返回它們的長度差。
3. int compareTo(Object o) :如果o是String對象,和2的功能一樣;否則拋出ClassCastException異常。
例如:String s1 = new String("abcdefghijklmn");
String s2 = new String("abcdefghij");
String s3 = new String("abcdefghijalmn");
System.out.println("s1.compareTo(s2): " + s1.compareTo(s2) ); //返回長度差
System.out.println("s1.compareTo(s3): " + s1.compareTo(s3) ); //返回'k'-'a'的差
結果為:s1.compareTo(s2): 4
s1.compareTo(s3): 10
4. String concat(String str) :將該String對象與str連接在一起。
5. boolean contentEquals(StringBuffer sb) :將該String對象與StringBuffer對象sb進行比較。
6. static String ValueOf(char[] data) :
7. static String ValueOf(char[] data, int offset, int count) :這兩個方法將char數組轉換成String,與其中一個構造函數類似。
8. boolean endsWith(String suffix) :該String對象是否以suffix結尾。
例如:String s1 = new String("abcdefghij");
String s2 = new String("ghij");
System.out.println("s1.endsWith(s2): " + s1.endsWith(s2) );
結果為:s1.endsWith(s2): true
9. boolean equals(Object anObject) :當anObject不為空並且與當前String對象一樣,返回true;否則,返回false。
10. byte[] getBytes() :將該String對象轉換成byte數組。
11. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) :該方法將字元串拷貝到字元數組中。其中,srcBegin為拷貝的起始位置、srcEnd為拷貝的結束位置、字元串數值dst為目標字元數組、dstBegin為目標字元數組的拷貝起始位置。
例如:char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!
String s2 = new String("you!"); s2.getChars(0,3,s1,7); //s1=I love you!
System.out.println( s1 );
結果為:I love you!
12. int hashCode() :返回當前字元的哈希表碼。
13. int indexOf(int ch) :只找第一個匹配字元位置。
14. int indexOf(int ch, int fromIndex) :從fromIndex開始找第一個匹配字元位置。
15. int indexOf(String str) :只找第一個匹配字元串位置。
16. int indexOf(String str, int fromIndex) :從fromIndex開始找第一個匹配字元串位置。
例如:String s = new String("write once, run anywhere!");
String ss = new String("run");
System.out.println("s.indexOf('r'): " + s.indexOf('r') );
System.out.println("s.indexOf('r',2): " + s.indexOf('r',2) );
System.out.println("s.indexOf(ss): " + s.indexOf(ss) );
結果為:s.indexOf('r'): 1
s.indexOf('r',2): 12
s.indexOf(ss): 12
17. int lastIndexOf(int ch)
18. int lastIndexOf(int ch, int fromIndex)
19. int lastIndexOf(String str)
20. int lastIndexOf(String str, int fromIndex) 以上四個方法與13、14、15、16類似,不同的是:找最後一個匹配的內容。
public class CompareToDemo {
public static void main (String[] args) {
String s1 = new String("acbdebfg");

System.out.println(s1.lastIndexOf((int)'b',7));
}
}
運行結果:5
(其中fromIndex的參數為 7,是從字元串acbdebfg的最後一個字元g開始往前數的位數。既是從字元c開始匹配,尋找最後一個匹配b的位置。所以結果為 5)

21. int length() :返回當前字元串長度。
22. String replace(char oldChar, char newChar) :將字元號串中第一個oldChar替換成newChar。
23. boolean startsWith(String prefix) :該String對象是否以prefix開始。
24. boolean startsWith(String prefix, int toffset) :該String對象從toffset位置算起,是否以prefix開始。
例如:String s = new String("write once, run anywhere!");
String ss = new String("write");
String sss = new String("once");
System.out.println("s.startsWith(ss): " + s.startsWith(ss) );
System.out.println("s.startsWith(sss,6): " + s.startsWith(sss,6) );
結果為:s.startsWith(ss): true
s.startsWith(sss,6): true
25. String substring(int beginIndex) :取從beginIndex位置開始到結束的子字元串。
26.String substring(int beginIndex, int endIndex) :取從beginIndex位置開始到endIndex位置的子字元串。
27. char[ ] toCharArray() :將該String對象轉換成char數組。
28. String toLowerCase() :將字元串轉換成小寫。
29. String toUpperCase() :將字元串轉換成大寫。
例如:String s = new String("java.lang.Class String");
System.out.println("s.toUpperCase(): " + s.toUpperCase() );
System.out.println("s.toLowerCase(): " + s.toLowerCase() );
結果為:s.toUpperCase(): JAVA.LANG.CLASS STRING
s.toLowerCase(): java.lang.class string
30. static String valueOf(boolean b)
31. static String valueOf(char c)
32. static String valueOf(char[] data)
33. static String valueOf(char[] data, int offset, int count)
34. static String valueOf(double d)
35. static String valueOf(float f)
36. static String valueOf(int i)
37. static String valueOf(long l)
38. static String valueOf(Object obj)
以上方法用於將各種不同類型轉換成Java字元型。這些都是類方法。

Java中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 oldchar,char newChar)
在字元串中用newChar字元替換oldChar字元
public boolean startsWith(String prefix)
判斷字元串是否以prefix字元串開頭;
public boolean endsWith(String suffix)
判斷一個字元串是否以suffix字元串結尾;
public String toUpperCase()
返回一個字元串為該字元串的大寫形式;
public String toLowerCase()
返回一個字元串為該字元串的小寫形式
public String substring(int beginIndex)
返回該字元串從beginIndex開始到結尾的子字元串;
public String substring(int beginIndex,int endIndex)
返回該字元串從beginIndex開始到endsIndex結尾的子字元串
public String trim()
返回該字元串去掉開頭和結尾空格後的字元串
public String[] split(String regex)
將一個字元串按照指定的分隔符分隔,返回分隔後的字元串數組
實例:
public class SplitDemo{
public static void main (String[] args) {

String date = "2008/09/10";
String[ ] dateAfterSplit= new String[3];
dateAfterSplit=date.split("/"); //以「/」作為分隔符來分割date字元串,並把結果放入3個字元串中。

for(int i=0;i<dateAfterSplit.length;i++)
System.out.print(dateAfterSplit[i]+" ");
}
}

運行結果:2008 09 10 //結果為分割後的3個字元串

實例:
TestString1.java:
程序代碼
public class TestString1
{
public static void main(String args[]) {
String s1 = "Hello World" ;
String s2 = "hello world" ;
System.out.println(s1.charAt(1)) ;
System.out.println(s2.length()) ;
System.out.println(s1.indexOf("World")) ;
System.out.println(s2.indexOf("World")) ;
System.out.println(s1.equals(s2)) ;
System.out.println(s1.equalsIgnoreCase(s2)) ;

String s = "我是J2EE程序員" ;
String sr = s.replace('我','你') ;
System.out.println(sr) ;
}
}

TestString2.java:
程序代碼

public class TestString2
{
public static void main(String args[]) {
String s = "Welcome to Java World!" ;
String s2 = " magci " ;
System.out.println(s.startsWith("Welcome")) ;
System.out.println(s.endsWith("World")) ;
String sL = s.toLowerCase() ;
String sU = s.toUpperCase() ;
System.out.println(sL) ;
System.out.println(sU) ;
String subS = s.substring(11) ;
System.out.println(subS) ;
String s1NoSp = s2.trim() ;
System.out.println(s1NoSp) ;
}
}

=是賦值 比如 int a = 0; int b = 1; a = b; 那麼把b的值賦值給a a為1
== 返回一個boolean值(真或者假)
如上面的 int a = 0; int b = 1; a==b(返回值為false假);
把b賦值給a a = b; 這時再 a==b(返回值就為true真).

⑵ java中的語句有哪些吖

public static void main(String args[]){}
public static void main(String[] args){}
是一樣的,數組的申明方式有兩種一種就是[]在前,一種在後
JAVA的語句很多,無法片面的作出回答
介意看一些java基礎的書,慢慢的你就懂了
切記,先要學會走在學會跑!
加油^_^

⑶ java中條件語句包含哪些語法類型以及如何應用

在 Java 中有兩種類型的條件判斷語句,它們分別是:

if 語句

switch 語句

if 語句:

if 語句由一個布爾表達式後跟一個或多個語句組成。

語法

if 語句的語法是:

if(Boolean_expression)
{
//Statements will execute if the Boolean expression is true
}

⑷ 請說明Java語言定義了哪些基本數據類型並寫出相應的變數定義語句(提示:需寫6

四種八類:

基本數據類型

整數類型:

byte:位元組佔用 1位元組 8位,用來表達最小的數據單位,儲存數據長度為 正負 127;

short:位元組佔用 2位元組 16位,儲存數值長度為 -32768-32767

int:位元組佔用 4位元組 32位,最為常用的整數類型,儲存長度為,-2^31-1~2^31 (21 億)

long:位元組佔用 8位元組 64位,當到達int數值極限時使用,儲存長度為 看圖片:



浮點數類型:

float:單精度浮點型 32位 取值范圍 3.4e-38~3.4e-38

double:雙精度浮點型 64位 ,java中默認的浮點類型 取值范圍 1.7e-308~1.7e-308


字元類型:

char:16位 ,java字元使用Unicode編碼;


布爾類型

boolean : true 真 和 false 假


引用數據類型:

類 class

介面 interface

數組

⑸ java條件語句包括哪些語法類型,每種類型如何應用

if ,if else,if else if else,if else if ... else,switch case break .. default break,while,do while,for

⑹ JAVA語言分幾類

java提供了兩類數據類型:原始類型(基本類型),引用類型。
1、原始類型包括:boolean
,byte,char,int,short,float,long,double,值得注意的是:在原始數據類型中,除了boolean類型所佔長度與平台有關外,其他數據類型長度都是與平台無關的。比如,int永遠佔4個位元組。
2、引用類型常見的有:String,StringBuffer,ArrayList,HashSet,HashMap等。

閱讀全文

與java語句類型相關的資料

熱點內容
十三排電影院坐第幾排 瀏覽:122
尼故福利院 瀏覽:602
哪有好看的電影網站 瀏覽:773
紅顏薄命女斗小說 瀏覽:940
法國電影戀愛love2012電影完整版 瀏覽:459
在線影視 不卡 瀏覽:168
老男孩韓國完整版百度網盤 瀏覽:485
用箱子運水怪結果被放出來了電影 瀏覽:519
徐錦江空中飛人片名 瀏覽:164
手機免費在線看福利電影 瀏覽:457
羅麗星克萊爾經典 瀏覽:342
台灣紅羊有哪些經典電影 瀏覽:568
免下載你懂的 瀏覽:975
新建文件夾1女演員三位 瀏覽:740
不用下載就能看的視頻網站 瀏覽:330
我一個神偷硬生生把國家偷成強國 瀏覽:600
樣子是五歲小男孩和郭富城演的 瀏覽:460
韓國演員也美娜 瀏覽:898
陸離是哪部小說的主角 瀏覽:49