字符串概述和特点以及字符串的创建

字符串的概述和特点

概述:字符串或串(String)是由数字、字母、下划线组成的一串字符。一般记为 s=“a1a2···an”(n>=0)。

   它是编程语言中表示文本的数据类型。

 

字符串的特点:

  1.字符串的内容不可改变

  2.因为字符串是不可改变的,所以字符串是可以共同使用的

  3.字符串效果上相当于char[]字符数组,但是底层原理是byte[]字节数组

 

 

字符串的创建

创建字符串的常见的4中方式:

  1. String()  创建一个空白字符串,不包含任何内容

  2.String(char[] array)  根据字符数组的内容来创建字符串

  3.String(byte[] array)  根据字节数组的内容来创建字符串

  4.直接创建 String 名称= “”;右边直接使用双引号

    

    public static void main(String[] args) {
        //使用空参构造
        String str = new String();
        System.out.println(str);
        //使用字符数组创建字符串
        char[] arr = {'A','B','C'};
        String s = new String(arr);
        System.out.println(s);
        //使用字节数组创建字符串
        byte[] bytes = {97,98,99};
        String s1 = new String(bytes);
        System.out.println(s1);
        //直接创建
        String s2 = "Hello World";
        System.out.println(s2);
    }

 

posted @ 2022-06-30 09:05  xjw12345  阅读(1118)  评论(0)    收藏  举报