Peter is a DZone MVB and is not an employee of DZone and has posted 123 posts at DZone. You can read more from them at their website. View Full User Profile

Uses for Special Characters in Java Code

08.14.2012
| 7016 views |
  • submit to reddit
Ever wondered how you can write code like this in Java?
    if( ⁀ ‿ ⁀ == ⁀ ⁔ ⁀ || ¢ + ¢== ₡)

Background

Underscores has long been using in C like language such as Java to distinguish fields and method names.

It is common to see a leading underscore like _field or an underscore in a constant like UPPER_CASE. In Java the $ is also used in class names and accessor method names.

The SCJP has notes which state

Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number!
This leads to the question; what other connecting characters are there?

What are connecting characters?

A connecting character joins two words together. This page lists ten connecting characters

U+005F LOW LINE _ view
U+203F UNDERTIE view
U+2040 CHARACTER TIE view
U+2054 INVERTED UNDERTIE view
U+FE33 PRESENTATION FORM FOR VERTICAL LOW LINE view
U+FE34 PRESENTATION FORM FOR VERTICAL WAVY LOW LINE view
U+FE4D DASHED LOW LINE view
U+FE4E CENTRELINE LOW LINE view
U+FE4F WAVY LOW LINE view
U+FF3F FULLWIDTH LOW LINE _ view

And if you try the following you may find it compiles.

     int _, ‿, ⁀, ⁔, ︳, ︴, ﹍, ﹎, ﹏, _;

While this is interesting, does it have a use?  Recently I found one.
I have an object which represents a column, and this column has a value for that row. The names are basically the same but I want a notation to distinguish them. So I have something like
    Column<Double>︴tp︴ = table.getColumn("tp", double.class);
    double tp = row.getDouble(︴tp︴);

This way I can see with is tp the column, and which is the value.

Interestingly the currency characters are valid as well.
 for (int i = Character.MIN_CODE_POINT; i <= Character.MAX_CODE_POINT; i++)
        if (Character.isJavaIdentifierStart(i) && !Character.isAlphabetic(i))
            System.out.println(i + " : " + (char) i);

prints

36 : $
95 : _
162 : ¢
163 : £
164 : ¤
165 : ¥
1547 : ؋
2546 : ৲
2547 : ৳
2555 : ৻
2801 : ૱
3065 : ௹
3647 : ฿
6107 : ៛
8255 : ‿
8256 : ⁀
8276 : ⁔
8352 : ₠
8353 : ₡
8354 : ₢
8355 : ₣
8356 : ₤
8357 : ₥
8358 : ₦
8359 : ₧
8360 : ₨
8361 : ₩
8362 : ₪
8363 : ₫
8364 : €
8365 : ₭
8366 : ₮
8367 : ₯
8368 : ₰
8369 : ₱
8370 : ₲
8371 : ₳
8372 : ₴
8373 : ₵
8374 : ₶
8375 : ₷
8376 : ₸
8377 : ₹
43064 : ꠸
65020 : ﷼
65075 : ︳
65076 : ︴
65101 : ﹍
65102 : ﹎
65103 : ﹏
65129 : ﹩
65284 : $
65343 : _
65504 : ¢
65505 : £
65509 : ¥
65510 : ₩
Published at DZone with permission of Peter Lawrey, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)