day12-----------常見對象(傳智視頻)

package cn.itcast_03;

import java.util.Scanner;

/*
 * 經常使用的兩個方法:
 * 		public int nextInt():獲取一個int類型的值
 * 		public String nextLine():獲取一個String類型的值
 * 
 * 出現問題了:
 * 		先獲取一個數值,在獲取一個字符串,會出現問題。
 * 		主要緣由:就是那個換行符號的問題。
 * 如何解決呢?
 * 		A:先獲取一個數值後,在建立一個新的鍵盤錄入對象獲取字符串。
 * 		B:把全部的數據都先按照字符串獲取,而後要什麼,你就對應的轉換爲何。
 */
public class ScannerDemo {
	public static void main(String[] args) {
		// 建立對象
		Scanner sc = new Scanner(System.in);

		// 獲取兩個int類型的值
		// int a = sc.nextInt();
		// int b = sc.nextInt();
		// System.out.println("a:" + a + ",b:" + b);
		// System.out.println("-------------------");

		// 獲取兩個String類型的值
		// String s1 = sc.nextLine();
		// String s2 = sc.nextLine();
		// System.out.println("s1:" + s1 + ",s2:" + s2);
		// System.out.println("-------------------");

		// 先獲取一個字符串,在獲取一個int值
		// String s1 = sc.nextLine();
		// int b = sc.nextInt();
		// System.out.println("s1:" + s1 + ",b:" + b);
		// System.out.println("-------------------");

		// 先獲取一個int值,在獲取一個字符串
		// int a = sc.nextInt();
		// String s2 = sc.nextLine();
		// System.out.println("a:" + a + ",s2:" + s2);
		// System.out.println("-------------------");

		int a = sc.nextInt();
		Scanner sc2 = new Scanner(System.in);
		String s = sc2.nextLine();
		System.out.println("a:" + a + ",s:" + s);
	}
}
package cn.itcast_01;

/*
 * 字符串:就是由多個字符組成的一串數據。也能夠當作是一個字符數組。
 * 經過查看API,咱們能夠知道
 * 		A:字符串字面值"abc"也能夠當作是一個字符串對象。
 * 		B:字符串是常量,一旦被賦值,就不能被改變。
 * 
 * 構造方法:
 * 		public String():空構造
 *		public String(byte[] bytes):把字節數組轉成字符串
 *		public String(byte[] bytes,int index,int length):把字節數組的一部分轉成字符串
 *		public String(char[] value):把字符數組轉成字符串
 *		public String(char[] value,int index,int count):把字符數組的一部分轉成字符串
 *		public String(String original):把字符串常量值轉成字符串
 *
 * 字符串的方法:
 * 		public int length():返回此字符串的長度。
 */
public class StringDemo {
	public static void main(String[] args) {
		// public String():空構造
		String s1 = new String();
		System.out.println("s1:" + s1);
		System.out.println("s1.length():" + s1.length());
		System.out.println("--------------------------");

		// public String(byte[] bytes):把字節數組轉成字符串
		byte[] bys = { 97, 98, 99, 100, 101 };
		String s2 = new String(bys);
		System.out.println("s2:" + s2);
		System.out.println("s2.length():" + s2.length());
		System.out.println("--------------------------");

		// public String(byte[] bytes,int index,int length):把字節數組的一部分轉成字符串
		// 我想獲得字符串"bcd"
		String s3 = new String(bys, 1, 3);
		System.out.println("s3:" + s3);
		System.out.println("s3.length():" + s3.length());
		System.out.println("--------------------------");

		// public String(char[] value):把字符數組轉成字符串
		char[] chs = { 'a', 'b', 'c', 'd', 'e', '愛', '林', '親' };
		String s4 = new String(chs);
		System.out.println("s4:" + s4);
		System.out.println("s4.length():" + s4.length());
		System.out.println("--------------------------");

		// public String(char[] value,int index,int count):把字符數組的一部分轉成字符串
		String s5 = new String(chs, 2, 4);
		System.out.println("s5:" + s5);
		System.out.println("s5.length():" + s5.length());
		System.out.println("--------------------------");
		
		//public String(String original):把字符串常量值轉成字符串
		String s6 = new String("abcde");
		System.out.println("s6:" + s6);
		System.out.println("s6.length():" + s6.length());
		System.out.println("--------------------------");
		
		//字符串字面值"abc"也能夠當作是一個字符串對象。
		String s7 = "abcde";
		System.out.println("s7:"+s7);
		System.out.println("s7.length():"+s7.length());
	}
}

值不能被改變,可是引用能夠改變java

