why use dot operator in system.out.println statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devangi
    New Member
    • May 2014
    • 1

    why use dot operator in system.out.println statement

    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
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Because if we do not use a dot, than Java will not work?

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      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

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        But that's not an answer to "why use dot operator"

        Or is that my language problem with English ;)

        I mean, also '/' could me used i.e.:
        System/out/println("Hello" );

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          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

          Working...