문제

In order to get the char version of "\u221A" (aka the square root sign), would I just do the following:

String op = "\u221A";
char operator = op.charAt(0);

operator = '\u221A';

The last part doesn't seem right. Just wondering how I'd go about doing that. Thanks for your help!

도움이 되었습니까?

해결책

Using the hex literal directly seems simpler than either of the approaches you state, although they all have the same effect.

public class Test {
  public static void main(String[] args) {
    String op = "\u221A";
    char operator1 = op.charAt(0);
    char operator2 = '\u221a';
    char operator3 = 0x221a;
    System.out.println(operator1 == operator2);
    System.out.println(operator2 == operator3);
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top