
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Primitive Data Type vs Object Data Type in Java
In a Java environment, every variable contains with some data types, which specify the value and type of a sorted identifier.
There are two categories
Primitive Data type
Non-Primitive Data type or Object data type
The primitive data types are some predefined data types with some specific size and type. This method has some standard values with the types know as byte, short, int, long, float, double, char and boolean. When we want to run a primitive structure, then it stores the data in a stack and assign a value for the process. On the other hand, Object data type is a reference data type which includes with arrays, strings, classes, interfaces etc. Reference variable will be sorted and store in a stack and the original object takes a heap sorting path.
Algorithm to Create Primitive Data Type and Object Data Type
In this possible algorithm, we are going to show you how to perform a primitive data type and object data type in a Java environment. By using this algorithm, we are going to build some Java syntax to get a broad view of the problem statement.
Step 1 Start the process.
Step 2 Declare and import the Java packages.
Step 3 Declare a public class.
Step 4 Declare the argument string.
Step 5 Declare the input value method.
Step 6 Declare an array stream.
Step 7 Populate it.
Step 8 Mention the method as primitive or objective.
Step 9 Set up a memory heap.
Step 10 Get the affected value.
Step 11 Print the output.
Step 12 Terminate the process.
Syntax to Create Primitive Data Type and Object Data Type
public class ARBRDDTest{ public static void main(String[] args){ int[] x = new int[3]; System.out.println(x.getClass().getName()); } } public class TestARBRDD { public static void main(String[] args){ System.out.println(args instanceof Object); int[] arr = new int[2]; System.out.println(arr instanceof Object); } }
In this possible syntax above, we have tried to show you.
Approaches to Follow
Approach 1 Java code to define Primitive Data Type by using the Integer data, Floating-Point Number, Character data and Boolean data
Approach 2 Java code to define Object Data Type by using the method to make a change in the value by using changing method and define objective data type by BigDecimal object method
Approach 1
Using the Integer Data, Floating-Point Number, Character Data and Boolean Data.
Use of Primitive Data Type by the Integer Data
In this possible approach, we are going to apply the primitive data type with the integer data type. By following this approach we are going describe the process of working of the primitive data type in a Java environment .
public static void main(String[] args){ System.out.print("Y" + "O"); System.out.print('L' + 'O'); } public static void main(String[] args){ System.out.print("Y" + "O"); System.out.print('L'); System.out.print('O'); }
Example
//Java code to define Primitive Data Type by using the Integer data public class ARBRDD { public static void main(String[] args){ byte b = 16; System.out.println("b= " + b); short s = 07; System.out.println("s= " + s); int i = 2001; System.out.println("i= " + i); long l = 1997; System.out.println("l= " + l); } }
Output
b= 16 s= 7 i= 2001 l= 1997
Use of Primitive Data Type by the Floating-Point Data
In this possible approach, we are going to apply the primitive data type with the Floating Point data type. By following this approach we are going describe the process of working of the primitive data type in a Java environment .
Example
//Java code to define Primitive Data Type by using the Floating-Point Number data public class ARBRDD{ public static void main(String[] args){ float f = 2001.10f; System.out.println("f= " + f); double d = 1997.10; System.out.println("d= " + d); } }
Output
f= 2001.1 d= 1997.1
Use of Primitive Data Type by the Character Data
In this possible approach, we are going to apply the primitive data type with the character data type. By following this approach we are going describe the process of working of the primitive data type in a Java environment.
Example
//Java code to define Primitive Data Type by using the character data public class ARBRDD{ public static void main(String[] args){ char ch = 'S'; System.out.println(ch); char ch2 = '&'; System.out.println(ch2); char ch3 = '$'; System.out.println(ch3); } }
Output
S & $
Use of Primitive Data Type by the Boolean Data
In this possible approach, we are going to apply the primitive data type with the boolean data type. By following this approach we are going describe the process of working of the primitive data type in a Java environment .
Example
//Java code to define Primitive Data Type by using the Boolean data public class ARBBRDD{ public static void main(String[] args){ boolean t = true; System.out.println(t); boolean f = false; System.out.println(f); } }
Output
true false
Approach 2
Using Changing Method and Define Objective Data Type by BigDecimal Object Method.
Use of the Value Changing Method
In this possible approach, we are going to apply the objective data type with by the BigDecimal object method. By following this approach we are going describe the process of working of the primitive data type in a Java environment . Here are some basic declaration mentioned below
byte byteData= 88; //declaring the byte data type int intData= 20; // declaring the integer data type short shortData= 6000; //declaring the short data type long longData = 20000000000000L; // declaring the long data type float floatdata= 1.1f; // declaring the float data type double doubleData = 29.94d; // declaring the double data type boolean booleanData= true; //declaring the boolean data type char charData = 'A'; // declaring the character data type
Example
//Java code to define Object Data Type by using the method to make a change in the value of y which will not affect the value of x import java.lang.*; import java.util.*; public class ARBRDD { public static void main(String ar[]){ System.out.println("OBJECTIVE DATA TYPES ARE HERE\n"); int x = 10; int y = x; System.out.print("Initially: "); System.out.println("x = " + x + ", y = " + y); y = 30; System.out.print("After changing y to 30: "); System.out.println("x = " + x + ", y = " + y); System.out.println( "**Only value of y is affected here " + "because of Primitive Data Type\n"); System.out.println("REFERENCE DATA TYPES\n"); int[] c = { 10, 20, 30, 40 }; int[] d = c; System.out.println("Initially"); System.out.println("Array c: " + Arrays.toString(c)); System.out.println("Array d: " + Arrays.toString(d)); System.out.println("\nModifying the value at " + "index 1 to 50 in array d\n"); d[1] = 50; System.out.println("After modification"); System.out.println("Array c: " + Arrays.toString(c)); System.out.println("Array d: " + Arrays.toString(d)); System.out.println( "**Here value of c[1] is also affected " + "because of Reference Data Type\n"); } }
Output
OBJECTIVE DATA TYPES ARE HERE Initially: x = 10, y = 10 After changing y to 30: x = 10, y = 30 **Only value of y is affected here because of Primitive Data Type REFERENCE DATA TYPES Initially Array c: [10, 20, 30, 40] Array d: [10, 20, 30, 40] Modifying the value at index 1 to 50 in array d After modification Array c: [10, 50, 30, 40] Array d: [10, 50, 30, 40] **Here value of c[1] is also affected because of Reference Data Type
Use of the Java.math.BigDecimal Object Method
In this possible approach, we are going to apply the objective data type with by the java.math.BigDecimal object method. By following this approach we are going describe the process of working of the primitive data type in a Java environment .
Example
//Java code to define Object Data Type by using the BigDecimal object method import java.io.*; import java.math.BigDecimal; public class ARBRDD { public static void main(String[] args){ BigDecimal num1 = new BigDecimal("12"); int num2 = 15; System.out.println( "Addition of num1 and num2 = " + num1.add(new BigDecimal(num2))); System.out.println( "Subtraction of num1 and num2 = " + num1.subtract(new BigDecimal(num2))); System.out.println( "Multiplication of num1 and num2 = " + num1.multiply(new BigDecimal(num2))); System.out.println( "Division of num1 and num2 = " + num1.divide(new BigDecimal(num2))); } }
Output
Addition of num1 and num2 = 27 Subtraction of num1 and num2 = -3 Multiplication of num1 and num2 = 180 Division of num1 and num2 is = 0.8
Conclusion
Today in this article, we have learned about the various methods how to operate with the primitive and objective data type. By using the above mentioned syntax and algorithm we have built some Java codes to explain the problem statement in an efficient manner.
Also Read: Java Interview Questions and Answers