Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

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 a variable, constant, method, and properties in 

java.

int x;             datatype on Variable

final int X;   datatype on Constant

int addition()   datatype on Method
{
      return x;
}





Type of Data type in java:-


1 Primitive Datatype:-

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

int   ----------------------------------------------> 4byte
short ---------------------------------------------> 2byte
long ----------------------------------------------> 8byte
char  ------------------------------------------->    2byte
byte  ---------------------------------------------> 1byte
float  --------------------------------------------->  8byte
double ------------------------------------------> 16byte
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 created by C and C++ languages.



2 Derived Data type:-

this datatype is created by Java language itself and coded using Java programming language.
These data type has been defined using a Class pattern.

Object --->   It is the superclass of Java that can contain all types of subclasses. 
String  ------------------------------------------->it is the predefined class of java and it's size depending on the number of char
StringBuffer---> It is used to contain multiple String data. It provides a mutable String Object.
StringBuilder --> It is used to contain multiple String data, it also provides mutable String Object.
DateTime --->  Contain Date and Time type data
Integer         Wrapper Class
Float            Wrapper Class
Double         Wrapper Class
Character    Wrapper Class
Long            Wrapper Class
Byte             Wrapper Class
Short           Wrapper Class
Array               It is used to store multiple similar type data using the proper sequence
Collection:-     It is used to store the collection of the di-similar types of data.
Class:-             It is called the User define the data type.
Enum:-            It is used to declare a constant set.


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


Question?

WAP to perform the addition of two numbers without using primitive datatype?

public class Main
{
public static void main(String[] args) {
Integer a=10,b=20,c;
c=a+b;
System.out.println(c);
}
}


Memory allocation for datatype:-

..................................................................................
Java Datatype use two different types of memory allocation


1 Stack memory for value type datatype or primitive datatype:-

   it will directly store data without any address

    int a=10;

   this type of memory will be created after 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 a 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, the 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
o=a;
System.out.println(o);

 
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
        System.out.println(o);
        int b;
        b=(int)o;
        System.out.println(b);
        int a1=10;
        Integer a2 =a1;
        System.out.println(a2);
        float b2=20;
        Float b3=b2;
        System.out.println(b3);
        int x;
        float y=12.2F;
        x=(int)y;
        System.out.println(x);
        Integer i =(int)y;
        int i1=i;
     
      }


}



Example of Object Type in Java:-

class Objectexample
{

   public static void main(String args[])
   {
      Object o=12;   //type object
      Object o1=12.34F;  //type object
      Object o2 = true;  // type object
      Object o3 = "hello"; // type object
      System.out.println(o + "," + o1 + "," + o2 + ","+ o3);

      int a =(int)o;
      float b = Float.parseFloat(o1.toString());
      boolean bl = (boolean)o2; 
      String s = (String)o3;
      
      System.out.println(a + "," +b + " "+bl + " "+s);

   }

}

ASSIGNMENT:-

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;
       
       o=a;
       a= b;
       b=Integer.parseInt(o.toString());
       System.out.println("a="+a+" b= "+b); 
       


    }


}



WAP to perform multiplication of Complex Number?

3+4i
2+5i


Solution:-
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?









15 Comments

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

  1. class Interest
    {
    public static void main(String args[])
    {
    Object p=1000,t=2;
    float r=2.5F;
    float si= ((int)p*r*(int)t)/100;
    System.out.println(si);
    }
    }

    ReplyDelete
  2. class Swapping
    {
    public static void main(String args[])
    {
    Object a=2;
    float b=2.7F;
    Object o;
    o= b;
    o=a;
    a= b;
    b=Integer.parseInt(o.toString());
    System.out.println("a="+a+" b= "+b);
    }
    }

    ReplyDelete
  3. class Complexmul
    {
    public static void main(String args[])
    {
    int a=2,b=4,c=5,d=-3;
    System.out.println("Number 1= "+a+"+("+c+"i)");
    System.out.println("Number 2= "+b+"+("+d+"i)");
    int x=a*b-c*d;
    int y=a*d+c*b;
    System.out.println("The Multiple is: "+x+"+"+y+"i");
    }
    }

    ReplyDelete
  4. class SI
    {
    public static void main(String[]arg)
    {
    Object p=55000, t=2;
    float r=2.2F;

    float si=((int)p*r*(int)t)/100;
    System.out.println(si);
    }
    }

    ReplyDelete
  5. Q:- WAP to calculate Simple Interest where P and T will be Object type and r will be float type ??
    Solution:-
    class Si
    {
    public static void main(String args[])
    {
    Object p=5000,t=3;
    float r=4.2F;
    float si = ((int)p*r*(int)t)/100;
    System.out.println("SI value is :- " + si);
    }
    }

    ReplyDelete
  6. Q:- Implicit Example :-
    Solution:-
    class Boxing
    {
    public static void main(String args[])
    {
    Object o=30;
    int a=60;
    o=a; //boxing
    System.out.println(o);
    int b;
    b=(int)o;
    System.out.println(b);
    int a1=30;
    Integer a2 =a1;
    System.out.println(a2);
    float b2=60;
    Float b3=b2;
    System.out.println(b3);
    int x;
    float y=10.2F;
    x=(int)y;
    System.out.println(x);
    Integer i =(int)y;
    int i1=i;
    }
    }

    ReplyDelete
  7. Q:- WAP to swap two numbers where the first number is an integer and the second number is a float ??
    Solution:-
    class Swap
    {
    public static void main(String args[])
    {
    Object a=15;
    float b=14.12F;
    Object o;
    o= b;

    o=a;
    a= b;
    b=Integer.parseInt(o.toString());
    System.out.println("a = "+a+" b = "+b);
    }
    }

    ReplyDelete
  8. Q:- WAP to perform multiplication of Complex Number ??
    Solution:-
    class Complex
    {
    public static void main(String args[])
    {
    int a=4,b=6,c=8,d=8;
    int r=a*c-b*d;
    int i= a*d+b*c;
    System.out.println(r + "+" + i+ "i");
    }
    }

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



    }



    }

    ReplyDelete
  10. Nitesh bamotriya
    public class Main
    {
    public static void main(String[] args) {
    Integer a =10;
    Integer b =20;
    Integer c=a+b;
    System.out.println("addition is" +c);

    }
    }

    ReplyDelete
  11. Nitesh bamotriya
    public class Implicitexample
    {
    public static void main(String[] args) {
    int a=14;
    Object o;
    o=a;
    System.out.println(o);

    }
    }

    ReplyDelete
  12. Nitesh bamotriya

    public class explcite
    {
    public static void main(String[] args) {
    Object o=123;
    int a =(int)o;

    System.out.println(a);

    }
    }

    ReplyDelete
  13. Nitesh bamotriya

    public class Simpleinterest
    {
    public static void main(String[] args) {
    Object p=1290,t=2;
    float r=2.4f;
    float si=(int)p*r*(int)t/100;

    System.out.println(si);

    }
    }

    ReplyDelete
  14. Nitesh bamotriya
    public class Simple interest
    {
    public static void main(String[] args) {
    Object p=1290,t=2;
    float r=2.4f;
    float p1=Float.parseFloat(p.toString());
    float t1=Float.parseFloat(t.toString());
    float si=(p1*r*t1)/100;


    System.out.println(si);

    }
    }

    ReplyDelete
  15. class Si
    {
    public static void main(String arg[])
    {
    Object p=50,t=2;
    float r=2.0f;
    float si=(int)p*(int)r*(int)t/100;
    System.out.println(si);
    }
    }

    ReplyDelete

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