$show=/label

Java 8 Integer Class new methods

SHARE:

A quick guide to new integer class methods in java 8 api

1. Overview

In this tutorial, We'll learn what are the new methods added to the Integer class in Java 8.

Below are the new methods of integer class and look at each example to understand its usage.

All methods are static and access directly with class name. All unsigned methods work for positive and negetive integer values.

Java 8 Integer Class new methods


public static String toUnsignedString(int i, int radix)
public static String toUnsignedString(int i)
public static int parseUnsignedInt(String s, int radix) throws NumberFormatException
public static int parseUnsignedInt(String s) throws NumberFormatException
public static int hashCode(int value)
public static int compareUnsigned(int x, int y)
public static long toUnsignedLong(int x)
public static int divideUnsigned(int dividend, int divisor)
public static int remainderUnsigned(int dividend, int divisor)
public static int sum(int a, int b)
public static int max(int a, int b)
public static int min(int a, int b)

2. Java 8 Integer toUnsignedString()


String toUnsignedString(int i): Returns String for the given unsigned decimal value.

String toUnsignedString(int i, int radix): Returns string for the given integer with the given radix value from the arguments.

Example 1:
package com.javaprogramto.java8.integer;

public class IntegerToUnsignedStringExample {

	public static void main(String[] args) {

		String unsignedString1 = Integer.toUnsignedString(100);
		System.out.println("Unsigned decimal string for 100 is "+unsignedString1);
		
		String unsignedString2 = Integer.toUnsignedString(-100);
		System.out.println("Unsigned decimal string for -100 is "+unsignedString2);

		String unsignedString3 = Integer.toUnsignedString(-10000000);
		System.out.println("Unsigned decimal string for -10000000 is "+unsignedString3);
		
		String unsignedString4 = Integer.toUnsignedString(8, 2);
		System.out.println("Unsigned binary string for 8 is "+unsignedString4);
		
		String unsignedString5 = Integer.toUnsignedString(15, 2);
		System.out.println("Unsigned binary string for 15 is "+unsignedString5);
		
		String unsignedString6 = Integer.toUnsignedString(32, 16);
		System.out.println("Unsigned octa string for 32 is "+unsignedString6);		
		
	}
}

Output:
Unsigned decimal string for 100 is 100
Unsigned decimal string for -100 is 4294967196
Unsigned decimal string for -10000000 is 4284967296
Unsigned binary string for 8 is 1000
Unsigned binary string for 15 is 1111
Unsigned octa string for 32 is 20


3. Java 8 Integer parseUnsignedInt()


parseUnsignedInt(String s) throws NumberFormatException

Retruns an integer value in decimal form for the given string.

parseUnsignedInt(String s, int radix) throws NumberFormatException

Retruns an integer value in decimal form for the given string with given radix value.

Example 2:

This method works similar to the parseInt() method but parseInt() return singed int value.
package com.javaprogramto.java8.integer;

public class IntegerParseUnsignedIntExample {

	public static void main(String[] args) {

		Integer unsignedInteger1 = Integer.parseUnsignedInt("100");
		System.out.println("Unsigned decimal integer for 100 is " + unsignedInteger1);

		Integer unsignedInteger2 = Integer.parseUnsignedInt("4294967196");
		System.out.println("Unsigned decimal integer for 4294967196 is " + unsignedInteger2);

		Integer unsignedString4 = Integer.parseUnsignedInt("10", 2);
		System.out.println("Unsigned binary integer for 8 is " + unsignedString4);

		Integer unsignedString5 = Integer.parseUnsignedInt("111", 2);
		System.out.println("Unsigned binary integer for 15 is " + unsignedString5);

		Integer unsignedString6 = Integer.parseUnsignedInt("20", 16);
		System.out.println("Unsigned octa integer for 32 is " + unsignedString6);

	}
}

Output:
Unsigned decimal integer for 100 is 100
Unsigned decimal integer for 4294967196 is -100
Unsigned binary integer for 8 is 2
Unsigned binary integer for 15 is 7
Unsigned octa integer for 32 is 32

4. Java 8 Integer hashCode()


int hashCode(int value) This method returns hashcode for the given intger value.

Internal code returns the same integer value as hashcode.

Example 3:
package com.javaprogramto.java8.integer;

public class IntegerHashcodeExample {

	public static void main(String[] args) {

		Integer unsignedInteger1 = Integer.hashCode(100);
		System.out.println("hashcode integer for 100 is " + unsignedInteger1);

		Integer unsignedInteger2 = Integer.hashCode(200);
		System.out.println("hashcode integer for 4294967196 is " + unsignedInteger2);
	}
}
Output:
Unsigned decimal integer for 100 is 100
Unsigned decimal integer for 4294967196 is 200

