I have a question pertaining the method in the java, I have checked for online resources but i could not get the answer that i wanted. I would like to know why the "out" and "println" in the statement below has to be in lowercase letters ?
System.Out.Println()
CodePudding user response:
System
is the name of a class (java.lang.System
) and follows normal conventions.
out
is a static field. This is conventionally named, but it's quite rare to see public fields for anything other than constants.
The type of out
is PrintStream
, and println()
is just a method on PrintStream
- again, conventionally named.
It may help (in terms of understanding) to break things up:
PrintStream outputStream = System.out; // Access to the out field
outputStream.println(); // Just a method call
Now as for why you have to use a lower case 'o' and a lower case 'p' in the code System.out.println()
- that's just because Java is case sensitive, and the names are out
and println()
, not Out
and Println()
. They could have been named in the latter way, but that would violate normal Java naming conventions.
CodePudding user response:
The System
part is a class. out
and println()
are both instances of a type and a class respectively. Someone can correct me if I'm wrong. Unless this isn't exactly what OP asked
CodePudding user response:
As Jon Skeet explained in his answer:
- Java is case sensitive,
- this (
System
,out
andprintln
) is how the respective identifiers for the class, field and method (respectively) have been specified, and - this choice of identifiers conforms to the normal Java naming conventions.
There is however one small wrinkle.
The out
variable is declared as a static final
field and is (from a certain perspective) a constant. It arguably could therefore have been named as OUT
rather than out
.
Q: Why isn't it?
A: A couple of reasons:
- The conventions for what qualifies for "constant-ness" are unclear.
- While you cannot assign a value to
System.out
, the object it refers to is mutable by design. - In fact, there is an official / supported way to change
System.out
... despite it being declared asstatic final
. (TheSystem.setOut
method.)