为什么 Java 类的第一个字母要大写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11947084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why should the first letter of a Java class be upper-cased?
提问by SKADIN
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?
为什么 Java 类的第一个字母要大写?是否无法运行包含小写类名的程序?如果可能,有什么区别?
回答by MadProgrammer
It's a coding convention, adopted by most Java programs. It makes reading code easier as you become use to a given standard.
这是一种编码约定,被大多数 Java 程序采用。当您习惯于给定的标准时,它会使阅读代码变得更容易。
No, you don't have to follow it, but you won't make any friends by not doing so ;)
不,您不必遵循它,但是如果不这样做,您将无法结交任何朋友;)
回答by jahroy
Using naming conventionsmakes it easier to quicklyunderstand 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:
如果我们知道作者遵循命名约定,我们很快就会知道几件事:
- foois an instance of some class
- FooFactoryis a class
- getFooInstance()is a staticmethod of the class FooFactory
- doSomethingFoosDo()is an instancemethod of foo
- foo是某个类的实例
- FooFactory是一个类
- getFooInstance()是FooFactory类的静态方法
- doSomethingFoosDo()是foo 的一个实例方法
Otherwise we waste time wondering things like "is FooFactory an object or a class?"
否则我们会浪费时间想像“ FooFactory 是一个对象还是一个类?”
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 ENORMOUSdifference as you begin to write more complex software and work with others.
起初这似乎过于紧张,但是当您开始编写更复杂的软件并与他人合作时,它会产生巨大的差异。
You'll find that naming conventions even make your own codemore readable.
您会发现命名约定甚至使您自己的代码更具可读性。
回答by JMelnik
Why should the first letter of a Java class be upper-cased?
为什么 Java 类的第一个字母要大写?
There are Code Conventions for the Java Programming Languageand 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.
此 Java 编程语言代码约定文档包含我们在 Sun 遵循并建议其他人遵循的标准约定。它涵盖文件名、文件组织、缩进、注释、声明、语句、空格、命名约定、编程实践,并包括一个代码示例。
- 一个软件生命周期成本的 80% 用于维护。
- 几乎没有任何软件是由原始软件终生维护的
- 代码约定提高了软件的可读性,使工程师能够更快速、更彻底地理解新代码。
It states Naming Conventions, which says on class names:
它声明了Naming Conventions,它在类名上说:
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;
类名应该是名词,大小写混合,每个内部单词的第一个字母大写。尽量保持你的类名简单和描述性。使用完整的单词——避免首字母缩略词和缩写词(除非缩写词比长格式更广泛使用,例如 URL 或 HTML)。
即类光栅;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.
这是可能的,合适的,并且对于 compiler没有区别。
It is disrespectful for code and other programmers.
这是对代码和其他程序员的不尊重。
It is uncomfortable for you not to follow Java coding conventions.
不遵循 Java 编码约定会让您感到不舒服。
回答by Stephen C
Why should the first letter of a Java class be upper-cased?
为什么 Java 类的第一个字母要大写?
It is a convention that is universally observed by experienced Java programmers.
这是有经验的 Java 程序员普遍遵守的约定。
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-socialto 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 justanti-social. It turns out that you can burn yourselfby 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.
我应该补充一点,这不仅仅是反社会的。事实证明,您可以通过忽略约定来燃烧自己。样式约定旨在与 Java 的标识符消歧规则结合使用;见JLS 6.5。如果您忽略样式规则,您很可能会发现歧义标识符(例如,可以是类名或变量名)以意想不到的方式消除歧义。最终结果将是(通常)难以理解的意外编译错误。
回答by Mohammod Hossain
Java naming convention for class
类的 Java 命名约定
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
回答by Jigar Joshi
It is the java naming convention
这是java命名约定
回答by oyss
just naming convention.recommended to follow.
只是命名约定。建议遵循。
回答by jpkrohling
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).
类名应该是名词,大小写混合,每个内部单词的第一个字母大写。尽量保持你的类名简单和描述性。使用完整的单词——避免首字母缩略词和缩写词(除非缩写词比长格式更广泛使用,例如 URL 或 HTML)。
http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367
http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367
回答by Sai Ye Yan Naing Aye
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.
是 可以在 Java 中使用小写的类名。但是您应该遵守 Java 社区推荐的 Java 命名约定。
回答by Krithika Vittal
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.
Java 编码和命名约定是为了代码可维护性而设计的。遵守这些标准的代码将易于维护。另一个开发人员可以理解您在 java 代码中所做的工作。
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."
“不同的 Java 程序员可以有不同的编程风格和方法。通过使用标准的 Java 命名约定,他们可以让自己和其他程序员更容易阅读他们的代码。Java 代码的可读性很重要,因为这意味着花在尝试上的时间更少弄清楚代码的作用,留出更多时间来修复或修改它。”