欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一篇文章帶你深入了解Java基礎(chǔ)

 更新時(shí)間:2021年08月02日 11:49:58   作者:zsr6135  
這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1、String類

1.1兩種對(duì)象實(shí)例化方式

對(duì)于String在之前已經(jīng)學(xué)習(xí)過了基本使用,就是表示字符串,那么當(dāng)時(shí)使用的形式采取了直接賦值:

public class StringText{
	public static void main(String args[]){
	String str =new String( "Hello");     //構(gòu)造方法
	System.out.print(str);
	}
}

對(duì)于String而言肯定是一個(gè)類,那么程序之中出現(xiàn)的str應(yīng)該就是這個(gè)類的對(duì)象,那么就證明以上的賦值操作實(shí)際上就表示要為String類的對(duì)象進(jìn)行實(shí)例化操作。

但String畢竟是一個(gè)類,那么類之中一定會(huì)存在構(gòu)造方法,String類的構(gòu)造:

public class StringText{
	public static void main(String args[]){
	String str =new String( "Hello");     //構(gòu)造方法
	System.out.print(str);
	}
}

發(fā)現(xiàn)現(xiàn)在也可以通過構(gòu)造方法為String類對(duì)象實(shí)例化。

1.2字符串比較

如果現(xiàn)在有兩個(gè)int型變量,如果想要知道是否相等,使用“==”進(jìn)行驗(yàn)證。

public class StringText{
	public static void main(String args[]){
	int x = 10;
	int y = 10;
	System.out.print(x==y);
	}
}

換成String

public class StringText{
	public static void main(String args[]){
		String str1 = "Hello";
		String str2 = new String("Hello");
		String str3 = str2;     //引用傳遞
		System.out.print(str1== str2);          //false
		System.out.print(str1== str3);          //false
		System.out.print(str2== str3);          //ture
	}       
}

image-20210730181004695

現(xiàn)在使用了“==”的確是完成了相等的判斷,但是最終判斷的是兩個(gè)對(duì)象(現(xiàn)在的對(duì)象是字符串)判斷是否相等,屬于數(shù)值判斷------判斷的是兩個(gè)對(duì)象的內(nèi)存地址數(shù)值,并沒有判斷內(nèi)容,而想要完成字符串內(nèi)容的判斷,則就必須使用到String類的操作方法:public Boolean equals(String str)(將方法暫時(shí)變了)

public class StringText{
	public static void main(String args[]){
		String str1 = "Hello";
		String str2 = new String("Hello");
		String str3 = str2;     //引用傳遞
		System.out.print(str1.equals(str2));          //ture
		System.out.print(str2.equals(str3));          //ture
		System.out.print(str2.equals(str3));          //ture
	}       
}

1.3字符串常量是String的匿名對(duì)象

如果在程序之中定義了字符串(使用“””),那么這個(gè)就表示一個(gè)String對(duì)象,因?yàn)樵诟鱾€(gè)語言之中沒有關(guān)于字符串?dāng)?shù)據(jù)類型的定義,而Java將其簡(jiǎn)單的處理了,所以感覺上存在了字符串?dāng)?shù)據(jù)類型。

**范例:**驗(yàn)證字符串是對(duì)象的概念

public class NiMing{
	public static void main(String args[]){
		String str = "Hello";
		System.out.print("Hello".equals(str));     //通過字符串調(diào)用方法
	}       
}

匿名對(duì)象可以調(diào)用類之中的方法與屬性,以上的字符串可以調(diào)用了equals()方法,那么它一定是一個(gè)對(duì)象。

**小技巧:**關(guān)于字符串與字符串常量的判斷

例如:在實(shí)際工作之中會(huì)有這樣一種操作,要求用戶輸入一個(gè)內(nèi)容,之后判斷此內(nèi)容是否與指定內(nèi)容相同。

public class NiMing{
	public static void main(String args[]){
		String str = "Hello";
        if(str.equals("Hello")){
		    System.out.print("條件滿足");
		}   
	}       
}

但,既然數(shù)據(jù)是用戶自己輸入,那么就有可能沒有輸入內(nèi)容。

public class TestDemo1{
	public static void main(String args[]){
		String str = null;
        if(str.equals("Hello")){
		    System.out.print("條件滿足");
		}   
	}       
}
//報(bào)錯(cuò)
    Exception in thread "main" java.lang.NullPointerException
        at NiMing.main(TestDemo1.java:4)
//現(xiàn)在將代碼反過來操作:
        public class TestDemo1{
	public static void main(String args[]){
		String str = null;
        if("Hello".equals(str)){
		    System.out.print("條件滿足");
		}   
	}       
}

