Data Types in JAVA

34


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  ---------------------------------------------> 1 byte
float  --------------------------------------------->  4 byte
double ---‐-----------------------‐----------------------> 8-16 byte flexible range
boolean -----------------------------------------> 1bit

primitive datatype has been defined by C and C++ Languages hence java uses JNI (java native interface) at runtime to link their library to JVM.

Java is not 100% object-oriented because it supports primitive data type that has been created by C and C++ languages.
Program Explanation of Java:-

class DataTypeExample
{

   public static void main(String args[])
   {
         int a=10;
         char ch='a';
         float b=12.34F;
         String s = "hello";
         byte bt = 12;
         short st = 23; 
         double d = 123456.789;
         System.out.println("a="+a);
         System.out.println("b="+b);
         System.out.println("ch="+ch);
         System.out.println(s); 
         System.out.println(bt); 
         System.out.println(st); 
         System.out.println(d);         
      }
}
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.
String Program ?
class StringExample
{
public static void main(String args[])
{
String s = "hello";   //String pool memory
String s1 = "hello";  //String pool memory
String s2 = new String("hello");  //heap memory

if(s==s2)
{
    System.out.println("equall");
}
else
{
    System.out.println("not equall");
}

if(s.equals(s2))
{
    System.out.println("equall");
}
else
{
    System.out.println("not equall");
}
/*Object o=12;
Object o1=12.34;
Object o2 = true;

System.out.println(o + " "+o1+ ""+ o2);
String s = new String("hello");
String s2=s.concat("world");
System.out.println(s);  //hello
System.out.println(s2);
StringBuilder sb = new StringBuilder("hello");
sb.append("world");
System.out.println(sb);
StringBuffer sbs = new StringBuffer("hello");
sbs.append("world");
System.out.println(sbs);
*/
}
}
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 uses two different types of memory allocation


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

   it will directly store data without any reference, 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 derived type datatype:-

it will indirectly store data using the address, first, we provide an address then we can store value. all derived datatype of java use heap memory. Heap memory will be created at run time
Heap memory is also called dynamic memory and runtime memory allocation.
String s = "hello";       String pool
String s1 = new String("hello");  //String heap
s is the address
    
Type Conversion in Java:-
it is used to convert one data type to another. means if we want to convert int type to object or object type to int or any other type then type conversion will be used.
class Typec
{
    public static void main(String args[])
    {
       String s = "123";
       String s1 = "456";
       int s2 = Integer.parseInt(s) + Integer.parseInt(s1);
       s = "12.34";
       s1 = "45.6";  
       float s3 = Float.parseFloat(s) + Float.parseFloat(s1);
       System.out.println(s2 + " " + s3);
    }
}
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 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 reducing 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);

   }
}

Another Exmple of Data type in Java:-

class UnboxingExample
{
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)o1;
      boolean bl = (boolean)o2; 
      String s = (String)o3; */
      int a=Integer.parseInt(o.toString());
      float b = Float.parseFloat(o1.toString());
      boolean bl = Boolean.parseBoolean(o2.toString());
      String s = o3.toString();
      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 afloat?
class SwapExample
{
   public static void main(String args[])
   {
        Object a=12;
        Object b=3.14F;
        Object c;
        c=a;
        a=b;
        b=c;
        System.out.println(a + " " +b);   
          }
}
WAP to perform multiplication of Complex Numbers?
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?
Tags

Post a Comment

34Comments

