Core Java

Java Unsigned int Example

In this article, we will talk about the Java Unsigned int. Firstly, we will answer the question if java has unsigned int data type and then we will show you some Java Unsigned int Example on Guava library and JOOU library.

1. Introduction

There are a lot of options if you want to store a number in Java. You can use integer, float, short, long etc. Whole numbers such as 9, 125, 12.076, etc, are stored using the int variable. Java does not have a datatype for unsigned integer so you can define "long" instead of an "int" to store large values. Int data type is a 32-bit signed two’s complement integer, which has a minimum value of -231 and a maximum value of 2^31-1. In Java 8 +, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2^32-1. Furthermore, some static methods such as compareUnsigned and divideUnsigned have been added to the Integer class to help us with arithmetic operations with unsigned integers.

2. Technologies used

The example code in this article was built and run using:

  • Java 1.8.231(1.8.x will do fine)
  • Eclipse IDE for Enterprise Java Developers- Photon
  • Maven 4.0.0

3. Guava Library

Google Guava is an open-source set of common libraries for Java, which was developed by Google engineers. The purpose of its creation is to reduce coding errors. Furthermore, it provides primitives support, cashing, concurrency, it provides utility methods for collection, common annotations and string processing. As you can see below we will focus on primitive support of Guava.

3.1 Guava Library unsigned int example

In this step, I will create a Maven project to eclipse with a dependency to import Guava library.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>JCG_INT</groupId>
  <artifactId>JCG_INT</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>  
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
  <dependencies>
  <dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>28.2-jre</version>
  </dependency>
</dependencies>
</project>

You can see the dependency on the lines: 21-25.

Here you can see an example in which we will use the parseUnsignedInt() method and print the max value of an unsigned integer. parseUnsignedInt() returns the unsigned int value represented by the given decimal string.

Guava_example.java

import com.google.common.primitives.UnsignedInteger;
import com.google.common.primitives.UnsignedInts;

public class Guava_example {
public static void main(String args[]) {
	int a = UnsignedInts.parseUnsignedInt(UnsignedInteger.MAX_VALUE.toString());
    String maxInt = UnsignedInts.toString(a);
    System.out.println(maxInt); 
}
}

The output of the above code is: 42949672954294967295 or 2^32-1 which is the Max value of unsigned int.

4. JOOU Library

JOOU is a minimalistic library that we can use to solve our “unsigned ” problem in Java. It helps us with 4 primitive types: byte, short, long, int.

4.1. JOOU Library unsigned int example

Before we write the example we must add the Joou library at pom.xml of Maven. There is a difference in the dependency between those who use Java 9+ and those who use Java 6 +.

If you use Java 9+ the dependency is:

<dependency>
  <groupId>org.jooq</groupId>
  <artifactId>joou</artifactId>
  <version>0.9.3</version>
</dependency> 

If you use Java 6+ the dependency is (this dependency we will use at this example):

<dependency>
  <groupId>org.jooq</groupId>
  <artifactId>joou-java-6</artifactId>
  <version>0.9.3</version>
</dependency> 

In this simple example, you can see how to use Joou library.

Joou_example.java

import org.joou.UInteger;
import static org.joou.Unsigned.*;
public class Joou_example {
	public static void main(String args[]) {
	UInteger i = uint(3);
	System.out.println(i);
	UInteger j = uint(-8);
	System.out.println(j);
	i=i.add(j);
	System.out.println(i);
	}			
}

The output of the above code is:

  • 3
  • 4294967288
  • 4294967291

At line 7 you can notice that a negative number will be subtracted from the max unsigned value (4294967295) so that will be an unsigned value. First -8 is set as 4294967288, then we add -3.

5. Download the Source Code

Here is the code from the examples we used!

Download
You can download the full source code of these examples here: Java Unsigned int Example

Ioannis Makrygiannakis

John is an undergraduate student at the Department of Informatics of Athens University of Economics and Business. He is specialized in Databases, Knowledge Management, Information Systems and Information Security. In his free time he loves learning new things with regard to programming and network security.
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