Language Fundamentals in Java

Overview

Language Fundamentals

  1. Identifiers - A name in Java Program is called an Identifier. Which can be used for identification purpose. It can be method name, or variable name of class name or label name. The characters allowed in an identifier are A-Z, a-z, 0-9, _ and $. Rest other characters will give you a compile time error. An identifier must not start with a digit. An identifier is case sensitive and it can have n number of characters.
  2. Reserved Words - There are 53 reserved words available in Java. 
    1. Keywords (50)
      1. Used Keywords (48)
        1. Data types
          1. byte
          2. short
          3. int
          4. long
          5. float
          6. double
          7. char
          8. boolean
        2. Conditions
          1. if
          2. else
          3. switch
          4. case
          5. default
          6. do
          7. while
          8. for
          9. break
          10. continue
          11. return
        3. Modifiers
          1. public
          2. private
          3. protected
          4. static
          5. final
          6. abstract
          7. synchronized
          8. native
          9. transient
          10. volatile
          11. strictfp
        4. Exception
          1. try
          2. catch
          3. finally
          4. throw
          5. throws
          6. assert
        5. Class
          1. class
          2. interface
          3. extends
          4. implements
          5. package
          6. import
        6. Object
          1. new
          2. instanceof
          3. super
          4. this
        7. Return type
          1. void
      2. Unused Keywords (2) - goto and const
    2. Reserved Literals (3) - true, false, null 
  3. Data types
    1. byte - 1byte = 8 bits. Max value = 127, Min value = -128, Range -128 - 127. Used for small values.
    2. short
    3. int
    4. long - Used for long range values.
    5. float - Used for short floating point values. Means if you want to 5 to 6 decimal places of accuracy then you can use float. Float follows single precision (accuracy).
    6. double - Used for long floating point values. Means if you want to 14-15 decimal places of accuracy then you can use double. Double follows double precision (accuracy). 
    7. char - Old languages (like C and C++) are ASCII code based and the number of different allowed ASCII code characters are less or equal to 256. To represent these 256 characters, 8 bits are enough hence, the size of char in old languages is 1 byte. 
      But Java is Unicode based and the number of unicode characters are greater than 256 and less then or equal to 64526. To represent these many characters, 8 bits may not enough. Compulsory we should go for 16 bits. Hence the size of char in Java is 2 bytes. Range 0 to 65535.
    8. boolean - Size of boolean is Not Applicable, they are virtual machine dependent. 

  1. Literals - There are three type of literals
    1. Integral literals: For integer datatypes (byte, short, int, long) we can specify literal value in the following ways. These are only possible ways to specify literal value for integral data types. By default every integral literal is of int type, now to define a long type of integral literal, we have to use the suffix "L" e.g. long l = 10L;
      1. Decimal Form(base 10): Allowed digits are 0-9. e.g. int x = 10
      2. Octal Form (base 8): Allowed digits are 0-7. Literal should be prefixed with 0. e.g. int x = 010;
      3. Hexa Decimal Form (base 16): Allowed digits are 0-9 and a-f. For extra digits (a-f) we can use both lowercase and uppercase characters. This is one of very few areas where Java is not case sensitive. The literal value should be prefixed with 0x or 0X e.g. int x = 0X10;
    2. Floating Point Literals: By default every floating point literal is of double type and hence we can't assign directly to the float variable. 
      But we can specify floating point literal as float type by using the suffix "f" or "F".
      e.g. float f = 123.456F; // This is valid
      e.g. double d = 123.456; // This is valid
      e.g. float f = 123.456; // This is invalid because by default floating point literals is of double type.
      e.g. double d = 123.456D; // This is valid but this convention is not required.
      e.g. float f = 1.2e3; // This is valid. 1.2e3 = 1.2 x 103 = 1.2 x 1000
    3. Boolean Literals: The only allowed values type are true or false. 
      e.g. boolean b = true; // This is valid.
      e.g. boolean b = True; // This is invalid because True cannot be identified by the compiler. 
      e.g. boolean b = 0; // This is invalid because 0 is an Integer.
      e.g. boolean b = "true"; // This is invalid because "true" is a String. 
    4. Char Literals: We can specify char literal as integral literal which represents unicode value of the character and that integer literal can be specified later in decimal, or octal or hex decimal forms but allowed range is 0 to 65535.
      e.g. char c = 'a'; // This is valid.
      e.g. char c = "a"; // This is invalid because "a" is a String. 
      e.g. char c = a; // This is invalid because compiler can't find the symbol a.
      e.g. char c = 'ab'; // This is invalid because of unclosed char literal and unknown Java statement.
      e.g. char c = 97; // This is valid. The output of variable c is 'a'.
      e.g. char c = 0XFace; // This is valid. 
      e.g. char c = 0777; // This is valid. 
      We can specify char literals in Unicode representation i.e. '\uxxxx'. Here 'xxxx' is four digit hexa decimal number. Every escape characters is a valid char literal.
      e.g. char c = '/u0061'; // This is valid. Here the output is 'a' because four digit hexa decimal number of 97 is 0061. 
      e.g. char c = '/n'; // This is valid.
      e.g. char c = '/t'; // This is valid.
      e.g. char c = 65536; // This is invalid.
      e.g. char c = 0XBeer; // This is invalid.
      e.g. char c = \uface; // This is invalid because quotes are missing.
      e.g. char c = '\ubeef'; // This is valid.
      e.g. char c = '\m'; // This is invalid because this is not an escape character.
      e.g. char c = '\iface'; // This is invalid because this not an escape character.

    5. String Literals: Any sequence of characters within double quotes is treated as String Literal.
      e.g. String s = "Raj Kanchan";
    6. Binary Literals: For Integral data types until 1.6 version we can specify literal value in the following ways.
      1. Decimal Form
      2. Octal Form
      3. Hexa Decimal Form

        But from 1.7 version onwards we can specify literal value even in Binary Form also. The allowed digits are 0 and 1. Literal value should be prefixed with 0b or 0B. 
        e.g. int x = 0B1111; // This is valid. The output of is 15.

  2. Arrays
  3. Types of Variables
  4. var-arg methods
  5. main method
  6. Command line arguments
  7. Java Coding Standard

