Tuesday 8 November 2016

DATA TYPES IN JAVA



    The Primitive Types:Java defines eight primitive types of data:
byte,short,int,long,char,float,double and boolean.
The primitive types are also commonly referred to as simple types.

    Integers:
o   Includes byte, short, int and long.
o   All of these are signed, positive and negative values.
Name
Width
Range
Long
64
-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
Int
32
-2,147,483,648 to 2,147,483,647
Short
16
-32,768 to 32,767
Byte
8
-128 to 127

o   byte:
§  Smallest integer type.
§  Useful when working with a stream of data from a network or file.
§  Useful when working with row binary data that may not be directly compatible with Java’s other built-in types.
§  Declared by using byte keyword:
byte a,b;

o   short:
§  Signed 16 bit type.
§  Probably the least used Java type.
§  Declared by using shortkeyword:
short a,b;
o   int:
§  Signed 32 bit type.
§  Most commonly used integer type.
§  Generally used to control loops and to index arrays.
§  Declared by using int keyword:
int a,b;
o   long:
§  Signed 64 bit type.
§  Useful in occasions where an int type is not large enough to hold the desired value.
§  Declared by using long keyword:
long a,b;

    Floating-Point Types:
o   Includes float and double.
o   Also known as real numbers.
o   Used when evaluating expressions that requires fraction precision.

Name
Width
Range
Double
64
4.9e-324 to 1.8e+308
Float
32
1.4e-045 to 3.4e+038
o   float:
§  Specifies a single-precision value.
§  Useful when needed a fractional components, but don’t require a large degree of precision.
§  Declared by using float keyword:
float a,b;
o   double:
§  Specifies a double-precision value.
§  Useful when needed to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers.
§  Declared by using double keyword:
double a,b;
    Character Types:
o   Includes char.
o   In Java char is 16-bit type. The range of char is 0 to 65,536.
o   char data type can be used to perform arithmetic operations.
o   Declared by using char keyword:
char a,b; 
    Boolean Types:
o   Includes boolean.
o   It can have one of two possible values, true or false.
o   Used when conditional expressions that govern the control statements such as if or for are evaluated.
o   Declared by using boolean keyword:
boolean b;

No comments:
Write comments