In java program when we write to print something we use one statement I.e. system.out .println. so why we use dot in that
why use dot operator in system.out.println statement
Collapse
X
-
The dots separate class name, field and method.
Just imagine you write only the first name of your friend on a letter and send it away, will he get it? So why do you need to add city and last name? The java dot is the same case.
There are many "out" fields in different classes, so you must say that you want the one from class "System". The "out" field has many methods, so you must say that you want the method "println".Comment
-
But "/" already means division!
So if you write "x=a/b" the compiler does not know if you mean "x equals a divided by b" or "x equals field b of object a". That means it's ambigious.
Let's look for other possible characters:- "-": ambigious (subtraction)
- "_" underscore: ambigious, already part of variable name
- " " Space: Does not optically separate. Two or more spaces, which is hard to count, would not separate. Also ambigious "a instanceof b" could mean "a.instanceof.b "
- "\" backslash: not available on most keyboards in the past. Also nowadays you must press "alt" and "?" (on German keyboard) together to print it, so it's hard to type.
Java syntax is derived from C syntax, and in C the dot already had the meaning of accessing a sublement of an element ("struct"), so it was just re-used, but in a slightly different meaning: "a.b" in java matches more the C-syntax "a*.b" instead to "a.b".Comment
Comment