How to type π (pi) in Java?

后端 未结 3 1097
深忆病人
深忆病人 2020-12-20 14:53

Is there a character within the Math API that allows for representing the character π?

3条回答
  •  佛祖请我去吃肉
    2020-12-20 15:12

    You don't even need to use the Math API. In Java source code, this:

    \u03C0
    

    is equivalent to this:

    π
    

    and you can even use it in an identifier; these two are equivalent:

    final String \u03C0 = "\u03C0";
    final String π = "π";
    

    (As the spec puts it:

    A Unicode escape of the form \uxxxx, where xxxx is a hexadecimal value, represents the UTF-16 code unit whose encoding is xxxx. This translation step allows any program to be expressed using only ASCII characters.

    See http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.2.)

提交回复
热议问题