Home » Core Java » Java Struct Example

About Kavitha Ashok

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.

Java Struct Example

In this post, we feature a comprehensive article about structs in Java.

1. Introduction

This article focuses on Structs but 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 uses 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, content is picked from https://tehleo.github.io/junion/features.html

5. 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
(+2 rating, 4 votes)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar

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

  Subscribe  
Notify of