/*
 * String s = new String(「hello」)和String s = 「hello」;的區別?
 * 有。前者會建立2個對象,後者建立1個對象。
 * 
 * ==:比較引用類型比較的是地址值是否相同
 * equals:比較引用類型默認也是比較地址值是否相同,而String類重寫了equals()方法,比較的是內容是否相同。
 */
public class StringDemo2 {
	public static void main(String[] args) {
		String s1 = new String("hello");
		String s2 = "hello";

		System.out.println(s1 == s2);// false
		System.out.println(s1.equals(s2));// true
	}
}

package cn.itcast_02;

/*
 * 看程序寫結果
 * 字符串若是是變量相加,先開空間,在拼接。
 * 字符串若是是常量相加,是先加,而後在常量池找,若是有就直接返回,不然,就建立。
 
   "=="比較的是地址,"equals"比較的是內容
 */
public class StringDemo4 {
	public static void main(String[] args) {
		String s1 = "hello";
		String s2 = "world";
		String s3 = "helloworld";
		System.out.println(s3 == s1 + s2);// false
		System.out.println(s3.equals((s1 + s2)));// true

		System.out.println(s3 == "hello" + "world");// false 這個咱們錯了,應該是true
		System.out.println(s3.equals("hello" + "world"));// true

		// 經過反編譯看源碼,咱們知道這裏已經作好了處理。
		// System.out.println(s3 == "helloworld");
		// System.out.println(s3.equals("helloworld"));
	}
}
/*
 * String類的獲取功能
 * int length():獲取字符串的長度。
 * char charAt(int index):獲取指定索引位置的字符
 * int indexOf(int ch):返回指定字符在此字符串中第一次出現處的索引。
 * 		爲何這裏是int類型,而不是char類型?
 * 		緣由是:'a'和97其實均可以表明'a'
 * int indexOf(String str):返回指定字符串在此字符串中第一次出現處的索引。
 * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中從指定位置後第一次出現處的索引。
 * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中從指定位置後第一次出現處的索引。
 * String substring(int start):從指定位置開始截取字符串,默認到末尾。
 * String substring(int start,int end):從指定位置開始到指定位置結束截取字符串。
 */
package cn.itcast_05;

/*
 * String的轉換功能:
 * byte[] getBytes():把字符串轉換爲字節數組。
 * char[] toCharArray():把字符串轉換爲字符數組。
 * static String valueOf(char[] chs):把字符數組轉成字符串。
 * static String valueOf(int i):把int類型的數據轉成字符串。
 * 		注意:String類的valueOf方法能夠把任意類型的數據轉成字符串。
 * String toLowerCase():把字符串轉成小寫。
 * String toUpperCase():把字符串轉成大寫。
 * String concat(String str):把字符串拼接。
 */
public class StringDemo {
	public static void main(String[] args) {
		// 定義一個字符串對象
		String s = "JavaSE";

		// byte[] getBytes():把字符串轉換爲字節數組。
		byte[] bys = s.getBytes();
		for (int x = 0; x < bys.length; x++) {
			System.out.println(bys[x]);
		}
		System.out.println("----------------");

		// char[] toCharArray():把字符串轉換爲字符數組。
		char[] chs = s.toCharArray();
		for (int x = 0; x < chs.length; x++) {
			System.out.println(chs[x]);
		}
		System.out.println("----------------");

		// static String valueOf(char[] chs):把字符數組轉成字符串。
		String ss = String.valueOf(chs);
		System.out.println(ss);
		System.out.println("----------------");

		// static String valueOf(int i):把int類型的數據轉成字符串。
		int i = 100;
		String sss = String.valueOf(i);
		System.out.println(sss);
		System.out.println("----------------");

		// String toLowerCase():把字符串轉成小寫。
		System.out.println("toLowerCase:" + s.toLowerCase());
		System.out.println("s:" + s);
		// System.out.println("----------------");
		// String toUpperCase():把字符串轉成大寫。
		System.out.println("toUpperCase:" + s.toUpperCase());
		System.out.println("----------------");

		// String concat(String str):把字符串拼接。
		String s1 = "hello";
		String s2 = "world";
		String s3 = s1 + s2;
		String s4 = s1.concat(s2);
		System.out.println("s3:"+s3);
		System.out.println("s4:"+s4);
	}
}
/*
 * String類的其餘功能:
 * 
 * 替換功能:
 * String replace(char old,char new)
 * String replace(String old,String new)
 *
 * 去除字符串兩端空格	
 * String trim()
 * 
 * 按字典順序比較兩個字符串  
 * int compareTo(String str)
 * int compareToIgnoreCase(String str)
 */

debug 中 F5 進入子函數
數組

相關文章
相關標籤/搜索