5. Java 8 Integer compareUnsigned()


int compareUnsigned(int x, int y)

Compares two int values numerically treating the values as unsigned.
Returns value 0 if x == y; -1 if x < y as unsigned values; and +1 if x > y as unsigned values

Example 4:
package com.javaprogramto.java8.integer;

public class IntegerCompareUnsignedExample {

	public static void main(String[] args) {

		Integer integer1 = Integer.compareUnsigned(10, 20);
		System.out.println("Result of comparing two unsigned values (10, 20) : " + integer1);

		Integer integer2 = Integer.compareUnsigned(20, 10);
		System.out.println("Result of comparing two unsigned values (20, 10) : " + integer2);
		
		Integer integer3 = Integer.compareUnsigned(10, 10);
		System.out.println("Result of comparing two unsigned values (10, 10) : " + integer3);
	}
}

Output:
Result of comparing two unsigned values (10, 20) : -1
Result of comparing two unsigned values (20, 10) : 1
Result of comparing two unsigned values (10, 10) : 0

6. Java 8 Integer toUnsignedLong()


long toUnsignedLong(int x) returns an unsigned long value for the given unsigned int value.

Converts the argument to a long by an unsigned conversion. In an unsigned conversion to a long, the high-order 32 bits of the long are zero and the low-order 32 bits are equal to the bits of the integer argument. Consequently, zero and positive int values are mapped to a numerically equal long value and negative int values are mapped to a long value equal to the input plus 2^32.

Example 5:
package com.javaprogramto.java8.integer;

public class IntegerToUnsignedLongExample {

	public static void main(String[] args) {

		Long long1 = Integer.toUnsignedLong(10);
		System.out.println("unsigned long value for int 10 is " + long1);

		Long long2 = Integer.toUnsignedLong(4294967);
		System.out.println("unsigned long value for int 4294967 is " + long2);
		
		Long long3 = Integer.toUnsignedLong(100000);
		System.out.println("unsigned long value for int 10 is " + long3);
	}
}

Output:
unsigned long value for int 10 is 10
unsigned long value for int 4294967 is 4294967
unsigned long value for int 10 is 100000

7. Java 8 Integer divideUnsigned() & remainderUnsigned()


int divideUnsigned(int dividend, int divisor)

Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.
Note that in two's complement arithmetic, the three other basic arithmetic operations of add, subtract, and multiply are bit-wise identical if the two operands are regarded as both being signed or both being unsigned. Therefore separate addUnsigned, etc. methods are not provided.

int remainderUnsigned(int dividend, int divisor)

Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.


Example 6:
package com.javaprogramto.java8.integer;

public class IntegerReminderDivideUnsignedExample {

	public static void main(String[] args) {

		//divideUnsigned examples
		Integer integer1 = Integer.divideUnsigned(10, 20);
		System.out.println("Result of divide two unsigned values (10, 20) : " + integer1);

		Integer integer2 = Integer.divideUnsigned(20, 10);
		System.out.println("Result of divide two unsigned values (20, 10) : " + integer2);

		Integer integer3 = Integer.divideUnsigned(10, 10);
		System.out.println("Result of divide two unsigned values (10, 10) : " + integer3);
		
		//remainderUnsigned examples
		Integer reminder1 = Integer.remainderUnsigned(10, 20);
		System.out.println("Result of remainder two unsigned values (10, 20) : " + reminder1);

		Integer reminder2 = Integer.remainderUnsigned(20, 10);
		System.out.println("Result of remainder two unsigned values (20, 10) : " + reminder2);

		Integer reminder3 = Integer.remainderUnsigned(10, 10);
		System.out.println("Result of remainder two unsigned values (10, 10) : " + reminder3);
	}
}

Output:
Result of divide two unsigned values (10, 20) : 0
Result of divide two unsigned values (20, 10) : 2
Result of divide two unsigned values (10, 10) : 1
Result of remainder two unsigned values (10, 20) : 10
Result of remainder two unsigned values (20, 10) : 0
Result of remainder two unsigned values (10, 10) : 0

8. Java 8 Integer sum() 


int sum(int a, int b) Adds two integers together as per the + operator and retuns result of sum as integer.

Example 7:
package com.javaprogramto.java8.integer;

public class IntegerSumExample {

