Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrive string value of Long.MAX_VALUE in compile time in java?

Is there any possible way to setup constant compile time value using a runtime call method? In "Spring in Action" book, I got this piece of code:

private static final String MAX_LONG_AS_STRING = Long.toString(Long.MAX_VALUE);

@RequestMapping(method = RequestMethod.GET)
public List<Spittle> spittles(
        @RequestParam(value = "max", defaultValue = MAX_LONG_AS_STRING) long max,
        @RequestParam(value = "count", defaultValue = "20") int count) {
    return spittleRepository.findSpittles(max, count);
}

the problem is with MAX_LONG_AS_STRING, because defaultValue param needs to be a String constant but MAX_LONG_AS_STRING is not a constant compile time variable, is there any possible way to get Long max value as a constant String value? Maybe there is something which can help me to call toString method during compile time, or retrieve this value in any other way ?

like image 355
krkonop Avatar asked Nov 18 '16 14:11

krkonop


People also ask

What is the value of long Min_value in Java?

From Oracle: long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).

What is the value of long MAX_VALUE?

The Long. MAX_VALUE is the static variable that contains a constant value in the Java wrapper Long class, and 9,223,372,036,854,775,807 is considered a maximum value of a long variable.

What is the largest value you can store in a long variable Java?

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.

What is long MAX_VALUE Java?

static long MAX_VALUE − This is a constant holding the maximum value a long can have, 263-1. static long MIN_VALUE − This is a constant holding the minimum value a long can have, -263. static int SIZE − This is the number of bits used to represent a long value in two's complement binary form.


1 Answers

You can achieve this as shown in the below steps:

(1) Get the Max value first long MAXVALUE = Long.MAX_VALUE;

(2) Set the @RequestParam value as defaultValue = MAXVALUE+"" (converts long to string)

Complete Code:

private static final long MAXVALUE = Long.MAX_VALUE;//Get the long value first

    @RequestMapping(method = RequestMethod.GET)
    public void spittles(
            @RequestParam(value = "max", defaultValue = MAXVALUE+"") long max,
            @RequestParam(value = "count", defaultValue = "20") int count) {
       // return spittleRepository.findSpittles(max, count);
    }
like image 103
developer Avatar answered Sep 21 '22 23:09

developer