简体   繁体   中英

Java naming convention

I have been trying to learn Java for the past week, and have been able to produce reasonable codes so far. However, I seem to have a problem in understanding the Java naming convention.

I just looked at a tutorial which says that class names should start with an upper case. If I look at the codes so far I have wrote, I have actually used lower case names, such as:

import java.io.*;
import java.util.*;

class orange implements Comparator {
    public int mango(...) {
    }
}

class jason {
    public static void main(String args[]) throws java.io.IOException {
        {
            // Content here
        }
    }
}

As you can see, both my class names start with a lower case.

When I compile and execute the program, I don't get any compile errors, and everything works as expected. I should have thought, since the class name starts with lower case, it would end up with a compile error: but this hasn't happened. Why?

If it helps, I run OpenJDK and IcedTea .

Naming conventions are just conventions , not rules. The Java language spec doesn't care either way. But if you don't stick to the conventions, your code will be difficult for other people to read and understand, so you really should stick to them.

A naming convention is a rule to follow as you decide what to name your identifiers (eg class, package, variable, method, etc..) and it is not an EXCEPTION.

A good programmer must and will follow the naming conventions specified in any programming language for a neat and effective program.

Refer to this page for more Java naming conventions.

It's a convention, not a compiler rule.

You can break it if you wish, but I recommend following the convention.

It's not about compiler syntax. It is about following coding conventions.

And you missed the convention in your code

See

It's a convention that class names start with upper case, but the compiler does not enforce it. Same with method names (though they start with lower case).

I have never seen class names with lower case. Just don't do it.

The Java compiler doesn't enforce naming conventions. It's called a convention for a reason: to facilitate reading of code. For example, when you see an orange , it's probably a variable, and there isn't any need to go look it up. And in your case, readers of your code will be quite surprised.

You have broken the naming convention, not the syntax of the language - which is why your code compiles without error.

Naming conventions are used primarily to improve readability in source code and to reduce the effort needed to understand the code.

Java is a language which is widely used worldwide. There are lot of applications built in Java every moment.

It is not necessary that the same code will be worked upon / maintained by the same developer throughout its lifetime. Hence, to avoid ambiguity and keep it simple to understand and reuse, a naming convention is followed.

Guess what would happen if each of us use our own conventions. It will put an adverse effect on code reusability, because it will take lot of time for other programmers to understand the code before working on it. Hence, it is required that every Java developer knows and follows the standard protocols so that each of are on same page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM