day08-常用类_String的构造方法
This commit is contained in:
37
day08/src/com/inmind/string01/Demo02.java
Normal file
37
day08/src/com/inmind/string01/Demo02.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.inmind.string01;
|
||||
/*
|
||||
String类的构造方法:创建String类的对象
|
||||
|
||||
String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本
|
||||
public String(char[] value) :通过当前参数中的字符数组来构造新的String。
|
||||
public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。
|
||||
*/
|
||||
public class Demo02 {
|
||||
public static void main(String[] args) {
|
||||
//public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String
|
||||
byte[] bytes = {97,98,99,100};//在ASCII码表中,‘a’---97 '0' ------48
|
||||
//将字节数组转为字符串
|
||||
String str = new String(bytes);
|
||||
System.out.println(str);//abcd
|
||||
}
|
||||
|
||||
public static void method2(String[] args) {
|
||||
//public String(char[] value) :通过当前参数中的字符数组来构造新的String。
|
||||
//将字符数组转为字符,它的作用就是转换
|
||||
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 s = "abcd";
|
||||
//通过构造方法创建一个对象
|
||||
String str = new String(s);
|
||||
System.out.println(s);//abcd
|
||||
System.out.println(str);//abcd
|
||||
System.out.println(s == str);//false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user