Reputation: 565
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.
Upvotes: 3
Views: 7827
Reputation: 334
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.
Upvotes: 0
Reputation: 20950
A naming convention is a rule to follow as you decide what to name your identifiers (e.g. 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.
Upvotes: 6
Reputation: 230521
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.
Upvotes: 1
Reputation: 240946
It’s not about compiler syntax. It is about following coding conventions.
And you missed the convention in your code
See
Upvotes: 2
Reputation: 3321
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.
Upvotes: 1
Reputation: 403581
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.
Upvotes: 7
Reputation: 19443
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.
Upvotes: 1
Reputation: 52478
It's a convention, not a compiler rule.
You can break it if you wish, but I recommend following the convention.
Upvotes: 5