Java Struct Example
This article focuses on Structs in Java and not Structs framework. Structs are similar to the class that holds different types of data and it is a value type. It is used to create lightweight objects and also when data is not modified after creation. Although it is useful, it is not present in Java. Many of us have confusion about whether Java supports Structs or not? Yes, Java doesn’t have a struct/value type yet. But you have good news as well. Project JUnion delivers struct types for Java programming language. So you can use Struct types in java by using Project JUnion plugin by annotating a class with @Struct annotation.
2. Installation
You can find many options to use this Project JUnion, such as source translator, compiler plugin, eclipse plugin, Netbeans plugin, ant or maven build. A convenient option would be to install it in eclipse as a plugin or add it as the maven dependency. Here I am going to walk you through the installation.
- Please wait till the installation complete.
- Select the “Restart Now” option in software update dialog.
- Add external jar and pick junion.jar
- Download junion.jar and junionc.jar from https://tehleo.github.io/junion/download.html or from other download sites.
- Goto Window -> Preferences -> Java -> Compiler -> Error/Warnings -> Deprecated or Restricted API Set Forbidden Reference to Warning.
- Create a new Java project and create a new file named .junion
- In .junion property file, set property compileLibs= path to junionc.jar. Save the file
- Now you will see “.generated_src_junion” folder generated automatically
- Add the above folder in the build path. Goto properties -> Java Build Path -> Sources -> Add Folder and add .generated_src_junion
- Now you are ready to use @Struct in your java project s installation is successful.
3. How to use Struct in java
As like in c++, you can also use Struct in java. Code Example1
package com.kavi.geek; import theleo.jstruct.Struct; public class StructExample < @Struct public static class EmpName < public String firstName,middleName,lastName; >public static void main(String[] args) < EmpName[] empArray = new EmpName[2]; empArray[0].firstName="Java"; empArray[0].middleName="Code"; empArray[0].lastName="Geek"; System.out.println("Name : "+empArray[0].firstName +" "+empArray[0].middleName +" "+empArray[0].lastName); >>
int[] primitiveArray = new int[500]; Integer[] intObjectArray = new Integer[500];
In the above code, we all agree primitive array consumes less memory than Integer object array. You might have a question like why you should use Structs? Why can’t you have a class instead of a struct?
- Struct types use less memory
- It performs better than the object.
- Performance of primitive array is far better
4. Conclusion
Struct types define data types, which use less memory as possible. Few features are implemented and still few are in progress. Should you like to check the implemented features, go to https://github.com/TehLeo/junion/blob/master/docs/wiki/structs.md. As this article is based on Project JUnion, the content is picked from https://tehleo.github.io/junion/features.html
5. More articles
6. Download the Source Code
This was an example of how to use Structs in java using Project JUnion.
Last updated on Aug. 03rd, 2021
Struct in Java
- Use the Classes to Simulate a Struct in Java
- Use the JUnion Plugin to Simulate a Struct in Java
In programming, the struct is a keyword for creating a structure that contains variables, methods, different types of constructors, operators, etc. It is similar to classes that hold different types of data and has a value type. It creates objects which require less memory.
However, structs are not present in Java. We can modify some objects in Java to use them as a struct.
These methods are discussed below.
Use the Classes to Simulate a Struct in Java
We can make all the methods in a class public to simulate a struct. The main difference between a struct and a class is that the struct is by default public, and the class is private. So, if we create a class and change its methods and variables to public, it will work similarly to a struct.
We implement this logic in the following example.
class EmployeeExample private String emp_name; private int emp_code; // constructor public Employee(String emp_name, int emp_code) this.emp_name = emp_name; this.emp_code = emp_code; > // getter public String getName() return emp_name; > public int getCode() return emp_code; > public static void main(String[] args) EmployeeExample[] array = new EmployeeExample[2]; // new stands for create an array object array[0] = new EmployeeExample("Ram", 1); // new stands for create an employee object array[1] = new EmployeeExample("Shyaam", 2); for(int i=0; i array.length;i++) System.out.println(array[i].getName()+" "+ array[i].getCode()); > > >
In the above example, we have created the constructors and getName() and getCode() methods as public. Then, the code takes employee name and employee code input and stores the values in emp_name and emp_code , respectively, in the class EmployeeExample .
Use the JUnion Plugin to Simulate a Struct in Java
We can also use the Project JUnion plugin. Here we get software that helps us to create struct by using @Struct annotation. We have to install the plugin from the site github.io/junion and place it in the maven dependency. Also, we have to add the jar file name junion.jar , which can be downloaded from the same website.
The @Struct annotation consists of methods that help us to create structure in Java.
The following example demonstrates the above method.
import theleo.jstruct.Struct; public class Struct @Struct public static class Emp_Name public String first_Name,middle_Name,last_Name; > public static void main(String[] args) Emp_Name[] array = new Emp_Name[2]; array[0].first_Name="Ram"; array[0].middle_Name=""; array[0].last_Name="Sharma "; System.out.println("Name : "+array[0].first_Name +" "+array[0].middle_Name +" "+array[0].last_Name); > >
Here we make a static public class, Emp_Name , which acts as a struct and consists of a few methods. Then in the main class, we created a new array of length two and then added data using the methods and store them in the array.
Copyright © 2023. All right reserved