Bluefire Bluefire - 2 years ago 152
Java Question

Netbeans GUI editor doesn't support ASCII - Java

So I'm making a basic GUI with the NetBeans IDE (in Java), and I want to make a button with a √ sign in it. It didn't let me copypaste it in, so I tried using its ASCII code -

char sqrt = (char) 251
. Instead of the square root sign, however, it gave me "û", and I have no idea why. Can someone please explain why this is happening, as well as offer an idea as to how I should go about this?

Answer Source

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

char sqrt = '\u221a';

since U+221A is the Unicode codepoint for the square root symbol.

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 that String, you will see

The square root of 2 (√2) is 1.4142
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download