Search This Blog

Data Types concept in java:-

It is used to represent the pattern of data and the size of data in memory.

The datatype is mandatory to declare variable, constant, method, and properties in java.

int x;           Variable

final int X;  Constant

int addition()  Method
      return x;

Type of Data type in java:-

1 Primitive Datatype:-

all C and C++ data type is supported in Java

int   ----------------------------------------------> 4byte
char  ------------------------------------------->    2byte
byte  ---------------------------------------------> 1byte
float  --------------------------------------------->  8byte
double ------------------------------------------> 16byte
String  -------------------------------------------> depend on the number of char
boolean -----------------------------------------> 1byte

primitive datatype has been defined by C and C++ Languages hence java uses JNI (java native interface) at runtime to link this library to JVM.
Java is not 100% object-oriented because it supports primitive data type that has been defined by C and C++.

2 Derived Data type:-

this datatype is created for Java language

Object --->   It is the superclass of Java that can contain all element's values.

Integer      Wrapper Class
Float         Wrapper Class
Double      Wrapper Class

Derived datatype size is not fixed it will be managed by java runtime memory dynamically.

Memory allocation for datatype:-
Java Datatype use two different types of memory allocation

1 Stack memory for value type datatype:-

   it will directly store data without any address

    int a=10;

   this type of memory will be created at compile time.
   int ,char,float ,double,boolean,byte

   It is also called compile-time memory allocation on a java program.

2 Heap memory for reference type datatype:-

it will indirectly store data using the address, which means first we provide address then we can substitute value.

    all derived datatype use heap memory to store data.

    String s = "hello";       String pool

     String s1 = new String("hello");  //String heap

    s is the address
    Heap memory will be created at run time

Heap memory is also called dynamic memory and runtime memory allocation.

Type Conversion in Java:-

it is used to convert one data type to another.

1 implicit (automatic) type conversion or boxing:-

   it is used to convert a value type(primitive) to reference type(derived) directly.

   it is also called boxing because boxing means to increase small scale memory to a large scale memory.

   implicit conversion automatically performed.

    for example, Object type can contain any datatype value

   int (value type ) to Object (reference type) is called an implicit conversion.     

  int a=10;  //4byte
  Object o;  //unlimted

2 explicit (manually) type conversion or unboxing:-

   it is used to convert reference type to value type .it will be manually performed.

  for example, if we convert object type data to the integer type then we will manually convert it.

  unboxing means to reduce large scale elements to a small scale.

  Object o=10;
  int a=o;   //it provide error
  int a =(int)o; 

class DatatypeExample
   public static void main(String args[])
       int a=10;
       Object o;
       o=a;   //boxing ,implicit conversion
       int b=(int)o;
       System.out.println(o+" "+b); 



implicit example

class BoxingExample
    public static void main(String args[])
        Object o=10;
        int a=20;
        o=a;  //boxing
        int b;
        int a1=10;
        Integer a2 =a1;
        float b2=20;
        Float b3=b2;
        int x;
        float y=12.2F;
        Integer i =(int)y;
        int i1=i;



WAP to calculate Simple Interest where P and T will be Object type and r will be float type?

class SI
   public static void main(String args[])
      Object p=45000,t=2;
      float r=2.2F;
      float si = ((int)p*r*(int)t)/100;
      System.out.println("SI value is "+si);



using float datatype.

the float can not be converted into an object directly. first, we convert float to String type then we will convert String to float.

class SI
   public static void main(String args[])
      Object p=45000,t=2;
      float r=2.2F;
      float p1 = Float.parseFloat(p.toString());
      float t1 = Float.parseFloat(t.toString());
      float si = (p1*r*t1)/100;
      System.out.println("SI value is "+si);



WAP to swap two numbers where the first number is an integer and the second number is a float?

class Swap
   public static void main(String args[])
       Object a=10;
       float b=10.12F;
       Object o;
       o= b;
       a= b;
       System.out.println("a="+a+" b= "+b); 



WAP to perform multiplication of Complex Number?

class ComplexExample
   public static void main(String args[])
        int a=5,b=6,c=3,d=3;
        int r=a*c-b*d;
        int i= a*d+b*c;
        System.out.println(r + "+" + i+ "i"); 


WAP to convert temeprature from celsious to fahrenhite?

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post