Hot Posts

hot/hot-posts

Core Java - Fundamentals I (Identifiers, datatypes and literals)





Identifiers
  1. It can be a class,variable or label name.They are case sensitive.
  2. Allowed characters are A-z,0-9 and special characters like _ and $.
  3. If we use any other characters we will get compile time error.
  4. Identifier can't start with a digit.
  5. Ideally there is no limit but recommended size is < 15
  6. Reserved Characters can't be used as identifiers.
  7. Predefined classes can be used as identifiers but is not recommended.
Datatypes
  1. Numeric (represents numbers)

    • Byte (Whole numbers) - Default is 0

      1. Size is 8 bits
      2. min value is -128 and max is 127
      3. Negative  numbers are represented in 2s complement form.
      4. We get PLP (possible loss of precision C.E) if we exceed the limit or assign some other data like Boolean ,String or float.
      5. It is suitable to handle streams of data from file or network.
    • Short (Whole numbers) - Default is 0
      1. Size is 2 Bytes
      2. Range is -32768 to 32767
      3. It is rarely used, usually for 16 bit processors.
    • Int (Whole numbers) - Default is 0
      1. Size is 4 Bytes
      2. Range is -2e31 to 2e31-1
      3. Helps in being robust at the cost of performance compared to C.
    • Long (Long Int) (Whole numbers) - Default is 0
      1. Size is 8 Bytes
      2. Range is -2e-63 to -2e63-1
    • Float vs Double (Real Numbers) - Default is 0.0
      1. Size is 4 vs 8 Bytes
      2. Range is -3.4e38 to 3.4e38 vs -1.7e308 to 1.7e308
      3. Accuracy in 5 to 6 decimal places vs 14 to 15 decimal places.
      4. Single precision vs double precision
          2. Boolean - Default is false
    1. Size and range is not applicable, it can be either true or false (Case Sensitive)
    2. Else we get compile time error of Incompatible Type (I.T)
          3. Char - Default is 0 (Blank Space)
    1. Size is 2 Bytes to accommodate Unicode compared to C (ASCII 1 Byte) 
    2. Range is 0 to 65535
Literals
  1. It is a constant to be assigned to variable.
  2. Decimals are 0 to 9, Octals are 0 to 7 prefixed by 0 & Hexadecimal literals are A-F (Case insensitive) prefixed by 0x.
  3. If the above criteria is not fullfilled we get C.E
  4. Long ,float and double Literals can be siffixed by l, f and d respectively (Case Insensitive).
    1. Floating point literals can't be specified in other datatypes than Decimals.
    2. We can specify floating  point literal in scientific form (2.2e51).
  5. Boolean literals are important during evaluation of expressions.
  6. Char literal
    1. Char literals are in single inverted commas with single character ,else we get C.E.Can be in decimal,octal or hexadecimal form till 65536.No floating point literals.
    2. Char literals can be expressed in unicode ('\uxxxx') 4 digit hexadecimal number eg : '\u0061' > a
    3. Every escape character is char literal
  7. Any sequence of characters written within double quotes is String literal
  8. Promotions of Datatypes handled by compiler.
    1.  byte>short>int>long>float>double
    2. char>int>long>float>double

Post a Comment

0 Comments