Is there any equivalent to typedef of C/C++ in Java?


We can find many similarities between Java and C/C++ programming languages in terms of syntaxes and features. But, there are several functionalities that have been omitted from Java like 'typedef'. If someone coming from a C/C++ background, must have heard the 'typedef' keyword, and often wonders, is there any equivalent to typedef in Java? In simple words, Java does not provide a direct equivalent to typedef. The creators of Java replaced this feature with classes. In fact, a class can do even more than a typedef can do.

Replacement to typedef of C/C++ in Java?

Before exploring the answer to the given question, let's discuss what is typedef in C/C++ and how we can use it in our programs.

typedef in C/C++

In C/C++, 'typedef' stands for type definition, which is a way of defining a custom name to a pre-defined datatype. This can make our code more readable and expressive, especially when dealing with complex types such as pointers or structs.

Syntax

typedef nameOfdatatype newNameofDatatype;

Instance

typedef float new_float;

Example 

The following example illustrates how we can use the 'typedef' in C++ programs.

#include <iostream>
using namespace std;
int main() {
   cout << "Example of typedef in C++!!" << endl; 
   typedef float new_float; // using typedef keyword 
   new_float marksPer = 80.08; // initializing typedef datatype
   // printing the result
   cout << "Percentage: " << marksPer << endl; 
   return 0;
}

Output

Example of typedef in C++!!
Percentage: 80.08

Substitute for typedef in Java

As mentioned earlier, Java does not have any direct methods or ways similar to typedef of C/C++. However, there is a possible way to achieve its functionality by using classes and objects of Java.

Class and Object

Classes and Objects exist at the core of Java programming language. The fundamental use of a class is to define a new data type that contains user-defined variables and methods. Once defined, this new data type can be used to create objects of that type. An object can be defined as an instance of class. When a class is created it does not occupy any memory, only the object of that class occupies memory. The one benefit of using class over typedef is that class provides the freedom to change the representation over time.

From the above discussion, we can clearly conclude that classes and objects can do everything that a 'typedef' is capable of. Perhaps, we are doing an injustice to classes and objects by comparing them with typedef because they offer even more functionality than typedef.

Syntax of Class

class nameOfClass {
   // your code here
}

Syntax of Object

nameOfclass nameOfinstance = new nameOfclass(); 

Example 

The following example illustrates how we can use a class and object in Java programs.

public class Class1 { // defining a class
   // member variable
   double marks = 78.3;
   // member method
   void shw() {
      System.out.println("Given Marks: " + marks);
   } 
   public static void main(String []args) {
      System.out.println("Example of class and object");
      // creating object of the class
      Class1 obj = new Class1();
      // calling the method using object
      obj.shw();
   }
}

Output

Example of class and object
Given Marks: 78.3

Conclusion

In this article, we first understood the basics of 'typedef', which is used to assign a new name to the pre-defined datatype. Then, we tried to find possible ways to perform similar tasks in Java. There is no direct equivalent to typedef of C/C++ in Java, but we can use a class as its alternative because it offers a plethora of functionalities including what typedef offers.

Updated on: 17-Aug-2023

232 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements