Netbeans Graphics Editor Doesn't Support ASCII-Java

So, I am creating a basic GUI with NetBeans IDE (in Java), and I want to make a button with a โˆš sign in it. He did not allow me to copy it, so I tried to use its ASCII code - char sqrt = (char) 251 . However, instead of the square root sign, he gave me "รป", and I have no idea why. Can someone explain why this is happening and also offer an idea on how I should do this?

+4
source share
2 answers

Java characters are Unicode, not ASCII. Unicode codepoint 251 ( U + 00FB ) is the Latin Small Letter U with Circumflex. To enter various Unicode characters using a character set with only basic ASCII characters, Java provides a way to enter Unicode characters using a literal format. So you can do this:

 char sqrt = '\u221a'; 

since U + 221A is the Unicode code number for the square root character.

This \ uXXXX format can also be used in string literals:

 String s = "The square root of 2 (\u221a2) is approximately 1.4142"; 

If you print this line, you will see

 The square root of 2 (โˆš2) is 1.4142 
+4
source

Java uses Unicode, and the Unicode value for "โˆš" is 8730. So this should do it:

 char sqrt = 8730; 
+3
source

All Articles