What is Range.toString() in Java?

Overview

toString() is an instance method of the Range class, which returns the string representation of the Range object. The format of the string representation is [minValue...maxValue] where minValue signifies the starting value of the range and maxValue signifies the ending value of the range.

How to import Range

The definition of Range can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the Range class as follows:


import org.apache.commons.lang3.Range;

Syntax


public String toString()

Parameters

This method has no parameters.

Return value

This method returns the string representation of the Range object.

Code

In the below code, we define a Range object using the between method and print the string representation to the console.

import org.apache.commons.lang3.Range;
public class Main{
public static void main(String[] args) {
int fromValue = 100;
int toValue = 200;
Range<Integer> range = Range.between(fromValue, toValue);
System.out.println("Range - " + range.toString());
}
}

Output

The output of the code will be as follows:


Range - [100..200]
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources