33

This question might be related to today's XKCD.

I discovered today that emoji could be used in variable names.

int ಢ_ಢ = 42; //apparently something valid

Out of curiosity, is there a list of such special characters which can be used in Java, and in a way which is readable by Eclipse?

Gnoupi
  • 4,715
  • 5
  • 34
  • 50
  • 4
    Now this is only for curiosity's sake. Using those in real life would be hellish. *Ok, maybe as an occasional prank, but that's it.* – Gnoupi Apr 17 '15 at 10:31
  • I might also be misnaming those characters, admittedly, so my apologies if this is the case. – Gnoupi Apr 17 '15 at 10:32
  • Related: http://stackoverflow.com/questions/21526799/using-utf-8-characters-in-java-variable-names – Thilo Apr 17 '15 at 10:34
  • @jurgemaister - does not cover the "visible in Eclipse" part, though, The interest of the question is to know the ones which would be actually visible also. – Gnoupi Apr 17 '15 at 10:36
  • 1
    @Gnoupi I guess that depends more on your font than Eclipse – Jørgen R Apr 17 '15 at 10:37
  • @jurgemaister - possibly, yes. Or the charset in use, I guess, if such thing can be set. – Gnoupi Apr 17 '15 at 10:37
  • It was a more entertaining question before thinking about it like a programmer, admittedly. Obviously it goes : "here are java rules on identifiers" and "here is how your IDE displays the characters", and well, it's all done and answered. Yippee. – Gnoupi Apr 17 '15 at 10:41
  • Strictly speaking, I don't think `ಢ_ಢ` is called an emoji. – Radiodef Apr 18 '15 at 03:12
  • @Radiodef - probably, but it's the closest I had for "funny characters looking like smileys or other stuff", in my mind. Not sure how to replace it properly. – Gnoupi Apr 18 '15 at 06:22
  • this should be mandatory in LOLCODE – loli Jul 17 '15 at 19:02
  • That's not an emoji. It's a Kannada character. –  Nov 20 '15 at 04:06

3 Answers3

14

Even if Java allows to use characters from the entire Unicode set it seems like Eclipse only allows a few.

Since I couldn't find a list of supported emojis I tried it out myself and found the following as working:

☺❤☝✌☀☁❄✈☎✉✂✒✏㊙㊗❇✳✴♈♉♊♋♌♍♎♏♐♑♒♓▶◀➡↗↘↙↖↕↔↪↩⤴⤵〰✖✔™©®〽‼⁉Ⓜ♨
♠♣♥♦☑▪▫☹♤♡♢♧☢☣☠☤†☯☮☭☥✠✙✞✟✧⋆★☆✪✫✬✭✮✯✰✡☫☬☸✵❂❀❃❁✼♫♪☃❅❆☂❦♕♛♔♖♜☾
→⇒⇨➩➪➫➬➭➮➯➲➳➵➸➻➺➼➽☜☟➹➷↶↷✆⌘⏎❥ツღ☻`

If you find more feel free to edit my answer and add them.

EDIT: I think there are now a lot more supported emojis than back then. You can also use thousands of letters from the chinese alphabet.

das Keks
  • 3,723
  • 5
  • 35
  • 57
11

You can find this information in the Java Language Specification section on identifers:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

...

Letters and digits may be drawn from the entire Unicode character set, which supports most writing scripts in use in the world today, including the large sets for Chinese, Japanese, and Korean. This allows programmers to use identifiers in their programs that are written in their native languages.

In this case, you're using the Unicode character \u0ca2.

Community
  • 1
  • 1
M A
  • 71,713
  • 13
  • 134
  • 174
  • What's "self-referential/incomplete" about "the entire Unicode character set?" – E-Riz Apr 17 '15 at 12:19
  • 1
    @E-Riz - the comment was probably referring to the [previous version](http://stackoverflow.com/revisions/29696837/1) of the post, with the two methods – Gnoupi Apr 17 '15 at 12:35
  • @Gnoupi, you're probably right. In that case the comment (and all these that follow) should be deleted. I've flagged it as obsolete. – E-Riz Apr 17 '15 at 15:01
1

Short Answer: Any emoji which is standard in the unicode character set will be a valid variable name in Java, Swift, or JavaScript.

Long Answer: There are 1282 unicode emoji, which you can find here.

Emoji variable names are a joke. But not all emoji are happy/sad faces. Some emoji are animals, foods, buildings, clocks displaying various times, sports, various technologies, currencies, and flags. So it is entirely possible you could sometimes use an emoji as a useful descriptor of your variable.

Here is a working Hello World example using emojis as variable names (they display correctly in my text editor, but not in GitHub's gist).

Please don't do this on any project I'm working on.

john_science
  • 6,325
  • 6
  • 43
  • 60
  • 2
    This doesn't appear to be correct. We can't use emojis as a Java identifier. Emojis (in proper use of the term) are supplementary. http://ideone.com/me6vOP (The OP's emoticon face is not an emoji.) – Radiodef Apr 18 '15 at 03:28
  • @Radiodef I am not sure if I understand the issue. Here is a working HelloWorld example using a unicode emoji as a variable name: https://gist.github.com/theJollySin/b160948614bfd29de5c3 Do I perhaps lack an understanding of what is officially an "emoji"? – john_science Apr 18 '15 at 17:21
  • 1
    [Emojis in Unicode](http://en.wikipedia.org/wiki/Emoji#In_the_Unicode_standard) refer to some specific blocks. [`0ca2`](http://www.fileformat.info/info/unicode/char/0ca2/index.htm) is a letter. Emojis (such as the ones in your link) do not appear to work. http://ideone.com/EI6fnB http://ideone.com/0OR3PX – Radiodef Apr 18 '15 at 19:24
  • @Radiodef I see. Would you feel comfortable if I amended my answer to say that we can use the escape-character version of the unicode emojis (`\u0ca2`, `\u1f60b1`, `\u1f419`, etc) in Java, and it would be up to your text editor or IDE to display them in the correctly (hilarious) way? – john_science Apr 19 '15 at 04:33
  • 3
    `\u0ca2` is not an emoji. `\u1f60b1` and `\u1f419` aren't valid unicode escapes, nor would they be valid identifiers as escaped surrogate pairs. *"Any emoji which is standard in the unicode character set will be a valid variable name in Java"* This is just an incorrect statement. The only way you could make it technically correct would be to change *"will"* to *"will not"*. – Radiodef Apr 19 '15 at 04:56
  • @Radiodef In the Gist I have linked above, I was able to compile and execute Java code using all three of those symbols. I have a standard Java 1.7 compiler on Ubuntu and there were no errors compiling or running. – john_science Apr 20 '15 at 15:13
  • 3
    I don't see the second two in your gist and they aren't valid Java identifiers. http://ideone.com/AgW86n They may not be getting parsed the way you think they are. http://ideone.com/NWbtyl I really don't know why you are spending time arguing about this. You should research the topic and correct the error or delete the answer. – Radiodef Apr 20 '15 at 20:02