Saturday, 31 May 2014

Primitive types and literals in java

In Java programs you must declare variable before they are used. Variable means it holds some value. during the program execution value of the variable may be changed. Variable may be of any type, here the type is the type of value variable roining  store in it(i.e) numeric or letters or the address.

 in java types are can be divided into two:

  1. Primitive Types.

  2. Reference Types.

here we are going to discuss only about primitive types.

Primitive Types:

in java there are 8 primitive data types:

       1.boolean     (true,false)
        2.byte           (8 bits == 1 byte)
        3.short         (2 bytes == 16 bits)
        4.char          (2 bytes == 16 bits) Unicode
        5.int             (4 bytes == 32 bits)
        6.float          (4 bytes == 32 bits)
        7.long          (8 bytes == 64 bits)
        8.double      (8 bytes == 64 bits)

boolean:
              it is a one bit representation. It has only two possible values true,false
boolean t = true; // Legal
boolean f = 0;// Compiler error!


 boolean literals are  true, false.

byte: The byte data type is an 8-bit signed two's complement integer.
 
            size : 1 byte (8 bits)
          range: -128 to 127.

short: The short data type is a 16-bit signed two's complement integer
           size: 2 bytes(16 bits)

int   : the int data type is a 32-bit signed two's complement integer
            size: 4 bytes(32 bits)

long: The long data type is a 64-bit two's complement integer
            size: 8 bytes(64 bits)
 long no = 1234234L
      
byte,short,int,long are save integer type of literals.

There are three ways to represent integer numbers in the Java language:
  
   decimal(base 10)
   octal (base 8)
   hexadecimal (base 16)
   
decimal  ex: 342
            342 = 3*100+4*10+2*1 

octal  : Octal integers use only the digits 0 to 7. In Java, you represent
an integer in octal form by placing a zero in front of the number, as follows:
 ex:       int six = 06;// Equal to decimal 6
int seven = 07;// Equal to decimal 7
int eight = 010; // Equal to decimal 8
int nine = 011;// Equal to decimal 9

hexadecimal:

Hexadecimal (hex for short) numbers are constructed
using 16 distinct symbols. Because we never invented single digit symbols for the
numbers 10 through 15, we use alphabetic characters to represent these digits.
Counting from 0 through 15 in hex looks like this:
0 1 2 3 4 5 6 7 8 9 a b c d e f
 
hexa numbers are represented as follows:
 int x = 0X0001;
 int y = 0x7f;
All three integer literals (octal, decimal, and hexadecimal) are defined as int by
default, but they may also be specified as long by placing a suffix of L or l after the number
  
char:The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).         size: 2 bytes(16 bits)
A char literal is represented by a single character in single quotes.
char a = 'a';
char b = '@';
You can also type in the Unicode value of the character, using the Unicode
notation of prefixing the value with \u as follows:
char letterN = '\u004E'; // The letter 'N'

float: The float data type is a single-precision 32-bit IEEE 754 floating point.
                    size: 4 bytes(64 bits)
   ex: float g = 49837849.029847F
 

double:The double data type is a double-precision 64-bit IEEE 754 floating point
Floating-point numbers are defined as a number, a decimal symbol, and more
numbers representing the fraction.
            size: 8 bytes(64 bits)
double d = 11301874.9881024;


** Some key points to remeber:

 1. There are 8 primitivite data types

 2. There are 4 types of literals in java

        a)integer literals--> base10,octal,hexa.
        b)char literals --> ex: 'c'(unicode representation)
        c)floating point representation: fraction numbers. 
        d)boolean representation: true, false.

3. except char remaining type are signed in java.
    signed means variable can store  positive and negative numbers.

  example byte range is -128 to 127.
  byte b = -122
  byte b = 127




 

     

         

  
        
        

No comments:

Post a Comment