Why should the first letter of a Java class be upper-cased? Is it not possible to run a program containing lower-cased class names? If it is possible, what's the difference?
问题:
回答1:
It's a coding convention, adopted by most Java programs. It makes reading code easier as you become use to a given standard.
No, you don't have to follow it, but you won't make any friends by not doing so ;)
回答2:
Using naming conventions makes it easier to quickly understand code when you read it.
Here are two simple lines of code:
foo = FooFactory.getFooInstance();
foo.doSomethingFoosDo();
If we know the author follows naming conventions, we quickly know several things:
- foo is an instance of some class
- FooFactory is a class
- getFooInstance() is a static method of the class FooFactory
- doSomethingFoosDo() is an instance method of foo
Otherwise we waste time wondering things like "is FooFactory an object or a class?"
Without naming conventions, you'd actually have to search through the source code to answer such questions.
This seems overly uptight at first, but it makes an ENORMOUS difference as you begin to write more complex software and work with others.
You'll find that naming conventions even make your own code more readable.
回答3:
Why should the first letter of a Java class be upper-cased?
There are Code Conventions for the Java Programming Language and it says:
This Code Conventions for the Java Programming Language document contains the standard conventions that we at Sun follow and recommend that others follow. It covers filenames, file organization, indentation, comments, declarations, statements, white space, naming conventions, programming practices and includes a code example.
- 80% of the lifetime cost of a piece of software goes to maintenance.
- Hardly any software is maintained for its whole life by the original
- Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.
It states Naming Conventions, which says on class names:
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
i.e. class Raster; class ImageSprite;
Is it not possible to run a program containing lower-cased class names? If it is possible, what's the difference?
It is possible, appropriate and there is no difference for compiler.
It is disrespectful for code and other programmers.
It is uncomfortable for you not to follow Java coding conventions.
回答4:
Why should the first letter of a Java class be upper-cased?
It is a convention that is universally observed by experienced Java programmers.
Is it not possible to run a program containing lower-cased class names?
Yes it is possible. It is also possible to go to work in the same set of clothes for a month. But you won't make any friends by doing either.
If it is possible, what's the difference?
The difference is that your code is much less readable to other people if you ignore the conventions. Now, if you are writing code that will only ever be read by you, then code style is purely your problem. However, in the real world, most of us regularly have to read code written by other people. And if your code is painful to read because you've chosen to ignore the conventions, don't expect people to praise you for it.
Frankly, it is anti-social to write unreadable code in a context where there is a reasonable expectation that other people will need to read it.
I should add that it is not just anti-social. It turns out that you can burn yourself by ignoring the conventions. The style conventions are designed to work in conjunction with Java's identifier disambiguation rules; see JLS 6.5. If you ignore the style rules, you are liable to find that an ambiguous identifier (e.g. one that could be either a class name or a variable name) gets disambiguated in an unexpected way. The net result will be unexpected compilation errors that are (typically) hard to understand.
回答5:
Java naming convention for class
Classes: Names should be in CamelCase.
Try to use nouns because a class is normally representing something in the real world:
class Customer
class Account
回答6:
It is the java naming convention
回答7:
just naming convention.recommended to follow.
回答8:
Those are not rules, but conventions. You are free to use whatever you want, the way you want, but by following the convention you are sure that other developers will quickly understand your code, as it's clear what's a class, what's a property, what's a method, and so on.
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367
回答9:
Java coding and naming conventions were framed for code maintainability. Code that is adhering to these standards will be easy to maintain.Another developer can understand what you have done in the java code.
Extract from the internet:
"Different Java programmers can have different styles and approaches to the way they program. By using standard Java naming conventions they make their code easier to read for themselves and for other programmers. Readability of Java code is important because it means less time is spent trying to figure out what the code does, leaving more time to fix or modify it."
回答10:
Yes It is possible to use lower-cased class names in Java. But you should obey naming convention of Java that is recommended in Java Community.