Friday 24 May 2013

jstl if/else set forEach if

Leave a Comment
<c:choose>
    <c:when test="${role eq 2}">
       <c:forEach items="${authorityList}" var="value">
         <c:if test="${value == 2}">
            <c:set var="found" value="true"/>
         </c:if>
       </c:forEach>
       <c:if test="${found == 'true'}">
     
       </c:if>
     
    </c:when>
    <c:otherwise>
       
    </c:otherwise>
</c:choose>
Read More...

Friday 3 May 2013

Find Second Largest Number In Array Without Sorting

Leave a Comment
import java.util.Scanner;

class SecondLargest {
        public static void main(String[] args) {
        	
            int secondlargest = Integer.MIN_VALUE;
            int largest = Integer.MIN_VALUE;
            
            Scanner input = new Scanner(System.in);
            System.out.println("Enter array values: ");
            int arr[] = new int[5];
            for (int i = 0; i < arr.length; i++) 
            {
                arr[i] = input.nextInt();
                
                if (largest < arr[i]) 
                {
                    secondlargest = largest;
                    largest = arr[i];
                }
                
                if (secondlargest < arr[i] && largest != arr[i])
                    secondlargest = arr[i];
            }
            System.out.println("Second Largest number is: " + secondlargest);
        }
    }

Read More...

Thursday 2 May 2013

ParseInt vs valueOf in Java

Leave a Comment


Both valueOf and parseInt methods are used to convert String to Integer in Java, but there are subtle difference between them. If you look at code of valueOf() method, you will find that internally it calls parseInt() method to convert Integer to String, but it also maintains a pool of Integers from -128 to 127 and if requested integer is in pool, it returns object from pool. Which means two integer objects returned using valueOf() method can be same by equality operator. This caching of Immutable object, does help in reducing garbage and help garbage collector. Another difference between parseInt() and valueOf() method is there return type. valueOf() of java.lang.Integer returns an Integer object, while parseInt() method returns an int primitive. Though, after introducing Autoboxing in Java 1.5, this doesn't count as a difference, but it's worth knowing.




If you look code of parseInt() and valueOf() method from java.lang.Integer class, you will find that actual job of converting String to integer is done by parseInt() method, valueOf() just provide caching of frequently used Integer objects, Here is code snippet from valueOf() method which makes things clear:

public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
}

This method first calls parseInt() method, in order to convert String to primitive int, and then creates Integer object from that value. You can see it internally maintains an Integer cache. If primitive int is within range of cache, it returns Integer object from pool, otherwise it create a new object.

public static Integer valueOf(int i) {
        if(i >= -128 && i <= IntegerCache.high)
            return IntegerCache.cache[i + 128];
        else
            return new Integer(i);
}

There is always confusion, whether to use parseInt() or valueOf() for converting String to primitive int and java.lang.Integer, I would suggest use parseInt() if you need primitive int and use valueOf() if you need java.lang.Integer objects. Since immutable objects are safe to be pooled and reusing them only reduces load on garbage collector, it's better to use valueOf() if you need Integer object.

Read More...