Why is java.lang.String capitalized the way it is? - java

Why is the String class capitalized but java and lang are lowercase, another example would be System.out.println, println is a method so lowercase is expected, but why is out lowercase? Am I missing something or does someone not follow their own rules?

Class names have the first letter in Uppercase. Package names are always in lower case as per naming conventions.
java.lang is the package name
String is the Class name
For the System.out.println() it breaks down as this
System is the class name, so the first letter is Uppercase
out is a public static field which is of the PrintStream class so it can be access directly.
println() is a method of the PrintStream class
Example of usage would be System.out.println("Hello World");
More information about this can be found in the documentation here http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#out

System.out.println() here the whole thing is not a method name. Here only the println() is a method name and it is in lower case. System is the class name and out represent an standard object which I don't need to create implicitly.
More over java.lang is a package name and String is the class name. java.lang.String as a whole is not a class name, it's fully qualified class name.Each group in a package name always starts with lowercase.And it's customary in java to write the class and package name by this convention.

"Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code."
Reference: Java Naming Conventions
So, in your example "java.lang" is the package name and "String" is the class name which followed Java Naming Conventions.

Related

naming convention for class in java - all caps

How do you name a class when it's all caps in Java? For example, if I want to create a class to select certain people to be VIP. Should I name the class "VIPSelector" or "VipSelector"?
Thanks!
Both of your options work. The main goal with classes is to have them start with an Upper Case. So, VIPSelector and VipSelector both work. This convention is mostly used to get rid of a common mistake that you can find in OOP which is when you can't make the difference between a class and a method.
Imagine having a class object called "student", to initiate it, it would be
student s = new student();
That looks a lot like a method and this is why, by convention, we put the first letter in upper case.
This is how class Name should be :
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).
Examples: class Raster; class ImageSprite;
Check this for the information : https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html#:~:text=Class%20names%20should%20be%20nouns,such%20as%20URL%20or%20HTML).
Both names are acceptable. The general convention for naming classes in Java is just that the first letter should always be capitalized and the whole name should be in camel case, meaning that the first letter of each word is capitalized.
The google style guide prefers VipSelector
See this answer to a similar question.

Naming conventions of composed package names

I want to create a package named form validator.
Is it better to write
form_validator,
formValidator or
formvalidator?
I want to mention that I want to avoid form.validator. And that form-validator is forbidden.
From the documentation on package naming convention:
Package names are written in all lower case to avoid conflict with the names of classes or interfaces
So this would leave you with the following two possibilities:
form_validator
formvalidator
Actually the documentation also makes it clear that underscore plays a special role when it appears in package names:
if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int" ... the suggested convention is to add an underscore.
So, underscore is suggested only in special cases, into which your naming problem does not seem to fall. So I would recommend formvalidator as the package name.
The most conventional one would be the 3rd one: formvalidator.
The Google Java Style guide notes:
5.2.1 Package names
Package names are all lowercase, with consecutive words simply concatenated together (no underscores). For example, com.example.deepspace, not com.example.deepSpace or com.example.deep_space.
As #teppic said, the Oracle Java Docs state that
Package names are written in all lower case to avoid conflict with the names of classes or interfaces.
In Java package names are written in all lower case. Which means following would be the most ideal packaging name.
formvalidator
This is also accepted.
form_validator
Package names are written in all lower case to avoid conflict with the names of classes or interfaces. So
form_validator or
formvalidator.
For details see here.

How to resolve class name to fully qualified in Java?

