10x Blogs Directory

Saturday, February 21, 2009

Primitive Data Types


  • The Java programming language defines eight primitive data types.
    -boolean (for logical)‏
    -char (for textual)‏
    -byte
    -short
    -int
    -long (integral)‏
    -double
    -float (floating point).




Logical-boolean

  • A boolean data type represents two states: true and false.

  • An example is, boolean result = true;

  • The example shown above, declares a variable named result as boolean type and assigns it a value of true.
Textual-char
  • A character data type (char), represents a single Unicode character.

  • It must have its literal enclosed in single quotes(’ ’).

  • For example: ‘a’ //The letter a ‘\t’ //A tab

  • To represent special characters like ' (single quotes) or " (double quotes), use the escape character '\'.

  • For example, '\'' //for single quotes '\"' //for double quotes

  • Although, String is not a primitive data type (it is a Class), we will just introduce String in this section.

  • A String represents a data type that contains multiple characters. It is not a primitive data type, it is a class.

  • It has its literal enclosed in double quotes(“”).

  • For example: String message=“Hello world!”;
Integral – byte, short, int & long
  • Integral data types in Java uses three forms – decimal, octal or hexadecimal.

  • Examples are, 2 //The decimal value 2 077 //The leading 0 indicates an octal value 0xBACC //The leading 0x indicates a hex value

  • Integral types has int as default data type.

  • You can define its long value by appending the letter l or L.

  • For example:10L

Floating Point – float and double

  • Floating point types has double as default data type.

  • Floating-point literal includes either a decimal point or one of the following, E or e //(add exponential value) F or f //(float) D or d //(double)

  • Examples are:

3.14 //A simple floating-point value (a double)

6.02E23 //A large floating-point value

2.718F //A simple float size value

123.4E+306D//A large double value with redundant D