View RSS Feed

Advanced Java

Advanced Java programming.

  1. Java Exception Classes

    by , 02-18-2012 at 05:17 PM
    Java exceptions are basically java objects. An object is thrown when you throw a java exception. It is not possible to throw any object as an exception. Only those objects whose classes descend from Throwable.
    Throwable serves as the base class for all classes and it is declared in java.lang. Your program can instantiate and throw it.

    Following picture shows a part of this family.

    Name:  1.JPG
Views: 1381
Size:  19.8 KB
    Java Exception Classes

    As it ...
  2. Checked and Unchecked Exceptions

    by , 02-18-2012 at 05:14 PM
    Checked exceptions: Programmers are facilitated by checked Exceptions so that to dealings with exceptions which could be thrown. Exception class has sub classes. Checked Exception examples are:

    • Arithmatic exception

    In a method when checked exception takes place, either program caught it or it passes to caller program.

    Unchecked exceptions: They are RuntimeException. Class Errors and subclasses of it are also unchecked.

    Example: ArrayIndexOutOfBounds ...
  3. Java Exceptions

    by , 02-18-2012 at 05:12 PM
    Any unexpected, abnormal & extra-ordinary condition which occurs in Java program at runtime is known as Exception. Examples of events or conditions are as following:

    • Not able to get the “connection exception”
    • “File not found” exception

    In all these conditions, throw an exception object. Such exceptions are considered to be the Java objects. A java error exception can’t be escaped.

    Error conditions are being handled by the Java Exception ...
  4. Java Collection Subtypes

    by , 02-18-2012 at 05:10 PM
    Collection interface subtypes are given below. They all makes the collection interface extended.

    1. List
    2. Queue
    3. SortedSet
    4. Set
    5. NavigableSet
    6. Deque

    One of its subtypes shall be used when Collection interface are needed to be used. At Collection interface, implementation is not supported by Java. Just a set of methods are present in it. Specific Collection types are ignored by it.

    To operate at a Collection, ...
  5. Checking if a Collection Contains a Certain Element

    by , 02-18-2012 at 05:08 PM
    In collection interface, 2 methods have been implemented to check if certain elements are present in it. Two methods are as following:

    1. contains()
    2. containsAll()

    This code explains these 2 methods:
    Java Code: This is the code to explain the contains and containsAll methods
    Collection collection   = new HashSet();
    boolean containsElement = collection.contains("an element");
    Collection elements     = new HashSet();
    boolean containsAll     = collection.containsAll(elements);
  6. Iterating a Collection

    by , 02-18-2012 at 05:07 PM
    Collection’s all elements are iteration is done by getting an iterator from collection. Use the iterator through collection.

    It is explained by this given code:
    Java Code: This is the code to explain the collection iteration
    Collection myCollection = new HashSet();
    //... add elements to the collection
    Iterator iterator = myCollection.iterator();
        Object object =;
        //do something to object;    
    //You can also use the
  7. Using Collections.sort and Comparator in Java

    by , 02-18-2012 at 05:05 PM
    Sorting a collection in Java is easy, just use Collections.sort(Collection) to sort your values. For example:
    Collection sorting is easier in Java. To sort the values, use Collection.sort (Collection). E.g

    Java Code: This is the code to explain collection sort and comparator methods
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    public class SimpleSort {
        public static void main(String[] args) {
            List list = new ArrayList();
  8. The Collection Classes

    by , 02-18-2012 at 05:03 PM
    Standard collection classes are provided by the Java which does the implementation of the Collection interfaces. Few classes give full implementations that are being used as the abstract class, skeletal implementations are provided which are the starting points to make the concrete collections.

    Here is the list given of standard collection classes.

    • AbstractCollection: Collection interfaces are implemented by it.
    • AbstractList: Extension of AbstractCollection is
  9. Java Interfaces

    by , 02-18-2012 at 05:02 PM
    Core collection interfaces are shown in this figure, which are basically encapsulated by various collections. Such interfaces are used for manipulation of maintenance of the interfaces, independent of details. Core collection interfaces are taken as the basis of Java collection Framework.

    Core collection interfaces are detailed by this list:

    • Collection —a collection presents objects group which are known as its elements.
    • Set — a collection in which duplicate elements
  10. Benefits of the Java Collections Framework

    by , 02-18-2012 at 05:01 PM
    Java Collections Framework offers these benefits:

    • Reducing the efforts of Programming: Useful data structures and algorithms are present in Collection Framework. Built in data structures time and efforts of a programmer gets reduced. It also allows you interoperability to the other APIs. There is no need of conversion codes or writing of the adapter objects.
    • Increase speed and quality of program: Higher quality algorithms, high performance and data structures are provided by
  11. Collections Framework

    by , 02-18-2012 at 04:59 PM
    Unified architecture to manipulate and present the collections is known as collections framework. Following is present in all collections framework:

    • Interfaces: Interfaces present the collections and are considered to be an abstract data type. Connections are allowed by interfaces to be manipulated independent of their presentation details. A hierarchy is formed by interface in the object oriented language.
    • Implementations: They are collection interface’s concrete implementation.
  12. Java Generic's Wildcards

    by , 02-18-2012 at 04:57 PM
    Consider a situation where you desire all List elements to be printed. This could be explained by the given code:
    Java Code: This is the code to explain printing of all elements
    public void processElements(List<Object> elements){
       for(Object o : elements){
    This method couldn’t be used along with List<String> instance. To sort out this problem, wildcard operator is used. For writing generic List by the help of wildcard operator, use this code.
  13. Java Generic Methods

    by , 02-18-2012 at 04:55 PM
    It is also possible and supported to generify methods in Java.

    Java Code: This is the code to expalin generic methods
    public static <T> T add(T element, Collection<T> collection){
        return element;
    A type T is being specified by this method. It is used as generic type of collection as well as for element parameter type.

    Java Code:
    String stringElement = "stringElement";
    List<String> stringList = new ArrayList<String>();
  14. Generic Classes in Java

    by , 02-18-2012 at 04:52 PM
    Your Java classes could also be generified. Generics are not restricted or bound to any pre-defined classes of Java. This is also applicable to customized written classes of Java.

    Simple generic class is explained here by this code.
    Java Code: This is the code to explain generic class
    public class GenericTestFactory<E> {
        Class myClass = null;
        public GenericTestFactory(Class myClass) {
            this.myClass = myClass;
        public E createInstance()
  15. Generic Map in Java

    by , 02-18-2012 at 04:49 PM
    It is possible to generify java.util.Map. Instances of Map in such cases could be of a specific type. Instances of that type in that case could be only inserted as well read, from that Map.
    Java Code: This is the code to explain Generic Map
    Map<Integer, String> set = new HashMap<Integer, String>;
    This Map just can now accept string instances to be as values and Integer instances to be as Keys.

    Accessing a Generic Map

    Following code could be used for addition of String to generic ...
    Tags: generic map Add / Edit Tags
  16. Generic Set in Java

    by , 02-18-2012 at 04:45 PM
    It is also possible to generify the java.util.Set. Set instances in these cases might of a specific type.

    Instances of that type in such cases could be inserted or read, from that set.

    Usage of generic Set is explained here.
    Java Code: This is the code to explain usage of generic Set
    Set<String> set = new HashSet<String>;
    Adding Elements to a Generic Set

    To a set, specific String type is added by using this code.
    Java Code: This is the code to add specific String type in a Set
    Set<String> set = new HashSet<String>;
    Tags: generic set Add / Edit Tags
  17. Generic List in Java

    by , 02-18-2012 at 04:43 PM
    It is possible to do the generification of java.util.List. This shows that List instances might be of a specific type. That type of instances in such cases could only be read from List.

    Generic List is explained by this code.
    Java Code: This is the code to expalin Generic List declaration
    List<String> list = new ArrayList<String>;
    String instances can be stored by this list.

    Exists are being checked just at the compilation time by this generic type.

    Accessing a Generic ...
    Tags: generic list Add / Edit Tags
  18. Java's Generic For Loop

    by , 02-18-2012 at 04:40 PM
    Generic consists of “new for loop”, that is known as “for each loop”. New for loop iterates the generic collections and also supports to integrate. Iteration over a List or Generic set is explained by this code:
    Generic List gets iterated by this code:

    Java Code: This is the code to explain the Generic For Loop
    List<String> list = new ArrayList<String>;
    for(String aString : list) {
    For loop parenthesis, declaration of a String variable is done. ...
  19. Generic Methods and Constructors

    by , 02-18-2012 at 04:37 PM
    Within methods declaration of the type parameters could be done and constructor signatures for creation of the generic constructors or generic methods.
    Java Code: This is the code to explain Generic Methods
    public class Box<T> {
        private T t;          
        public void add(T t) {
            this.t = t;
        public T get() {
            return t;
        public <U> void inspect(U u){
            System.out.println("T: " +
  20. Different Java Generic Types

    by , 02-18-2012 at 04:35 PM
    By using Generics, implementation of Box class is done. It is shown in the given code

    Java Code:
    public class Box<T> {
        // T stands for "Type"
        private T t;
        public void add(T t) {
            this.t = t;
        public T get() {
            return t;
    In given Box class replacement takes place with T, of all occurrences objects.

    For referencing ...
  21. What is Java Generics?

    by , 02-18-2012 at 04:32 PM
    Java provides support to the Generic features of JV. For specification of the concrete types to certain general methods or classes, Generics is used.

    Above given definition is an abstract definition. For better understanding, consider few examples.
    Object instances are stored by the help of List interface. Therefore one can do storage and also place objects in a List. E.g.

    Java Code:
    List list = new ArrayList();
    list.add(new Integer(2));
  22. Getting Java Heap Size

    by , 02-18-2012 at 04:29 PM
    In JVM, heap size is considered as a main area. New objects are stored or created here. To get heap size, see this Java code. We will also be considering various memory methods.

    Java Code: This is the code to get the heap size in Java
    // Get current size of heap in bytes
    long heapSize = Runtime.getRuntime().totalMemory();
    // Get maximum size of heap in bytes. The heap cannot grow beyond this size.
    // Any attempt will result in an OutOfMemoryException.
    long heapMaxSize = Runtime.getRuntime().maxMemory();
  23. Understanding Java Memory Settings

    by , 02-18-2012 at 04:28 PM
    -Xmx & -Xms parameters set memory. Here discussion would be done regarding such parameters that why they are used to set memory.

    JVM i.e. Java Virtual Machine is produced in system memory, when Java starts. Execution as well as complete process of Java program occurs in JVM. Java applications have 64MB memory, by default.

    In JVM, Xms is minimum heap size. If high sized initial memory is set, this might help a lot. Like,

    • GC would be made to work
    Tags: java memory Add / Edit Tags
  24. Java Garbage Collection

    by , 02-18-2012 at 04:26 PM
    Java provides support for its memory management by using its built-in mechanism. In Garbage collector, two threads run.

    • Responsibility of 1st thread is memory collections, primarily heap. It is quite light weighted.
    • 2nd thread is considered to be a full “GC thread”. When comes memory shortage, entire heap is
    traversed by it. Spaces for the objects are allocated by it.

    When there is inadequate heap allocation or memory leak then full GC thread runs ...
  25. How to manage Java Heap Size?

    by , 02-18-2012 at 04:25 PM
    You can specify the memory allocation for JVM using –X option. Following table explains the JVM options.

    -Xms: initial java heap size
    -Xmx: maximum java heap size
    -Xmn: the size of the heap for the young generation

    unix> bin/ -Xmn100M -Xms500M -Xmx500M
    win> bin/httpd.exe -Xmn100M -Xms500M -Xmx500M
    install win service> bin/httpd.exe -Xmn100M -Xms500M -Xmx500M -install

    To set max –Xmx and min –Xms heap sizes ...
  26. Java memory management

    by , 02-18-2012 at 04:22 PM
    There are 2 categories of Java memory management problems which are as following:

    1. In-complete deallocation
    2. Pre-mature deallocation

    Incomplete deallocation has been divided into 2 subclasses which are:

    1. Desing bugs
    2. Coding bugs

    Coding bugs are considered to be dependent upon language whereas design bugs are not language dependent. They are programming mistakes. Usually programmer is involved in handling the memory ...
  27. High Load Web Servlets Performance Tips

    by , 02-18-2012 at 04:19 PM
    • Do not redirect request from the servlet. Always send requests directly from static resources to the web server using URL.
    • Use different web servers for dynamic and static content.
    • Use cache to store content.
    • Manage your RAM to cache content.
    • Use multiple JVMs to balance the load.
    • Limit the number of file descriptors.
    • Do not avoid logging to improve performance. It is more important.
    • Always monitor resources.
  28. JMS Performance Tips

    by , 02-18-2012 at 04:17 PM
    • To avoid message queue, start consumer before producer.
    • To process messages, use ConnectionConsumer with ServerSessionPool.
    • Close all resources explicitly like connections, sessions, objects etc.
    • Non-transactional as well as transactional sessions should be used for non-transactional & transactional messages.
    • Destination parameters shall be tuned.
    • For messages to be received asynchronously, MessageListener shall be implemented.
    • Select NON_PERSISTENT ...
  29. EJB Performance Tips

    by , 02-18-2012 at 04:16 PM
    • Tune EJB calls. They are expensive.
    • Use Plain Java Object (POJO), when you do not need an EJB.
    • If EJB client and EJB are deployed on same JVM, then use Local Interface.
    • Multiple entity beans must be wrapped in one session bean to reduce remote calls.
    • Use transient with unnecessary data variables to avoid network overhead.
    • Always cache to EJBHome references so that do not need to lookup JNDI each time.
    • Set transaction time out.
    • Use ...
  30. Servlet Performance Tips

    by , 02-18-2012 at 04:14 PM
    • Cache static data using jspInit() method.
    • Release static data in jspDestroy() method.
    • To concatenate string use, StringBuffer.
    • Do not use println() method.
    • Do not use PrintWriter to send binary data. Use ServletOutputStream.
    • Always flush data in sections.
    • Use getLastModified() method to handle browser and server cache.
    • Use application server cache.
    • Use session in following order: HttpSession, Hidden fields, Cookies, URL rewriting. ...
Page 1 of 2 12 LastLast