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 Javaint ----------------------------------------------> 4byte
short ---------------------------------------------> 2byte
long ----------------------------------------------> 8byte
char -------------------------------------------> 2byte
byte ---------------------------------------------> 1 byte
float ---------------------------------------------> 4 byte
double ---‐-----------------------‐----------------------> 8-16 byte flexible range
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.
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.
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.
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
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.
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
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
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
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.
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.
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
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
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:-
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 exampleclass 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);
}
}
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);
}
}
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+4i2+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");
}
}
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);
}
}
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);
}
}
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");
}
}
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);
}
}
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);
}
}
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;
}
}
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);
}
}
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");
}
}
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);
}
}
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);
}
}
Nitesh bamotriya
ردحذفpublic class Implicitexample
{
public static void main(String[] args) {
int a=14;
Object o;
o=a;
System.out.println(o);
}
}
Nitesh bamotriya
ردحذفpublic class explcite
{
public static void main(String[] args) {
Object o=123;
int a =(int)o;
System.out.println(a);
}
}
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);
}
}
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);
}
}
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);
}
}
//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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
# 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);
}
}
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);
}
}
#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);
}
}
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);
}
}
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);
}
}
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);
}
}
//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);
}
}
# 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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
// 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);
}
}