Java Struct Example
1. Introduction
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.
- Goto Eclipse -> Help -> Install New Software
- Enter site: https://tehleo.github.io/junion/update
- Select JUnion plugin and select next.
- Accept the license agreement and continue with 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<version>.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); } }
Output
Name : Java Code Geek
Code Example2
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?
The answer is simple. Reasons are
- 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
- Java Tutorial for Beginners (with video)
- Java Map Example
- Java Queue Example (with video)
- Java Stack Example (with video)
- Java Collections Tutorial
- LinkedList Java Example (with video)
- Hashset Java Example
6. Download the Source Code
This was an example of how to use Structs in java using Project JUnion.
You can download the full source code of this example here: Java Struct Example
Last updated on Aug. 03rd, 2021