java-String

package ch3;

import org.junit.Test;

import java.util.Arrays;
import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("What is your name?");
        String name = sc.nextLine();//读取一行
        String msg = sc.next();//读取一个单词
        //li hua
        //lihua 1
        //li hua,lihua
        System.out.println(name + "," + msg);
    }

    @Test
    public void test1() {
        double x = 4;
        double y = Math.sqrt(x);
        System.out.println(y);//2.0

        double y1 = Math.pow(x, 3);
        System.out.println(y1);

        int a = 1000000000;//10亿
        long a_L = 1000000000L;//10亿
        System.out.println(a * 3);//-1294967296
        System.out.println(a_L * 3);//3000000000
//        System.out.println(Math.multiplyExact(a,3));//integer overflow
        System.out.println(Math.multiplyExact(a_L, 3));//3000000000
        System.out.println("-------------");
    }

    @Test
    public void test2() {
        String s = "I am ";
        String name = "LiHua";
        String message = s + name;
        System.out.println(message);//I am LiHua

        int num = 76;
        message = s + num;
        System.out.println(message);//I am 76

        String all = String.join("@", "S", "M", "L", "XL");
        System.out.println(all);//S@M@L@XL

        String repeated = "Java".repeat(3);//Java 11才支持
        System.out.println(repeated);//JavaJavaJava

        String greeting = "Hello!";//String类不可变
        greeting = greeting.substring(0, 3) + "p!";
        System.out.println(greeting);//Help!

        String hello = "Hello";//== 比较地址,equals 比较字面值
        System.out.println("Hello".equals(hello));//true
        System.out.println("hello".equals(hello));//false
        System.out.println("hello".equalsIgnoreCase(hello));//true
        System.out.println("Hello" == hello);//true
        System.out.println("Hel".equals(hello.substring(0, 3)));//true
        System.out.println("Hel" == hello.substring(0, 3));//false
    }

    @Test
    public void test3(){
        String str = "";
        String str1 = null;
        if (str.length() == 0)
            System.out.println("空");
        if (str.equals(""))
            System.out.println("空");
        if (str1 != null)
            System.out.println("null");

        String str2 = "    Hello   ";
        System.out.println(str2.trim());//Hello

        StringBuilder sb = new StringBuilder();
        sb.append('I');
        sb.append(" am LiHua");
        System.out.println(sb);//I am LiHua
        System.out.println(sb.toString());//I am LiHua

        char[] passwd="1242".toCharArray();
        String str3=String.valueOf(passwd);
        System.out.println(str3);//1242
        String str4= Arrays.toString(passwd);
        System.out.println(str4);//[1, 2, 4, 2]
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值