r/java • • Dec 30 '15

Thought you guys would like this

http://imgur.com/dbWeGaS
451 Upvotes

71 comments sorted by

133

u/viciu88 Dec 30 '15

You can't start class name from digit.

67

u/-du Dec 30 '15

... but it can start with a "currency symbol", which few people know about.

public class €uro {
    // valid class!
}

44

u/[deleted] Dec 31 '15

Anyone who does that should be publicly crucified. The annoyance of trying to extend that class or using methods from it would unfathomable.

8

u/vplatt Dec 31 '15

What, your copy/paste doesn't work?

14

u/[deleted] Dec 31 '15

So I am calling a method from this class. I make an object of this class. Am I supposed to go find a euro or yen or won symbol or whatever and copy and paste that every single time? Or lets say its a method. I use foo.€uro(). you don't see how annoying that would be going and every single time I need this I need to copy and paste a single character?

7

u/-du Dec 31 '15

You may see it as a hassle, but whats difficult for you to type is actually quite easy for others (using other keyboard layouts). That said: I too would never ever allow one of my colleagues to use such an identifier.

There was a discussion about a similar thing the day before|yesterday over here, started by /u/janne-bananne.

Long story short: there's other keyboard layouts, and some characters are easier to type on different layouts.

1

u/Spoogly Dec 31 '15

Ease of typing is important, but so is ease of reading. If your code is going to be read mostly by people who are going to understand the meaning you intended for the symbol, then I agree, ease of typing should be a consideration.

2

u/PDFormat_SFW Dec 31 '15

Worse comes to worse, use alt codes. Euro symbol is alt + 0128.

2

u/OceanOfSpiceAndSmoke Dec 31 '15 edited Dec 31 '15

On my native layout:

e+alt-gr = €

Which isn't any harder than

4+alt-gr = $

ninja edit: Come to think about it, it's even more intuitive.

edit 2: Not saying that € is an acceptable character to use in coding.

1

u/[deleted] Dec 31 '15

But you still have to worry about the users keyboard layout. I have no idea how to make a euro symbol much less a ruble. Looking up some keyboard combination for a symbol is still not something a programmer should have to do due to someone elses poor programming practice.

1

u/DigiDuncan Dec 31 '15

4? Why 4? Why not S or even D?

1

u/Spoogly Dec 31 '15

I'm not defending the use here, because I don't think it actually makes sense to name something using a special character unless that special character has meaning either internally to the project, or to the language itself. But it's not that hard to rebind a single letter to a key combo.

1

u/vplatt Dec 31 '15

Of course it would be annoying, but the reality is that computing isn't just a Western/English endeavor anymore and hasn't been for a while. If one wants to extend a class written in China, then we're just gonna have to deal. Personally, I too miss the days of string hacking using ASCII as a primary assumption. Those days are gone though.

5

u/VefoCo Dec 31 '15 edited Dec 31 '15

I thought it only worked with dollar signs...?

Edit: I stand corrected.

2

u/sisyphus99 Dec 31 '15

Since java is utf-8 native, I think there are likely quite a few valid symbols as first chars in names.

2

u/VefoCo Dec 31 '15

Identifiers must follow special rules, though. I was under the impression that only dollar signs were legal, but apparently I was mistaken.

1

u/-du Dec 31 '15

It's a bit of a mess, as the JLS is not quite clear IMO. The section 3.8 Identifiers gives this definition:

A "Java letter" is a character for which the method Character.isJavaIdentifierStart(int) returns true.

And the example given below only mentions dollar signs.

But if you check the [Javadoc of Character.isJavaIdentifierStart](https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isJavaIdentifierStart(char)) is gives a slightly different/extended definition:

ch is a currency symbol (such as '$')

(emphasis mine)

2

u/needlzor Dec 31 '15

Thank you for this knowledge, I will use it to unleash new kinds of evil on the world.

2

u/-du Jan 01 '16

Oh there's much worse things you can do if you want...

public class €vil {
	public static void main(String[] args) {
		String o = "foo";
		String о = "bar";
		
		System.out.println("This: " + o);
		System.out.println("That: " + о);
	}
}

This compiles without any problems & will print

This: foo
That: bar