因?yàn)樽址A渴悄涿麑?duì)象,匿名對(duì)象不可能為null。

1.4String兩種實(shí)例化方式區(qū)別

1、分析直接賦值方式

String str = "Hello";     //定義字符串

image-20210730181725468

發(fā)現(xiàn)現(xiàn)在只開辟額一塊堆內(nèi)存空間和一塊棧內(nèi)存空間。

2、構(gòu)造方法賦值

String  str = new String("Hello");

image-20210730182113204

使用構(gòu)造方法賦值的方式開辟的字符串對(duì)象,實(shí)際上會(huì)開辟兩塊空間,其中有一塊空間就愛那個(gè)成為垃圾。

public class TestDemo2{
public static void main(String args[]){
		String str1 = new String("Hello");
		String str2 = "Hello";   //入池
		String str3 = "Hello";  //使用池中對(duì)象
		System.out.print(str1==str2);          //false
		System.out.print(str2==str3);          // ture 
		System.out.print(str1==str3);          // false
	}       
}

通過上面的程序可以發(fā)現(xiàn),使用構(gòu)造方法實(shí)例化String對(duì)象,不會(huì)入池,只能自己使用??墒窃赟tring類之中為了方便操作提供了一種稱為手工入池的方法:public String intern()。

public class TestDemo2{
public static void main(String args[]){
		String str1 = new String("Hello").intern();    //手工入池
		String str2 = "Hello";   //入池
		String str3 = "Hello";  //使用池中對(duì)象
	    System.out.print(str1==str2);          //ture		 	 	    			System.out.print(str2==str3);          //ture
		System.out.print(str1==str3);          //ture
	}       
}

1.5字符串常量不可改變

字符串類的操作特點(diǎn)決定:字符串不可能去修改里面的內(nèi)容。

public class TestDemo3{
	public static void main(String args[]){
		String str = "Hello";
		str += "World";
		str += "!!!";
		System.out.print(str);
	}
}

image-20210730200637440

通過以上的代碼可以發(fā)現(xiàn),字符串內(nèi)容的更改,實(shí)際上改變的是字符串對(duì)象的引用過程,那么一下的代碼應(yīng)該盡量避免:

public class TestDemo3{
	public static void main(String args[]){
		String str = "Hello";
		for(int x=0;x<1000;x++){
			str += x;
		}
		System.out.print(str);
	}       
}
  • 字符串賦值只用直接賦值模式進(jìn)行完成
  • 字符串的比較采用equals()方法進(jìn)行實(shí)現(xiàn)
  • 字符串沒有特殊的情況不要改變太多

1.6開發(fā)中String必用

任何一個(gè)類的文檔由如下幾個(gè)部分組成

  • 類的相關(guān)定義,包括這個(gè)類的名字,有哪些父類,有哪些接口。
  • 類的相關(guān)簡(jiǎn)介。包括基本使用
  • 成員摘要(field):屬性就是一種成員,會(huì)列出所有成員的信息項(xiàng)
  • 構(gòu)造方法說明(Constructor),列出所有構(gòu)造方法的信息
  • 方法信息(Method),所有類中定義好的可以使用的方法
  • 成員、構(gòu)造、方法的詳細(xì)信息

1.7字符串和字符數(shù)組

字符串就是一個(gè)字符數(shù)組,所有在String類中有字符串轉(zhuǎn)變?yōu)樽址麛?shù)組,字符數(shù)組轉(zhuǎn)換為字符串的方法。

方法名稱 類型 描述
public String(char[] value) 構(gòu)造 將字符數(shù)組中的所有內(nèi)容變?yōu)樽址?/td>
public String(char[] value, int offset, int count) 構(gòu)造 將字符數(shù)組中的所有內(nèi)容變?yōu)樽址?offset-開始 count-個(gè)數(shù)
public char charAt(int index) 普通 返回char指定字符的索引值
public char[] toCharArray() 普通 將字符串轉(zhuǎn)化為字符數(shù)組

charAt方法

public class TestDemo4{
	public static void main(String args[]){
		String str = "Hello";
		System.out.println(str.charAt(0));
		//如果現(xiàn)在超過了字符串的長度,則會(huì)產(chǎn)生異常StringIndexOutOfBoundsException
		System.out.println(str.charAt(10));
	}
}

字符串和字符數(shù)組的轉(zhuǎn)化是重點(diǎn)

