Question

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!

Was it helpful?

Solution

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);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top