There are two types of data types in Java: primitive data types and reference data types. Primitive data types include int
, char
, boolean
, etc while reference data types include objects
and arrays
. However, it is also possible to create our own data types in Java through the use of user defined data types.
There are two main types of user defined data types in Java:
- Classes and
- Interfaces.
A class is a template that can be used to create objects, while an interface is a set of related methods with empty bodies.
User defined data type example
Following is an example of creating a user defined data type in Java:
// Define a class called Person
public class Person {
// Instance variables (data)
String name;
int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Methods
public void introduce() {
System.out.println("Hi, my name is " + name + " and I'm " + age + " years old.");
}
}
In the example above, we define a class called Person
. The Person
class has two instance variables: name
of type String
and age
of type int
. We also define a constructor that takes the name
and age
as parameters and assigns them to the respective instance variables.
The Person
class also has a method called introduce()
, which prints out a simple introduction message using the name
and age
instance variables.
To use this user-defined data type, you can create objects of the Person
class:
// Create a Person object
Person person1 = new Person("Virat Kohli", 31);
// Access the instance variables
System.out.println(person1.name); // Output: Virat Kohli
System.out.println(person1.age); // Output: 31
// Call the introduce() method
person1.introduce(); // Output: Hi, my name is Virat Kohli and I'm 31 years old.
In the example above, we create a Person
object named person1
using the new
keyword and passing the constructor arguments. We can then access the instance variables (name
and age
) and call the introduce()
method on the person1
object.
By defining your own classes, you can create complex data structures, encapsulate data and behavior together, and create reusable code components in your Java programs.
Frequently Asked Questions
What is a User Defined Data Type (UDT) in Java?
A UDT in Java is a custom data type created by the programmer to represent a specific structure or object that is not available as a built-in type.
How can I create a User Defined Data Type in Java?
You can create a User Defined Data Type by defining a class or an interface, specifying its attributes (fields), and defining methods to operate on those attributes.
What are the advantages of using User Defined Data Types in Java?
User Defined Data Types promote code organization, reusability, and encapsulation, allowing you to model complex real-world entities efficiently.
Can I use User Defined Data Types as part of other data structures or classes?
Yes, User Defined Data Types can be used as fields within other classes or as elements in data structures like arrays, lists, or maps.
What is the difference between a User Defined Data Types and a built-in data type in Java?
User Defined Data Types are user-defined and can represent custom structures, while built-in data types are predefined by Java and include basic types like int, float, and char.