//字符串轉(zhuǎn)化為字符數(shù)組
public class TestDemo4{
	public static void main(String args[]){
		String str = "helloworld";
		char data [] = str.toCharArray();
		for(int i = 0; i < data.length; i++){
		data[i] -= 32;	//轉(zhuǎn)大寫字母簡(jiǎn)化模式更簡(jiǎn)單
			System.out.print(data[i] + "、");
		}
	}
}
//字符數(shù)組轉(zhuǎn)化為字符串
public class TestDemo4{
	public static void main(String args[]){
		String str = "helloworld";
		char data [] = str.toCharArray();
		for(int i = 0; i < data.length; i++){
		data[i] -= 32;	//轉(zhuǎn)大寫字母簡(jiǎn)化模式更簡(jiǎn)單
			System.out.print(data[i] + "、");
		}
		System.out.println();
		System.out.println(new String(data));//字符串?dāng)?shù)組全部轉(zhuǎn)化為字符數(shù)組
		System.out.println(new String(data,1,4));//字符串?dāng)?shù)組部分轉(zhuǎn)化為字符數(shù)組
	}
}

image-20210730204340346

判斷字符串是否由數(shù)字組成

public class TestDemo5{
	public static void main(String args[]){
		String str1 = "helloworld";
		String str = "1234567890";
		Judgenum(str);
		Judgenum(str1);
	}
	public static void Judgenum(String str){
		char data [] = str.toCharArray();
		boolean judge = true;
		for(int i = 0; i < data.length; i++){
			if(data[i]>= '0' && data[i]<= '9'){
				judge = false;
			}
		}
		if(judge){
			System.out.println(str+"是由字母組成");
		}else
			System.out.println(str+"是由數(shù)字組成");
	}
}

image-20210730214040542

1.8字節(jié)和字符串

方法名稱 類型 描述
public String(byte[] bytes) 構(gòu)造 將部分字節(jié)數(shù)組變?yōu)樽址?/td>
public String(byte[] bytes, int offset,int length) 構(gòu)造 將部分字節(jié)數(shù)組變?yōu)樽址?bytes——要解碼為字符的字節(jié) offset——要解碼的第一個(gè)字節(jié)的索引 length——要解碼的字節(jié)數(shù)
public byte[] getBytes() 普通 將字符串變?yōu)樽止?jié)數(shù)組
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException 普通 編碼轉(zhuǎn)換編碼
//將字符串通過字節(jié)流轉(zhuǎn)化為大寫
public class TestDemo6{
	public static void main(String args[]){
		String str = "helloworld";
		byte data [] = str.getBytes();//字符串轉(zhuǎn)換為字節(jié)數(shù)組
		for(int i = 0; i < data.length ; i++){
			System.out.print(data[i]+"、");
			data[i] -= 32;
		}
		System.out.println(new String(data));//字節(jié)數(shù)組轉(zhuǎn)化為字符串
	}
}

image-20210730221957667

一般情況下,在程序之中如果想要操作字節(jié)數(shù)組只有兩種情況:

**1、**需要進(jìn)行編碼的轉(zhuǎn)化;

2、 數(shù)據(jù)要進(jìn)行傳輸?shù)臅r(shí)候。

**3、**二進(jìn)制文件適合字節(jié)處理

1.9字符串比較

方法名稱 類型 描述
public boolean equals(String anObject) 普通 區(qū)分大小寫比較
public boolean equalsIgnoreCase(String anotherString) 普通 不區(qū)分大小寫比較
public int compareTo(String anotherString) 普通 比較兩個(gè)字符串的大小關(guān)系

如果現(xiàn)在要比較兩個(gè)字符串的大小關(guān)系,那么就必須使用comepareTo()方法完成,而這個(gè)方法返回int型數(shù)據(jù),而這個(gè)int型數(shù)據(jù)有三種結(jié)果:大于(返回結(jié)果大于0)、小于(返回結(jié)果小于0)、等于(返回結(jié)果為0).

public class CompareTo{
	public static void main(String args[]){
	String str1 = "HELLO";
	String str2= "hello";
	System.out.println(str1.compareTo(str2));
	}
}

1.10字符串查找