Do's and Dont's

  • char ch = null; will throw compatible error.
  • Floating Point Literals can be specified only in decimal form. It cannot be specified in Octal or Hexa Decimal Form.
    e.g. double d = 123.456; // This is valid.
    e.g. double d = 0123.456; // This is valid because the compiler has converted 0123.456 as Decimal form. This is not an example of Octal form.
    e.g. double d = 0x123.456; // This is invalid because the compiler cannot convert 0x123.456 to a Decimal form. This is an example of Hexa Decimal Form.
    e.g. double d = 0786; // This is invalid because the range of Octal form is exceeded.
    e.g. double d = 0XFace; // This is valid because the literal comes under the range of Hexa Decimal Form.
    e.g. double d = 0786.0; // This is valid because the compiler has converted 0786.0 as Decimal form. This is not an example of Octal form.
    e.g. double d = 0XFace.0; // This is invalid because the compiler cannot convert 0XFace.0 to Decimal Form. This is an example of Hexa Decimal Form.
    e.g. double d = 10; // This is valid
    e.g. double d = 0777; // This is valid.
  • Don't assign Floating Point Literals to Integral types. 
    e.g. double d = 10; // This is valid.
    e.g. int x = 10.0; // This is invalid.
  • We can specify Floating Point Literal even in exponential form (Scientific notation)
    e.g. double d = 1.2e3; // This is valid.
    e.g. float f = 1.2e3; // This is invalid. Found double required float.
    e.g. float f = 1.2e3f; // This is valid. 

Reference



Comments