Is there any way in Java to resolve simple class name (e.g. String) to fully qualified one (e.g. java.lang.String) or better yet to Class directly? The only way that comes to my mind is to parse file for imports and figure out from classpath jars. Sure there must be a better way.
There is no way to do this in a general way, since the same class name can be used in several packages, for example "Date" can be java.util.Date or java.sql.Date. Unless you can somehow limit the search area, this isn't even theoretically possible.
Also, one must wonder what use case would require you to get a Class from an unqualified class name.
Java compiler determines fully qualified names class names by trying to find classes on the class path with different combination of simple class names in source file and package names in import statements. Compiled classes (.class) always get fully qualified names.
import java.util.Date;
public class Test {
String str;
Date date;
...
compiles to Test.class
public class Test {
Ljava/lang/String; str
Ljava/util/Date; date
...

Can you use a name like "HELLOWorld.class" for a java program?

I could not execute my java program by a command line, although the Eclipse's "Run" menu executed it. While examining the problem, I happened to find that changing the name from HELLOWorld to HelloWorld fixed the problem for my particular case. Does really Java's naming rule restrict such names?
Your file name must match to the public class name that you defined in that file. May be this might be the case in your scenario.
The Java Language Specification states the requirements for a class name (which is an Identifier). To sum it up: it must begin with a letter or underscore, and the remainder may contain letters or digits.
Thus, HELLOWorld is a valid classname, however, if your class is public, the filename and classname must match (you will receive a compilation error otherwise).
in one word, Yes
public class name must be same as class file name
Yes. The name of your public class must be same as the name of your filename i.e .java file.However you may have number of other classes also.

package system not found in case I wrote system.out.println();

I am writing a simple program:
class Demo{
public static void main(String[] args){
system.out.println("Hello");
}
}
on compilation it gives an error: package system not found.
Why it do so that package not found instead system is a misspelled class name.
System.out.println("Hello");
You need a capital S
Package names are lower case like java.lang.SomeClass, since it's lowercase it assumes you're looking for a package named system.
Why it do so that package not found instead system is a misspelled class name
When you say something.somethingElse the compiler assumes you are doing a packageName.classname. In this case you were intending to access out of the System class, but you could very well be trying to access a package called system that is not present (in the classpath for instance). So it is a guess from a compiler.
And (I guess) it is so because this is the better guess. Let's say the compiler said class not found. You might be happy. But tons of others doing Java.util.List (instead of java.util.List) would complain "I was trying to access the java.util package but misspelled it. The compiler wrongly says Missing class name.
Update (for the sake of completeness)
From #paxdiablo's answer below:
The reason why the compiler is assuming it's a package name rather
than a class or variable name lies in section 6.5 of the JLS,
"Determining the meaning of a name"
It's System with a capital S:
class Demo {
public static void main (String[] args) {
System.out.println ("Hello");
}
}
The reason why the compiler is assuming it's a package name rather than a class or variable name lies in section 6.5 of the JLS, "Determining the meaning of a name":
The meaning of a name depends on the context in which it is used. The determination of the meaning of a name requires three steps.
First, context causes a name syntactically to fall into one of six categories: PackageName, TypeName, ExpressionName, MethodName, PackageOrTypeName, or AmbiguousName.
Second, a name that is initially classified by its context as an AmbiguousName or as a PackageOrTypeName is then reclassified to be a PackageName, TypeName, or ExpressionName.
Third, the resulting category then dictates the final determination of the meaning of the name (or a compilation error if the name has no meaning).
Your particular use is an AmbiguousName, due to 6.5.1:
A name is syntactically classified as a MethodName in these contexts: (1) Before the '(' in a method invocation expression; (2) some other irrelevant contexts.
A name is syntactically classified as an AmbiguousName in these contexts: (1) To the left of the '.' in a qualified ExpressionName; (2) To the left of the '.' in a qualified MethodName; (3) some other irrelevant contexts.
Based on your code, system.out.println(whatever) is a qualified MethodName preceded by an AmbiguousName. Later on in the process, 6.5.2, the reclassification, mentioned earlier, takes place:
If the AmbiguousName is a qualified name, consisting of a name, a '.', and an
Identifier, then the name to the left of the '.' is first reclassified, for it is itself
an AmbiguousName.
If the name to the left of the '.' is reclassified as a PackageName, then if
there is a package whose name is the name to the left of the '.' and that
package contains a declaration of a type whose name is the same as the
Identifier, then this AmbiguousName is reclassified as a TypeName.
Otherwise, this AmbiguousName is reclassified as a PackageName.
A later step determines whether or not a package of that name actually exists.
Because the reclassification walking up the tree (from println towards system) never results in a TypeName, the default reclassification to PackageName is always done.
That's why the error message you see is about a missing package rather than a missing class.
THe only error in this code is that you have written small s instead of capital S in System.out.println("Hello");
The most case in which you get an error Package system not found in system.out.println(" "); is because, you have to give your file name as "nameofpublicclass.java".
In this example, file name must be "Demo.java".
I think that will solve the error.
The compiler says system package not found...
Because it can't find a package called system ( starting with a lower case s.)
It might be able to find one called System, but that's not what your code asked for.
The compiler almost never guesses.

Categories

Resources