方法名稱 類型 描述
public boolean contains(String s) 普通 判斷一個(gè)子字符串是否村存在
public int indexOf(String str) 普通 返回字符串中第一次出現(xiàn)字符串的索引
public int indexOf(String str, int fromIndex) 普通 從指定地方開始查找子字符串的位置
public int lastIndexOf(String str) 普通 從后向前查找子字符串的位置
public int lastIndexOf(String str, int fromIndex) 普通 從指定位置由后向前查找
public boolean startsWith(String prefix) 普通 從頭判斷是否以某字符串開頭
public boolean startsWith(String prefix,int toffset) 普通 從指定位置判斷是否以字符串開頭
public boolean endsWith(String suffix) 普通 判斷以某字符串結(jié)尾
public class TestDemo7{
	public static void main(String args[]){
		String str = "helloworld";
		System.out.println(str.contains("world"));		//true
		//使用indexOf()進(jìn)行查找
		System.out.println(str.indexOf("world"));
		System.out.println(str.indexOf("java"));
		//JDK1,5之前這樣使用
		if(str.indexOf() != -1){
			System.out.println("可以查找到指定的內(nèi)容");
		}
	}
}
  • 基本上所有的查找現(xiàn)在都是通過contains()方法完成。
  • 需要注意的是,如果內(nèi)容重復(fù)indexOf()它只能返回查找的第一個(gè)位置。
  • 在進(jìn)行查找的時(shí)候往往會(huì)判斷開頭或結(jié)尾。
public class TestDemo7{
	public static void main(String args[]){
		String str = "**@@helloworld##";
		System.out.println(str.startsWith("**"));	//true
		System.out.println(str.startsWith("@@",2));	//true
		System.out.println(str.endsWith("##"));	//true
	}
}

1.11字符串的替換

方法名稱 類型 描述
public String replaceAll(String regex,String replacement) 普通 替換所有的內(nèi)容
public String replaceFirst(String regex,String replacement) 普通 替換首內(nèi)容
public class TestDemo7{
	public static void main(String args[]){
		String str = "**@@helloworld##";		
		System.out.println(str.replaceAll("l","_"));	//**@@he__owor_d##
	}
}

1.12字符串的拆分

方法名稱 類型 描述
public String[] split(String regex) 普通 將字符串全部拆分
public String[] split(String regex,int limit) 普通 將字符串部分拆分
public class TestDemo8{
	public static void main(String args[]){
		String str = "hello world hello zsr hello csdn";
		String result [] = str.split(" ");	//按照空格進(jìn)行拆分
		//hello、world、hello、zsr、hello、csdn、
		for(int i = 0; i < result.length ; i++){
			System.out.print(result[i]+"、");	
		}
		System.out.println();
		//部分拆分
		String result1 [] = str.split(" ",3);	//按照空格進(jìn)行拆分
		//第二個(gè)參數(shù) 從第幾個(gè)位置開始不進(jìn)行拆分操作
		//hello、world、hello zsr hello csdn、
		for(int i = 0; i < result1.length ; i++){
			System.out.print(result1[i]+"、");	
		}
	}
}
//拆分ip地址
public class TestDemo9{
//吳國發(fā)現(xiàn)內(nèi)容無法拆分,就需要用到“\\”進(jìn)行轉(zhuǎn)義
	public static void main(String args[]){
	//120
	//11
	//219
	//223:57114
		String str = "120.11.219.223:57114";
		String result [] = str.split("\\.");
		for(int i = 0 ; i < result.length ; i++){
			System.out.println(result[i]);
		}
	}
}

1.12字符串的截取

方法名稱 類型 描述
public String substring(int beginIndex) 普通 從指定位置截取到結(jié)尾
public String substring(int beginIndex,int endIndex) 普通 截取部分內(nèi)容
public class TestDemo10{
	public static void main(String args[]){
		String str = "helloworld";
		//world
		System.out.println(str.substring(5));
		//hello
		System.out.println(str.substring(0,5));	
	}
}

1.13其他操作方法

方法名稱 類型 描述
Public String trim() 普通 去掉左右空格,保留中間空格
public String toUpperCase() 普通 將全部字符串轉(zhuǎn)大寫
public String toLowerCase() 普通 將全部字符串轉(zhuǎn)小寫
public String intern() 普通 字符串入對(duì)象池
public String concat() 普通 字符串連接

思考題:

1.現(xiàn)在給出了如下一個(gè)字符串格式:“姓名:成績(jī)|姓名:成績(jī)|姓名:成績(jī)”,例如:給定的字符串是:“Tom:90|Jerry:80|tony:89”,要求可以對(duì)以上數(shù)據(jù)進(jìn)行處理,將數(shù)據(jù)按照如下的形式顯示:姓名:Tom,成績(jī):90;

public class Exam1{
	public static void main(String args[]){
		String str = "Tom:90|Jerry:80|tony:89";
		String data [] = str.split("\\|");
		for(int i = 0 ; i < data.length ; i++){
			String result [] = data[i].split(":");
			System.out.print("姓名 = " + result[0] + ",");
			System.out.println("年齡 = " + result[1]);
		}
		/*姓名 = Tom,年齡 = 90
		  姓名 = Jerry,年齡 = 80
		  姓名 = tony,年齡 = 89*/
	}
}

