Core Java

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
Java Struct - Installation step 1
Installation step 1
  • Select JUnion plugin and select next.
Java Struct - Installation step 2
Installation step 2
  • Accept the license agreement and continue with the installation
Installation step 3
  • Please wait till the installation complete.
  • Select the “Restart Now” option in software update dialog.
Installation step 4
  • 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.
Installation step 5
  • Goto Window -> Preferences -> Java -> Compiler -> Error/Warnings -> Deprecated or Restricted API Set Forbidden Reference to Warning.
Installation step 6
  • 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

6. Download the Source Code

This was an example of how to use Structs in java using Project JUnion.

Download
You can download the full source code of this example here: Java Struct Example

Last updated on Aug. 03rd, 2021

Kavitha Ashok

Kavitha has a Master's degree in Information Technology and a Master's degree in Business Administration. She started her career as a Junior Software Developer trainee and moved up as a Technical lead. She has been involved with projects in various domains such as CMS, IMS, android and digital TV products, applications, web services, etc. , She is mainly involved in development, design, analysis, refactoring, etc., based on Java and other technologies.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button