Saturday 10 October 2015

Core Java with OCJP_SCJP Language Fundamentals Part-2 __ Data Types part-1

Java with Govind  
   

Datatypes

In java every variable has a type, every expression has a type and all types are strictly defined.All the assignments should be checked by the compiler for the type compatibility. Hence java language
considers as strongly typed language.Java is not considered as pure object oriented programming
language because several OOP features(like multiple inheritance, operator overloading) are not supported by java. Even java contains non-object primitive datatypes.

Except boolean and char all the remaining datatypes are signed datatypes i.e we can represent both +ve and –ve numbers.

byte

              Size :  8-bits
             Range:  -128 to 127
-ve numbers can represented in 2’s compliment form.
   Ex:
         byte b = 10;
         byte b = 127;

         byte b = 130; --->  C.E: possible loss of precision

      byte b = true;  ----> C.E: Incompatible types found: boolean
                                                                  required: byte
byte datatype is best suitable if we are handling data either from file or form network.

short


     
Ex:
        short s = 10;
        short s = 32767;
        short s = 65535;---> C.E: possible loss of precision.
        short s = true;   ---->  C.E: Incompatible types
 short is best suitable datatype for 16 -bit process. But currently these are completely  out dated and hence the corresponding datatypes also no one is using.

int

      The most commonly used datatype is int. 
       size = 4 bytes

The size of int is always fixed irrespective of platform hence the chance of failing java program is
very less if u r changing the platform hence the java is considered as Robust.

Core Java with OCJP_SCJP Language Fundamentals Part- 3 __ Data Types part-II


long

       if int is not enough to hold big values then we should go for long-datatype
     


Ex:
The amount of distance traveled by light in 1000days can be represented by long
datatype only and int is not enough.

floating -point


           for representing real numbers(numbers with decimal points)


boolean datatye

         size = not a pplicable(virtual machine dependent).

         range = not applicable but allowed values are true/false

Ex:

int x = 0;
if(x)
{
System.out.println("Hello");
}
else
{
System.out.println("Hai");
}
C.E: Incompatible types found :int
required: boolean.

char


Comparison table for java primitive datatypes








No comments:

Post a Comment