2.1、 給定一個(gè)email地址,要求驗(yàn)證其是否正確,提示:可以簡(jiǎn)單的驗(yàn)證一下,重點(diǎn)驗(yàn)證“@”和“.”。

標(biāo)準(zhǔn)如下:

1.email長度不短于5

2.@和.不能做開頭或結(jié)尾

3.@和.順序要有定義

public class Exam2{
	public static void main(String args[]){
		String email = "1016942589.@qqcom";
		char date[] = email.toCharArray();
		if (date.length>5&&email.startsWith("@")==false 
		&& email.startsWith(".")==false && email.endsWith("@")==false
		&&email.endsWith(".")==false && email.indexOf(".") >email.indexOf("@"))
		{System.out.println("正確");
		}else{System.out.println("錯(cuò)誤");}
	}
}

總結(jié)

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • MyBatis discriminator標(biāo)簽原理實(shí)例解析

    MyBatis discriminator標(biāo)簽原理實(shí)例解析

    這篇文章主要為大家介紹了MyBatis discriminator標(biāo)簽實(shí)現(xiàn)原理實(shí)例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • SpringBoot項(xiàng)目訪問任意接口出現(xiàn)401錯(cuò)誤的解決方案

    SpringBoot項(xiàng)目訪問任意接口出現(xiàn)401錯(cuò)誤的解決方案

    今天小編就為大家分享一篇關(guān)于SpringBoot項(xiàng)目訪問任意接口出現(xiàn)401錯(cuò)誤的解決方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 解決Shiro 處理ajax請(qǐng)求攔截登錄超時(shí)的問題

    解決Shiro 處理ajax請(qǐng)求攔截登錄超時(shí)的問題

    這篇文章主要介紹了解決Shiro 處理ajax請(qǐng)求攔截登錄超時(shí)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • JavaSE的類和對(duì)象你真的了解嗎

    JavaSE的類和對(duì)象你真的了解嗎

    這篇文章主要為大家詳細(xì)介紹了JavaSE的類和對(duì)象,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Java編程實(shí)現(xiàn)鄰接矩陣表示稠密圖代碼示例

    Java編程實(shí)現(xiàn)鄰接矩陣表示稠密圖代碼示例

    這篇文章主要介紹了Java編程實(shí)現(xiàn)鄰接矩陣表示稠密圖代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • SpringBoot整合Elasticsearch并實(shí)現(xiàn)CRUD操作

    SpringBoot整合Elasticsearch并實(shí)現(xiàn)CRUD操作

    這篇文章主要介紹了SpringBoot整合Elasticsearch并實(shí)現(xiàn)CRUD操作,需要的朋友可以參考下
    2018-03-03
  • Java線程同步的四種方式詳解

    Java線程同步的四種方式詳解

    這篇文章主要介紹了Java線程同步的四種方式詳解,需要的朋友可以參考下
    2023-02-02
  • SpringBoot使用jasypt實(shí)現(xiàn)數(shù)據(jù)庫信息脫敏的方法詳解

    SpringBoot使用jasypt實(shí)現(xiàn)數(shù)據(jù)庫信息脫敏的方法詳解

    這篇文章主要介紹了SpringBoot使用jasypt實(shí)現(xiàn)數(shù)據(jù)庫信息的脫敏,以此來保護(hù)數(shù)據(jù)庫的用戶名username和密碼password(容易上手,詳細(xì)),文中有詳細(xì)的圖文講解和代碼示例供大家參考,需要的朋友可以參考下
    2024-06-06
  • Spring Boot 2.0快速構(gòu)建服務(wù)組件全步驟

    Spring Boot 2.0快速構(gòu)建服務(wù)組件全步驟

    這篇文章主要給大家介紹了關(guān)于Spring Boot 2.0快速構(gòu)建服務(wù)組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot 2.0具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 使用Java將字符串在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換

    使用Java將字符串在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換

    大家都知道在一些情況下,我們需要特殊的編碼格式,如:UTF-8,但是系統(tǒng)默認(rèn)的編碼為ISO-8859-1,遇到這個(gè)問題,該如何對(duì)字符串進(jìn)行兩個(gè)編碼的轉(zhuǎn)換呢,下面小編給大家分享下java中如何在ISO-8859-1和UTF-8之間相互轉(zhuǎn)換,感興趣的朋友一起看看吧
    2021-12-12

最新評(píng)論