POST Answer of Questions and ASK to Doubt

  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
  16. //WAP to calculate Simple Interest where P and T will be Object type and r will be float type?
    class SimpleIntrest
    {
    public static void main (String args[])
    {
    Object p=10,t=10;
    Float r=1f;
    System.out.println(((int)p*(int)t*r)/100);

    }
    }

    ReplyDelete
  17. Write a program to perform the swapping of two numbers without using primitive datatype?


    import java.util.Scanner;
    public class nonprimetiveswap{
    public static void main(String args[]){
    Integer a=2, b=3, c=0;
    c=a;
    a=b;
    b=c;
    System.out.print("a is:" +a+ "\n");
    System.out.print("b is:" +b);
    }
    }

    ReplyDelete
  18. Write a program to perform the swapping of two numbers without using third veriable and primitive datatype?


    public class nonprimetiveswap2{
    public static void main(String args[]){
    Integer a=2, b=3;
    b=a+b;
    a=b-a;
    b=b-a;
    System.out.print("a is:" +a+ "\n");
    System.out.print("b is:" +b);
    }
    }

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



    public class nonprimetiveaddition{
    public static void main(String args[]){
    Integer a=2, b=3, c;
    c=a+b;
    System.out.print("a+b:" +c);
    }
    }

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

    Using Scanner Class

    import java.util.Scanner;
    class Check121
    {
    public static void main(String args[])
    {
    System.out.print("Enter Principle Ammount,Time,Rate:");
    Scanner Sc = new Scanner(System.in);
    Object P = Sc.nextInt();
    Object T = Sc.nextDouble();
    Float R = Sc.nextFloat();
    Double SI = ((Integer)P*(Float)R*(Double)T)/100;
    System.out.print("Total" +SI);
    }
    }

    ReplyDelete
  21. class Swap2
    {
    public static void main(String args[])
    {
    int a=5;
    float b=6f;
    Object o;
    o=a;
    a=(int)b;
    b=Integer.parseInt(o.toString());
    System.out.println(a+" "+b);


    }

    }

    ReplyDelete
  22. # nayansi purwar


    Q) WAP to calculate Simple Interest where P and T will be Object type and r will be double type?


    ss SimpleObj
    {

    public static void main(String[] args)
    {
    Object p=4500, r=4.7;
    double t=2.5;
    double si= (Double.parseDouble(p.toString())*t*(double)r)/100;
    System.out.println(si);

    }
    }

    ReplyDelete
  23. Q)WAP to calculate Simple Interest where P and T will be Object type and r will be float type?

    sol- class SimpleObj
    {

    public static void main(String[] args)
    {
    Object p=4500, t=4.7;
    float r=4.5f;
    double si= ((int)p*(double)t*r)/100;
    System.out.println(si);

    }
    }

    ReplyDelete
  24. #Nayansi Purwr

    Que)WAP to swap two numbers using third variable or using unboxing?

    class SwapBox
    {
    public static void main(String[] args)
    {

    Object a=10;
    float b= 3.4f;
    Object temp;

    temp=a; // temp= 10;
    a= b; // a=3.4;
    b=Float.parseFloat(temp.toString()); // b=10;
    System.out.println("a="+a+"b="+b);

    }
    }

    ReplyDelete
  25. Abhishek chauhan
    //boxing ,implicit conversion
    class Datatype
    {
    public static void main(String args[])
    {
    int a=20;
    Object o;
    o=a;

    int b=(int)o;
    System.out.println(o+" "+b);


    }


    }

    ReplyDelete
  26. Abhishek chauhan
    // All Datatype example
    public class DataType
    {
    public static void main(String args[])
    {
    int a=0;
    char ch='a';
    float b=20.3f;
    String s="hello";
    byte bt=20;
    short st=10;
    double d=2322.3;
    System.out.println("a="+a);
    System.out.println("ch="+ch);
    System.out.println("b="+b);
    System.out.println(s);
    System.out.println(bt);
    System.out.println(st);
    System.out.println(d);

    }
    }

    ReplyDelete
  27. class Simplei
    {
    public static void main(String args[])
    {
    Object p=2000,t=2;
    float r=2.0f,si;
    Float d=Float.parseFloat(p.toString());
    Float m=Float.parseFloat(t.toString());
    System.out.println(d);
    System.out.println(m);

    si=d*m*r/100;

    System.out.println(si);

    }



    }

    ReplyDelete
  28. //WAP to convert temeprature from celsious to fahrenhite?
    import java.util.Scanner;
    class Celcius_to_farenite
    {
    public static void main(String abc[])
    {
    double celcius;
    int temp;
    Scanner sc= new Scanner(System.in);
    System.out.println("enter temperature");
    temp=sc.nextInt();
    celcius=temp*9/5+32;
    System.out.println("temperature in ferenite is" +celcius);
    }
    }

    ReplyDelete
  29. # Program to Implement Addition without using primitive data type

    class Addition
    {
    public static void main(String args[])
    {
    Object num1=10,num2=20,num3;
    num3 = Integer.parseInt(num1.toString())+ Integer.parseInt(num2.toString

    ());
    System.out.println(num3);

    }

    }

    ReplyDelete
  30. Addition Program using Wrapper Class Integer

    class Addition
    {
    public static void main(String args[])
    {
    Integer num1=10,num2=20,num3;
    num3 = num1+num2;
    System.out.println(num3);

    }

    }

    ReplyDelete
  31. Addition Program using Wrapper Class Object:-

    class Addition
    {
    public static void main(String args[])
    {

    Integer num1 = new Integer(10);
    Integer num2 = new Integer(20);
    Integer num3;
    num3 = num1+num2;
    System.out.println(num3);

    }

    }

    ReplyDelete
  32. Sandeep kelwa

    class Swap2
    {
    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);
    }
    }

    ReplyDelete
  33. Sandeep kelwa

    class Implicitly
    {
    public static void main(String args[])
    {
    Object o=10;
    int a=20;
    o=a;
    System.out.println(a);

    int b;
    b=(int)o;
    System.out.println(b);

    int a1=10;
    Integer a2=a1;
    System.out.println(a2);

    float b2=20;
    Float b1=b2;
    System.out.println(b1);

    int x;
    float y=12.5F;
    x=(int)y;
    System.out.println(x);
    }
    }

    ReplyDelete
  34. // Q:- WAP to find simple interest where principal amount (p) is in String form and rate (r) is in Object form.

    public class SItypecast {

    public static void main(String[] args) {

    String p="1000";
    Object r = "2";
    double t=2;
    double P= (Double.parseDouble(p.toString()));
    double R = (Double.parseDouble(r.toString()));

    double si = (P*R*t)/100;

    System.out.println(si);


    }

    }

    ReplyDelete
Post a Comment