为什么键盘输入的super Unicode字符串在传递给方法后会发生变化?

问题描述 投票:0回答:0

为什么调用func(str2)后Scanner(System.in)输入的字符串“🍷Hello”打印错误?

java代码如下:

package test;
import java.util.Scanner;

public class TestCodePoint
{
    private static void func(String str)
    {
        System.out.println("after: " + str);
    }

    public static void main(String[] args)
    {
        String str = "🍷Hello";
        System.out.println("before: " + str);
        func(str);
        System.out.print("Please input a string: ");
        Scanner sc = new Scanner(System.in);
        String str2 = sc.next();
        System.out.println("str2_before: " + str);
        func(str2);
    }
}

执行结果为:

before: 🍷Hello
after: 🍷Hello
Please input a string: 🍷Hello   (Here is the paste input)
str2_before: 🍷Hello
after: ��Hello

为什么最后一个输出的是“��Hello”而不是“🍷Hello”?

java java.util.scanner surrogate-pairs
© www.soinside.com 2019 - 2024. All rights reserved.