15-02-常用对象API(String类-构造函数)

来源:互联网 发布:mac听写与语音没有了 编辑:程序博客网 时间:2024/05/23 22:41
package cn.itcast.string.demo;public class StringConstructDemo {public static void main(String[] args) {stringConstructDemo2();}//用String构造器只能将两种数组能变成字符串,一种byte,一种charprivate static void stringConstructDemo2() {char[] arr = {'w','a','p','q','s'};String s = new String(arr);//将数组连成字符串String s1 = new String(arr,2,3);//从数组arr中角标为2的元素开始取,取3个元素,组成字符串System.out.println("s="+s);System.out.println("s1="+s1);}private static void stringConstructDemo(){String s = new String();//等效于String s = "";不等效String s = null;byte[] arr = {65,66,67,68};String s1 = new String(arr);//arr数组传进去,会自动根据数值查ASC码表//真正开发时候有用,许多数据可变成字节存储,因为字节单位最小,而当需要观察这些数据时,可以将其转为字符串形式,一目了然System.out.println("s1="+s1);}}

0 0
原创粉丝点击