5 Replies Latest reply: Jul 24, 2013 4:51 AM by user10808089 RSS

    Differerence between Keywords and Literals

    abroshan-Oracle

      Hi ..

      Keywords are the predefined definition known to compiler like public/static/void/class

      and liters are String/init/double/char/ , so here for e.g. String , its also predefined class ..

      So will it be right to say that literals are also keywords?

      Please correct me .. I am new to Java.

       

      Regards

      Abhishek

        • 1. Re: Differerence between Keywords and Literals
          gimbal2

          Partly. Its very simple, the java language specification explicitly states what keywords are:

           

          http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.9

           

          As you can see double, boolean, etc. are in there but String is in fact not. Can you perhaps think of a reason why not?

          • 2. Re: Differerence between Keywords and Literals
            rp0428

            AbhishekRoshan wrote:

             

            Hi ..

            Keywords are the predefined definition known to compiler like public/static/void/class

            and liters are String/init/double/char/ , so here for e.g. String , its also predefined class ..

            So will it be right to say that literals are also keywords?

            Please correct me .. I am new to Java.

             

            Regards

            Abhishek

            Just ask yourself 'when does a literal exist' and 'who creates literals'?

             

            Please explain how a 'literal' that YOU create, that has NEVER existed before could possibly be 'predefined' or 'known to MY compiler.

            • 3. Re: Differerence between Keywords and Literals
              aaron3.1416

              Hi

              • 4. Re: Differerence between Keywords and Literals
                abroshan-Oracle

                Thanks all ..

                Strings/int/char/double are all literals .. but why then we don't use like this ..

                 

                public static void main(int args[])

                and why we use always public static void main(String args[])

                 

                int/char/double are data types .. is String not a data type?

                If yes then why we don't use int/double/char  the way we use String..

                 

                why nowt this ..

                 

                public static void main(int args[])

                String i="hello World"

                • 5. Re: Differerence between Keywords and Literals
                  user10808089

                  Literals are the notation for representing the fixed value. In Java keywords are the reserved words which you cannot use for defining identifiers because Java treats them in predefined way.  int , float , char , double etc. are keywords and also primitive data types, you can use these type to define values example int a = 100 ; so here int is type/keyword a is variable and 100 is a literal. String is a built in type and not literal or keyword. String literal can be anything between ""(double quotes).