What are the valid characters for a Java method na

2020-06-13 11:17发布

I read about the naming of Java variables. It says that Java variables cannot start with any numbers and special characters except for $ and _.

Some valid examples:

int count;
int _count;
int $count;

And some invalid examples:

int %count;
int 4count;
int #count;

Do the same rules apply to method names?

3条回答
三岁会撩人
2楼-- · 2020-06-13 11:35

You might be surprised when having unusual characters for method, such as:

public void mój_brzuch_zacznie_burczeć()

and it works pretty nice. Take a look at this blog to see more fancy examples.

查看更多
乱世女痞
3楼-- · 2020-06-13 11:47

From the Java Tutorial:

"Although a method name can be any legal identifier, code conventions restrict method names." http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

查看更多
beautiful°
4楼-- · 2020-06-13 11:49

Yes, method names and variable names are what's called "identifiers". Identifiers all share the same rules regarding accepted characters. Take a look at §3.8 from the Java Language Specification to find out exactly what an identifier may contain, and §6.2 for an explanation about how identifiers are used.

查看更多
登录 后发表回答