day08-常用类_String的构造方法
This commit is contained in:
@@ -0,0 +1,44 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
/*
|
||||||
|
String的构造方法的作用:将其他类型的数据,转换成String
|
||||||
|
String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本
|
||||||
|
public String(char[] value) :通过当前参数中的字符数组来构造新的String。
|
||||||
|
public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来
|
||||||
|
|
||||||
|
String的构造方法,有转换的功能,可以将字节数据,字符数据,转换为我们看得懂的字符串
|
||||||
|
*/
|
||||||
|
public class Demo02 {
|
||||||
|
//public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组,创建一个新的String。
|
||||||
|
public static void main(String[] args) {
|
||||||
|
byte[] bytes = {97,98,99};//在ascii码表中,'a'的ASCII码是97
|
||||||
|
//通过构造方法创建对象,将字节数组的内容拼成新的字符串,该API主要使用于IO流数据的传输与解析
|
||||||
|
String str = new String(bytes);
|
||||||
|
System.out.println(str);//abc
|
||||||
|
}
|
||||||
|
|
||||||
|
//public String(char[] value) :通过当前参数中的字符数组来构造新的String。
|
||||||
|
public static void charArrMethod2(String[] args) {
|
||||||
|
//字符串就是一串字符
|
||||||
|
char[] chars = {'a','b','c'};
|
||||||
|
//通过构造方法创建对象
|
||||||
|
String str = new String(chars);
|
||||||
|
System.out.println(str);//abc
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//String(String original) 构造方法
|
||||||
|
public static void method1(String[] args) {
|
||||||
|
//String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本
|
||||||
|
String str1 = "abc";
|
||||||
|
String str2 = "abc";
|
||||||
|
String str3 = new String("abc");
|
||||||
|
System.out.println(str1);
|
||||||
|
System.out.println(str2);
|
||||||
|
System.out.println(str3);
|
||||||
|
//简单的面试题
|
||||||
|
System.out.println(str1 == str2);//true
|
||||||
|
System.out.println(str1 == str3);//false
|
||||||
|
System.out.println(str2 == str3);//false
|
||||||
|
System.out.println("--------------------------------------------------");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user