Last Updated:

Description and example of working with the Integer class in Java

Today we will continue the Java section for beginners with articles about the class - its features and an example of work.Integer

Description of the Integer class

A class in Java refers to a package, which means that any class can freely use the methods in that class without importing any package. It implicitly inherits the class , which is a subclass of . The Number class is an abstract class and is a superclass for , , and most wrapper classes — and .

The class implements the .Integerjava.langNumberObjectBigIntegerBigDecimalDouble, Integer, Short, Byte, FloatLongNumberSerializable

Features of the Integer class

A class is a wrapper class (learn more about wrapper classes). Its main function is to wrap a primitive data type into an object. An object has only one variable of type . The class provides a number of methods that can be useful for working with the primitive .IntegerintIntegerintint

  • The most commonly used methods are to convert the Integer class to a string and vice versa. The class implements two interfaces: and .SerializableComparable<Integer>
  • Integer is also used when working with collections, such as ArrayList or List. Starting with Java 5, we can use wrapper classes, which is automatically achieved through a concept called Autoboxing.
  • The primitive int data type stores the actual binary value of an integer. The maximum amount of data that an int variable can store is 4 bytes of data, which means that the value can range from -2,147,483,648 to +2,147,483,647.

It should also be noted that An Integer is a class in Java and any variable of type Integer stores a reference to an Integer object. The Integer class in Java contains various static methods for processing its content. Because the methods are static, we don't need an Integer object to access the methods, for example: in the following examples, the parseInt() and toString() methods are called directly.

Convert integer to String

Let's look at an example of converting a primitive to a string using the class :intInteger

First, the primitive is wrapped into an Integer object and then converted to a string. The code above can be simplified, but we are interested in working with Integer, so it may seem that the example is "far-fetched".

Convert String to Integer

Let's also look at an example of converting a string to an int primitive using an intermediate Integer object:

It should be noted that when parsing the int value from the string, we can get an exception . This can happen if the argument is not an interpreted integer, that is, not a textual representation of an integer. We will get an error if we pass .

In such cases, parsing is usually wrapped in a try-catch block and returns a default value when the exception is handled.NumberFormatExceptionString intString = "35684"String intString = "35684th"


Important points when working with Integer

  • A field is a constant variable that contains the minimum value that an int (-231) can have.MIN_VALUE
  • A field is a constant that contains the maximum value of the primitive int: (231 – 1).MAX_VALUE
  • A class is an instance of a class that represents the int type.Class<Integer> TYPE
  • Variable – This variable contains the number of bits, which is an integer in binary form.SIZE
  • You can get the value from by calling the .doubleIntegerdoubleValue()
  • Objects can be easily compared by using the .Integerequals()

Subscribe to new articles and stay tuned for updates on Java development.