New String or get parameter from http request every time?

Max CodeSmith :

is it better to instantiate a string with the value of an http request parameter or to read the parameter from the request every time in nested if conditions?

For example :

if( request.getParameter( "pageMove" ) != null ){
    if( request.getParameter( "pageMove" ).equals( "N" )){
    ;;
    }
}

vs.

String pageMove = request.getParameter( "pageMove" );
if( pageMove ) != null) {
    if( pageMove ).equals( "N" ) ){
    ;;
    }
}

Which is more efficient in terms of performance and memory management?

Thanks

Jacob G. :

Why use two if-statements at all? You can re-word the inner if-statement to the following so the null-check isn't required:

if ("N".equals(request.getParameter("pageMove"))) {
    // ...
}

To answer your question, though, you're over-optimizing. The extra amount of memory in use from not storing the value in a variable is insignificant and will be eligible for garbage collection whenever it goes out of scope.

In this scenario, I'd always prefer readability over a negligible performance increase.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=92966&siteId=1