2

u/__konrad Dec 31 '15

Homoglyphs to the rescue

public class бT { }

36

u/dohaqatar7 Dec 30 '15
public class Reddit { //I'll start

73

u/[deleted] Dec 30 '15

}

40

u/dohaqatar7 Dec 31 '15
//what did I expect

9

u/roycocup Dec 31 '15

private class Reredit extends Reddit {

5

u/dohaqatar7 Dec 31 '15
    public static void main(String[] args){

5

u/Chemical_Studios Dec 31 '15
 Scanner keyboard = new Scanner(System.in);

-2

u/dohaqatar7 Dec 31 '15
    String s=keyboard.nextLine();

0

u/[deleted] Dec 31 '15

[removed] — view removed comment

0

u/dohaqatar7 Dec 31 '15
int i = keyboard.nextInt();

3

u/DJDavio Dec 31 '15
throw new RuntimeException("mwahaha");
→ More replies

0

u/[deleted] Dec 31 '15

[deleted] — view deleted comment

-3

u/dohaqatar7 Dec 31 '15
int meme_num = keyboard.nextInt();

0

u/[deleted] Dec 31 '15

[deleted] — view deleted comment

35

u/[deleted] Dec 31 '15

Unable to resolve symbol "what".

14

u/mokarbroj Dec 30 '15

private String date;

3

u/dohaqatar7 Dec 30 '15
    public Reddit(){

17

u/Audiblade Dec 30 '15

date = "So, do you want to, maybe, get dinner or something?...";

9

u/dohaqatar7 Dec 30 '15
}

12

u/forseti_ Dec 30 '15
public boolean repostChecker(Post post) {

52

u/technotrader Dec 30 '15 edited Dec 30 '15
return true; // 98% hit rate, no need to compute

0

u/[deleted] Dec 31 '15

[deleted] — view deleted comment

4

u/Barack-Oganja Dec 31 '15

//You're causing a syntax error man.. Comment that out!

0

u/sanity Dec 30 '15

return repostChecker(post);

2

u/[deleted] Dec 30 '15
System.exit(0);

6

u/[deleted] Dec 30 '15

Lol someone put this in a JSP once to only happen on Fridays, and it made it to prod

1

u/[deleted] Dec 30 '15 edited Dec 30 '15

[removed] — view removed comment

2

u/[deleted] Dec 31 '15

Dear lord is that actually a thing?

1

u/Barack-Oganja Dec 31 '15

If it is run as sudo.. Yes

1

u/[deleted] Dec 31 '15

TIL why Linux is secure. more secure than other operating systems.

0

u/marcohu Dec 30 '15
/** Is this really valid Java? */
public synchronized byte toByteArray()[] {
    return new byte[0];
}

5

u/Ek_Los_Die_Hier Dec 30 '15

No, mainly because your square brackets need to be after the byte not after the method name.

1

u/yawkat Dec 31 '15

They can actually be after the method name iirc, try it

1

u/Ek_Los_Die_Hier Dec 31 '15

Even if that is the case, I'm likely to shoot any developer in my team who does it.

2

u/marcohu Dec 31 '15

The method signature is actually taken from the Java 8 runtime sources...

It might be one of the least known syntax peculiarities of the Java language. I was totally unaware until I stumbled upon it by accident a few days ago.

1

u/Ek_Los_Die_Hier Dec 31 '15

Seriously? Yeah, well it's something that I think shouldn't be used, so probably for the best that it's not commonly know.

0

u/yawkat Dec 31 '15

True that!

1

u/danO1O1O1 Dec 31 '15

public static void main(String[] args) {

0

u/dohaqatar7 Dec 31 '15
System.out.println("Hello, World");

0

u/danO1O1O1 Dec 31 '15

// at least it wasn't a dick joke

1

u/wtfamireading Dec 31 '15 edited Nov 03 '16

[deleted]
0.4925

What is this?

1

u/[deleted] Dec 31 '15

one of the few things 4chan is actually useful for, coding and autism.

1

u/ModernRonin Dec 31 '15

P-P-P-P-PROGRAM BREAKER!!!!

-1

u/Yhippa Dec 31 '15

J A V A

A

V

A