BeignTech - Tech That Matters Data Types in Java

Data Types in Java

 In a java there are are mainly two types of data types.

  1. Primitive data types: This data types include a Numeric datatypes , Char datatypes, Boolean datatypes
  2. Non-primitive data types - such as String, Arrays and Classes
1. Primitive DataTypes(8)
  • Internal datatypes: - byte, short , int , long
  • floating-point-datatypes:- float , double


1. byte:-

         Size = 8 - bit ( 1 byte)
         Max-value = 127
         Min-value = -128
         Range = -128 to +127
  • The most significant Bit is called "sign bit". 0 mans +ve value, 1 means -ve value 
  • +ve numbers represented directly in the memory where as -ve numbers represnted in 2's componenet form
    • byte b =100;
    • byte b =127;
  • byte datatypes is best suitable if we want to handle data in termsof strreams either from the file or the network
2. Short:-
     
        Size = 2- bytes (16-bits)
        Range= -32768 to 32767
  • Most recently used datatypes in java is 'Short'
  • short datatypes is best suitable if we are uisng 16-bit processors like 8086 but these processers are completly out dated ans hence correspending short datatypes is also out dated.
3. Int:-

        The most commonly used datatypes is 'int'.
        Size = 4-bytes
        range = -2147483648 to 2147483647

Note*
  • In  c language the size of int is varied from platform to platform for 16-bit processers it is 2-bytes but for 32-bit processor.
  • The main advantege of this approach is read and write operation we can perform very effiently and performance will be improved. But the main Disadvanteges of this approach is the chang od failing c program is very very high if we changing plaform C program is not considered as rubust.
  • But in java the Size of int is always 4-bytes  irrespective of any platform. The main advantege of this platforms here Java is considered as robust language
4. long:-
        
      when ever int is not enough to hold big values then we should go for long datatypes.
        Example.
                        To represent the amount of distance travelled by light in 1000days int is enough                                    complusury we should go for long type .
                        logn l = 1.23000*60*60*24*1000miles;

Note
  • All the above datatypes (byte, short ,int, long) ment for representing whole values.
  • if we want to respresent real numbers compulesary we should go for floating point datatypes.

 Floating Point datatypes

  1. float
  2. double

 

 

Float

double

1.    Size: 4-bytes

1.  Size: 8-bytes

2.    Range: -  -3.4e38 to 3.4e38

2. Range: -    -1.7e308 to 1.7e38

3.    If we want 5 to 6 decimal places of accuracy then we should go for float

3. If we want 14 to 15 decimal places of accuracy then we should go for double

4.    Float follows single precision

4. Double follows double precision


Boolean Datatypes

Size  = Not Applicable ( virtual machine dependency)

Range = Not Applicable (but allowed values are true/false)     

Ex.

  1. Boolean one = false  


Char Datatypes

In language like C/C++ we can use only ASCI characters  and to respresent all ASCI characters 8-bits are enough. hence char size is 1-byte.

But in java we can use unicode characters which covers world wide all alphabets sets. The number of unicode characters is '>256' . hence 1-byte is enough to respresent all characters compulsary we should go for 2-bytes.

Size = 2-bytes

Range = 0 to 65535

Read also.

 Identifiers in Java

Array In Java

Summary of primitive data types

Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object)   null
boolean false

Post a Comment

Previous Post Next Post