	public static void main(String[] args) {

		//divideUnsigned examples
		Integer integer1 = Integer.sum(10, 20);
		System.out.println("Result of sum two int values (10, 20) : " + integer1);

		Integer integer2 = Integer.sum(-20, 10);
		System.out.println("Result of sum two int values (-20, 10) : " + integer2);

		Integer integer3 = Integer.sum(-10,- 10);
		System.out.println("Result of sum two int values (-10, -10) : " + integer3);
		
	}
}

Output:
Result of sum two int values (10, 20) : 30
Result of sum two int values (-20, 10) : -10
Result of sum two int values (-10, -10) : -20
The sum method can be used with the streams.

9. Java 8 Integer max() 


int max(int a, int b)
Returns the greater of two int values as if by calling Math.max.

Example 8:
package com.javaprogramto.java8.integer;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class IntegerMaxExample {

	public static void main(String[] args) {

		// divideUnsigned examples
		Integer integer1 = Integer.max(10, 20);
		System.out.println("Result of max two int values (10, 20) : " + integer1);

		Integer integer2 = Integer.max(-20, 10);
		System.out.println("Result of max two int values (-20, 10) : " + integer2);

		Integer integer3 = Integer.max(-10, -10);
		System.out.println("Result of max two int values (-10, -10) : " + integer3);

	}
}

Output
Result of max two int values (10, 20) : 20
Result of max two int values (-20, 10) : 10
Result of max two int values (-10, -10) : -10

10. Java 8 Integer min() 


int min(int a, int b)
Returns the smaller of two int values as if by calling Math.min.


Example 9
package com.javaprogramto.java8.integer;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class IntegerMinExample {

	public static void main(String[] args) {

		// divideUnsigned examples
		Integer integer1 = Integer.min(10, 20);
		System.out.println("Result of min two int values (10, 20) : " + integer1);

		Integer integer2 = Integer.min(-20, 10);
		System.out.println("Result of min two int values (-20, 10) : " + integer2);

		Integer integer3 = Integer.min(-10, -10);
		System.out.println("Result of min two int values (-10, -10) : " + integer3);

	}
}
Output
Result of min two int values (10, 20) : 10
Result of min two int values (-20, 10) : -20
Result of min two int values (-10, -10) : -10


11. Java 8 Integer sum(), max(), min() with Stream API


All 3 methods of integer class max(), min() and max() methods can be used along with the stream api using reduce() method.

Example 10
package com.javaprogramto.java8.integer;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class IntegerMinMaxSumStreamsExample {

	public static void main(String[] args) {
		// Integer max with stream api
		List<Integer> maxIntList = Arrays.asList(40, 50, 10, 20, 30);

		Optional<Integer> maxOptional = maxIntList.stream().reduce(Integer::max);

		if (maxOptional.isPresent()) {
			System.out.println("max is " + maxOptional.get());
		}
		
		// Integer sum with stream api
		List<Integer> sumIntList = Arrays.asList(10, 20, 30, 40, 50);

		Optional<Integer> sumOptional = sumIntList.stream().reduce(Integer::sum);

		if (sumOptional.isPresent()) {
			System.out.println("sum is " + sumOptional.get());
		}

		// Integer min with stream api
		List<Integer> minIntList = Arrays.asList(40, 50, 10, 20, 30);

		Optional<Integer> minOptional = minIntList.stream().reduce(Integer::min);

		if (minOptional.isPresent()) {
			System.out.println("min is " + minOptional.get());
		}
	}
}

Output:
max is 50
sum is 150
min is 10


12. Conclusion


In this article, we've seen the examples on all new methods of java 8 integer class.




COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,21,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,32,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,135,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,7,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,127,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,113,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,29,String,61,String Programs,28,String Revese,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,24,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java 8 Integer Class new methods
Java 8 Integer Class new methods
A quick guide to new integer class methods in java 8 api
https://1.bp.blogspot.com/-yDvZ45EpigM/YZ6DyHlU0cI/AAAAAAAADi0/QGnpXp9dsB8Zt1xfngQ7hMNJoniqCmF_ACLcBGAsYHQ/w640-h450/Java%2B8%2BInteger%2BClass%2Bnew%2Bmethods.png
https://1.bp.blogspot.com/-yDvZ45EpigM/YZ6DyHlU0cI/AAAAAAAADi0/QGnpXp9dsB8Zt1xfngQ7hMNJoniqCmF_ACLcBGAsYHQ/s72-w640-c-h450/Java%2B8%2BInteger%2BClass%2Bnew%2Bmethods.png
JavaProgramTo.com
https://www.javaprogramto.com/2021/11/java8-integer.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2021/11/java8-integer.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content