JAVA基本语法--String

JAVA基本语法

String

  • String不是基本数据类型,属于引用数据类型

  • 使用方式与基本数据类型一致。例如

    String str="abcd";
    
  • 一个字符串可以串接另一个字符串,也可以直接串接其他类型的数据。例如

    String str="abcd";
    str=str+"xyz";
    int n=100;
    str=str+n;
    
  • String可以和8种基本数据类型变量做运算,且运算只能是连接运算:+,运算的结果仍然是String类型

    class Hello{
        public static void main(String[] args){
            char c='a';
            int num=10;
            String str="hello";
            System.out.println(c+num+str);//107hello
            System.out.println(c+str+num);//ahello10
            System.out.println(c+(num+str));//a10hello
            System.out.println((c+num)+str);//107hello
            System.out.println(str+num+c);//hello10a
    
            //要求输出  "*    *"
            System.out.println("*       *");     //可以输出
            System.out.println('*'+'\t'+'*');   //不可以输出
            System.out.println('*'+"\t"+'*');   //可以输出
            System.out.println('*'+'\t'+"*");   //不可以输出
            System.out.println('*'+('\t'+"*")); //可以输出
    
        }
    }
    //注意:只有String类型的变量遇到'+'可以进行拼接
    
posted @ 2022-01-01 17:01  ice--cream  阅读(72)  评论(0编辑  收藏  举报