Reputation: 442
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
Upvotes: 3
Views: 387
Reputation: 29680
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.
Upvotes: 4