Advanced Java programming.
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. Java Exception Classes As it ...
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 ...
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 ...
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, ...
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); ...
Collection collection = new HashSet(); boolean containsElement = collection.contains("an element"); Collection elements = new HashSet(); boolean containsAll = collection.containsAll(elements);
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(); while(iterator.hasNext()){ Object object = iterator.next(); //do something to object; } //You can also use the ...
Collection myCollection = new HashSet(); //... add elements to the collection Iterator iterator = myCollection.iterator(); while(iterator.hasNext()){ Object object = iterator.next(); //do something to object; } //You can also use the
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(); list.add(5); ...
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(); list.add(5);
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 ...
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 ...
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 ...
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. ...
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){ System.out.println(o); } } 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. ...
public void processElements(List<Object> elements){ for(Object o : elements){ System.out.println(o); } }
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){ collection.add(element); 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>(); ...
public static <T> T add(T element, Collection<T> collection){ collection.add(element); return element; }
String stringElement = "stringElement"; List<String> stringList = new ArrayList<String>();
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() throws ...
public class GenericTestFactory<E> { Class myClass = null; public GenericTestFactory(Class myClass) { this.myClass = myClass; } public E createInstance() throws
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 ...
Map<Integer, String> set = new HashMap<Integer, String>;
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>; ...
Set<String> set = new HashSet<String>;
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 ...
List<String> list = new ArrayList<String>;
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) { System.out.println(aString); } For loop parenthesis, declaration of a String variable is done. ...
List<String> list = new ArrayList<String>; for(String aString : list) { System.out.println(aString); }
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: " + ...
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: " +
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 ...
public class Box<T> { // T stands for "Type" private T t; public void add(T t) { this.t = t; } public T get() { return t; } }
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)); list.add("String"); ...
List list = new ArrayList(); list.add(new Integer(2)); list.add("String");
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(); ...
// 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();
-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 ...
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 ...
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/httpd.sh -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 ...
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 ...
• 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.
• 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 ...
• 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 ...
• 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. ...
Size Reduced for Images in PDF &...
05-15-2013, 05:53 PM in Java Software