Java Data Types
In this tutorial, we will learn about what data types are present in java, declaration of data type and an example of one customized a user defined data type.
What is a data type in Java
Before declaring a variable, it requires to declare its type. Type specifies which kind of data it can be holding and type of a data can be number or character or complex type. Based on the declaration of the type subsequent memory will be allocated for that specific variable.
Types of data types:
In Java, data types are classified broadly two types. One is being the primitive type and other being the user defined type.
Primitive types
There are 8 primitives are available, each primitive type specifies what type of data it can hold or store in the memory in other words primitives are classified into 8 types based on the type of data holding by those with range.
Primitive will have the size limit and it can hold the data up to a certain range only. Before declaring as primitive one should have an idea about size of the data before declaring.
The byte type is an 8-bit signed two’s complement integer, this type can hold min value of -128 and max value of 127. While writing an application where memory is bigger constraint can help to reduce the memory usage, if the value falls under its limit.
Declaration of byte type:
Byte data type variable value can be declared in numeric or binary format but the value should be within range of byte.
byte b = 36; byte bits = 0b00100100;
In the above, we declared byte type one with numeric format other with binary format. While specifying binary format it should be prefix with ‘0b‘
The short data type is a 16-bit signed two’s complement integer, this type can hold min value of -32,768 and max value of 32,767. It follows the same principle as byte and can be used where memory is constraint.
Declaration of short type:
short data type variable value can be declared in numeric or binary format but the value should be within its range.
short shrt = 36; short shInbinary = 0b00100100;
The int type is a 32-bit signed two’s complement integer, this type can hold min value of -2,147,483,648 and max value of 2,147,483,647.
Declaration of int type:
int data type variable value can be declared in numeric or binary format but the value should be within its range.
int intNumeric = 36; int intbinary = 0b00100100;
The long type is a 64-bit signed two’s complement integer, this type can hold min value of -9,223,372,036,854,775,808 and max value of 9,223,372,036,854,775,807.
Declaration of long type:
long data type variable value should be declared with the postfix L or l otherwise will be considered as int type while value with in the range of int type outside of int range will cause a compile time error.
long longVariable = 9223372036854775807L;
It is recommended to use L instead of l otherwise it is hard to distinguish it.
The float type is a single-precision 32-bit floating point follows IEEE-754 standard, IEEE-754 describes floating point formats. This type can hold up to 7 decimal digits of precision.
Declaration of float type:
float data type variable value should be declared with the postfix F or f otherwise will be considered as double type.
float floatVariable = 922.33721F;
The double type is a double-precision 64-bit floating point follows IEEE-754 standard, IEEE-754 describes floating point formats. This type can hold up to 15 decimal digits of precision.
Declaration of double type:
double data type variable value declaring with the postfix D or d is optional.
double doubleVariable = 922.33721345123;
Size, range, and default values of primitives shown in the following table.
S.No | Data Type | Size | Range | Default Value |
---|---|---|---|---|
1 | byte | 1 byte | store numbers from -128 to 127 | 0 |
2 | short | 2 bytes | stores numbers from -32,768 to 32,767 | 0 |
3 | int | 4 bytes | stores numbers from -2,147,483,648 to 2,147,483,647 | 0 |
4 | long | 8 bytes | stores numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L |
5 | float | 4 bytes | stores decimal numbers, stores up to 7 decimal digits of precision. | 0.0f |
6 | double | 8 bytes | stores decimal numbers, stores up to 15 decimal digits of precision. | 0.0d |
7 | char | 2 bytes | Stores single character | ‘\u0000’ |
8 | boolean | 1 bit | stores either true or false | false |
Complex or user-defined data types:
As we learned, datatype can be declared as user defined type and will consider one example as such and write an example of custom datatype.
While writing an application, it requires to have user-defined type. Suppose if we are writing a program to display student details. Student will be having name, studying class, group , address, and school name. To handle this requirement custom data type is required.
Student will be considered as one example, in which student will have name, class, group, address and institute name.
We do not have inbuilt type for Student, it requires to customize this type.
Following example will show how to customize the student datatype.
Complex or user-defined type example:
The following is one of the example of custom (or) user-defined type.
class StudentDataType public String studentName; public String studentClass; public String studentGroup; public String studentAddress; public String studentInstituteName; public StudentDataType(String studentName, String studentClass, String studentGroup, String studentAddress, String studentInstituteName) < this.studentName = studentName; this.studentClass = studentClass; this.studentGroup = studentGroup; this.studentAddress = studentAddress; this.studentInstituteName = studentInstituteName; > public String getStudentName() < return this.studentName; > public String getStudentClass() < return this.studentClass; > public String getStudentGroup() < return this.studentGroup; > public String getStudentAddress() < return this.studentAddress; > public String getStudentInstituteName() < return this.studentInstituteName; > > public class Student public static void main(String[] args) < StudentDataType student = new StudentDataType("Varun", "8th Class", "GROUP-A", "Delhi,India", "Higher School of Delhi"); System.out.println("Student Name :"+student.getStudentName()); System.out.println("Student Class :"+student.getStudentClass()); System.out.println("Student Group :"+student.getStudentGroup()); System.out.println("Student Address :"+student.getStudentAddress()); System.out.println("Student Institute name :" +student.getStudentInstituteName()); > >
Student Name :Varun Student Class :8th Class Student Group :GROUP-A Student Address :Delhi,India Student Institute name :Higher School of Delhi
In this tutorial, we have learned about the java data types of both primitive and custom with a working example of it.
Java User-defined Data Types
Java User-defined data types are the customized data types created by the developers by exploiting the special features offered by the Java language. This customized data type combines data types of a group of data elements with homogenous or assorted data types. It utilizes the object-oriented functionalities of Java and allows creation of any kind of data types as per the need.
Web development, programming languages, Software testing & others
These data types have versatile characteristics and behavior and it provides greater flexibility to developers in improving their productivity and maintainability of the programs. Let’s go through features of these special data types in this article.
Data types of all the variables should be declared well ahead of its compilation and it should be one from the standard type that Java offers. The variables cannot hold any other data type than what was declared. But the user-defined data type provides the flexibility to define the data type at a group level.
User-defined Data Types in Java
Two major User defined data types are:
1. Class
Java a true object-oriented language is full of Classes that encapsulate everything from data elements that acts as instance variables and functions to process the data. It also provides templates to create Objects that are instances of Class that contain a method for performing the defined functions and interact with other parts of the program.
A class typically contains
Invoking variables with data types
How Objects are created?
Objects are created or instantiated from the class and used as individual instance of the class. Instance variables are created for each of the objects to interact with other components of the program and there could be as many objects created and each object will have
- Clear identity with a unique name
- Different set of instance variables to reflect its state.
- All the functionalities that define its behavior
Each class is a user-defined data type with the combined effect of all the data types of its data elements and each object is a variable data.
Example #1: Class Definition
// Sample program to define Class, Object and Method class ValueStore //Class definition with default access < int Rate; // Variables with standard data types int Qty; void AssignData(int A, int B) // method definition < Rate = A; // Assigning the values Qty = B; >int ComputeValue() // Another method to compute value < int value; // variable definition value = Rate * Qty; return(value); // Returning the value >>
Class with name ValueStore with default access is defined. It has two instance variables Rate, QTY used in the method interactions. Two methods AssignData, ComputeValue with respective function codes are created inside the class.
public class OrderValueCompute // Public class < public static void main(String[] args) // Execution starts here < int value1; // variable definition int value2; ValueStore VS1; // Object VS1 definition VS1 = new ValueStore(); // Object VS1 instantiated ValueStore VS2; // Object VS2 definition VS2 = new ValueStore(); // Object VS2 instantiated VS1.AssignData (200,10); // Assigndata method in Object VS1 invoked //with values value1 = VS1.ComputeValue(); // Computedata method in object VS1 // invoked VS2.AssignData (500,20); // Assigndata method in Object VS2 // invoked with values value2 = VS2.ComputeValue(); // Computedata method in object VS2 //invoked System.out.println("Order value 1 " +value1); // Output 1 displayed System.out.println("Order Value 2 " +value2); // Output 2 displayed >>
In the execution class, the objects in the other class are defined, instantiated. Methods are invoked. Results obtained and displayed. Class is a user-defined data type and each object is a variable in it.
2. Interface
Interface is similar to Class in architecture. It has data variables and methods. It broadly suggests what the classes calling it should do but it does not suggest how it should be carried out.
The method in an interface differs from a normal method in a class by
- It is not full-fledged
- It is only an abstract and it has the only a definition and the body is empty.
- It will not perform any functionalities as such
Interfaces cannot instantiate any objects like class. But it facilitates abstraction, Multiple inheritances, and loose coupling of classes.
Class extends to another super class by a level. Interface extends to another super interface by a level. Class implements interface and multiple inheritance is achieved indirectly which is otherwise not possible in a class.
Each interface created by a user has a unique data type with a combined effect of its data elements. Objects created in these classes uses interfaces also and each of these objects are the variables for interface data types.
Below example shows how classes implement interfaces, how objects of these classes are created linking the interfaces, and how they are executed.
// Sample program to demonstrate Interfaces interface Intface //Interface is defined < // It has its own data type public void dayname(); //Abstract method within the interface >class day1 implements Intface < // Class interacts through public void dayname() // interface implementation < // (each class is a variable for interface) System.out.println("Monday"); >> class day2 implements Intface < // Another class public void dayname() < System.out.println("Tuesday"); >> class day3 implements Intface < // Another class public void dayname() < System.out.println("Wednesday"); >> public class Subject < // Execution starts here public static void main(String[] args) < Intface t = new day1(); // Object of day1 class thru interface t.dayname(); // method invoked Intface tx = new day2(); // Object of day2 class thru interface tx.dayname(); Intface tx2 = new day3(); // Object of day3 class thru interface tx2.dayname(); >> // objects t, tx1, tx2 are the variables of // the interface data type
Conclusion
Class and interfaces are the major user-defined data types. Some forums have the opinion that String and Arrays are also part of user-defined types. Strictly speaking, they are part of standard data types of Java and hence they are not discussed.
Recommended Articles
This is a guide to Java User-Defined Exception. Here we discuss the Introduction, syntax, How Objects are created? examples with code implementation. You may also have a look at the following articles to learn more –
89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
JAVA Course Bundle — 78 Courses in 1 | 15 Mock Tests
416+ Hours of HD Videos
78 Courses
15 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.8