Concept of Java reflection is very powerful. It gives you access to internal information about the classes (sub classes, methods, attributes) loaded into the JVM. This concept is not available in other programming languages like C and C++.
Consider the following example. I have displayed the complete signatures of “ MyClass " methods using reflection. First of all, you should obtain a java.lang.Class object for the class that you want to manipulate. “ java.lang.Class " is
Serialization is the process of saving the state of an object onto any storage medium. Storage medium can be a file or a memory buffer. Then you can transmit it across a network, either in binary form, or in some human-readable text format such as XML. The object in the form of bytes or any other format can be recreated to its internal state easily. The process of serializing an object is also called “ Marshalling ” an object. Extracting a data structure from a series of bytes, is “ Deserialization
Java 5 supports automatic conversion of primitive types (int, float, double etc.) to their object equivalents (Integer, Float, Double,...) in assignments, method and constructor invocations. This conversion is know as autoboxing.
Java 5 also supports automatic unboxing, where wrapper types are automatically converted into their primitive equivalents if needed for assignments or method or constructor invocations.For example: Java Code: int inative = 0;
int inative = 0;
Exception handling is an integral part of programming. In Java exceptions are itself defined as objects in other words throwing an exception is throwing an object. “ Throwable ”, declared in “java.lang package” is the parent class for all the classes that a program can instantiate and throw.
Basically Throwable has two direct subclasses.
Exceptions (means members of the Exception family) are thrown to indicate abnormal
Framework is a set of classes that acts as a base for building some advanced functionality. It contains super classes having useful functionality and mechanisms which can either be used directly or can be extended in form of subclasses so as to extend the functionality without having to reinvent the basic mechanisms. For example, AWT is a framework for user interfaces.
The Java collections library is a framework for collection classes. It includes a number of interfaces and
Here we assume that programmer is well aware about the basic concepts of Collections Framework. For more information about basics on Collection Framework click here.
Now have a look on the classes that implement the " Collection " and " Map " interface.
As the collection interfaces have a few methods that can be easily implemented from fundamental methods where as abstract classes in the collection framework supply some of these routine implementations.
A design pattern describes a proven solution to a recurring problem or in other words it is a template for solving problems. Singleton pattern is a creation pattern.
As the name denotes there will be only an instance of an object, which is created by this pattern. For example: central configuration object should have only one instance that should be accessible from other parts of application. If we have more than one instances of a central configuration object, then different parts
Pointer in C/C++ denotes to the variables saving the reference, In Java there is only call by reference. But here the pointer refers to an object which itself has another reference within it.
Java classes can have instance variables. If an instance variable is of class type, then it can hold instance of the class in it. This is called pointer to the object.
In some cases, we need an object that contains an instance variable that can refer to another object of the same
Collections in Java include so many interesting features. As the number of features increase there arise the possibility of incorrect usage. Here we discuss such an issue while removing an object from a Collection. Exception throw is "Concurrentmodificationexception" .
Code: Java Code: package test;import java.util.ArrayList;
public class ListExample
package test;import java.util.ArrayList;
public class ListExample
"Whether to implement 'service' method or 'doGet/doPost' in Http Servlet ?" ,Is question often heard at least in the beginners columns.
Actually the answer depends on the requirement.
First we should know that the purpose of servlet is to accept an HTTP request from a Web browser, and return an HTTP response. This is done by "service", "doGet\ doPost" methods. Each of these include request object (HttpServletRequest) to receive data from
Mostly new Java programmers get confused when comparing strings. You can check the equality of strings either by using "==" or "equals()". But one should know when to use each of these in order to get the required results.
First, lets talk about "equals() " method. Method "equals()" is included in the class java.lang.Object which every Java class inherit. "equals()" method actually compares the characters that make up String object.
UDP stands for User Datagram Protocol.
UDP does not ensure reliable delivery of all the packets, the order in which the packets would be received and it is connectionless too.
Despite all these UDP provides quick transfer of packets through low-overhead because of an minimal error-checking and correction.
Java supports UDP through two classes.
contains a wrapper for an array of
Method "clone"creates and returns a copy of the object. It makes another object of the class in memory. Suppose, We are creating a clone of the object x then x.clone() != x will return true but it is not necessary that x.clone().equals(x) will return true. It depends upon the implementation of class.
Class should implement "Cloneable" interface in order to make clone of its objects. Otherwise "CloneNotSupportedException" will be thrown. Array,HashMap are considered
Good Java programmers use "instanceof" operator since it is a must to escape from "Class Cast Exceptions".
Following are the key points to be remembered while using instanceof operator
Instanceof operator is used to restore full functionality of an object.
While programming many times heterogeneous collection are used and then to invoke full functionality of an object casting is required then to check the type of instance, "instanceof"
Though it is very simple to override "equals" method for a class. But people usually make mistakes in overriding this method. It is very important for a Java developer to know about this method.
Some Important points to remember
Always remember to override "hashCode" method if you are overriding "equals" method otherwise you may face problem while using your class with HashTable, HashMap like classes as they uses "hashCode"
Hibernate is a popular OR (Object Relational) mapping framework. To access database, we have to use Hibernate Query language. I will present the basics in this post that will help you starting query database tables using HQL.
Hibernate queries can be roughly divided into 3 categories:
The queries we general use can be divided up into three main categories :
- Queries using one class
- Queries which several classes so join operation is required
It is right to say that a typical application programmer won't need to define annotations in normal routine. But still, one should know how to define annotations.
Annotation type declarations are very much like normal interface declarations. An at-sign (@) comes before the interface keyword and each method declaration defines an element of the annotation type. Method declarations should not have any parameters or a throws clause. Also note that the return type should be amoung the
Methods called on the asynchronous proxy will be executed asynchronously, and the results can be obtained later on.
To execute methods asynchronously in EJB 3.0, one can use asynchronous proxy. This allows calling methods at an instance, and collecting the results later. Good thing is that there exist a JBoss extension to EJB 3.0, which allows obtaining asynchronous proxy from the remote or local interface of a stateful session bean, stateless session bean or service bean.
To create an implementation of SyncML protocol, you may use sync4j. I will introduce you to sync4j in this post.
You may download it from: http://sync4j.sourceforge.net.
Sync4j possesses layered architecture and the layers are:
Core layer - The actual SyncML handling is done by the core layer. Its responsibilities include XML parsing and conversion of the SyncML markup to an internal object representation. Sync4j can also convert internal
I have found may people asking the difference between ClassPath and path. They tend to mix these and find this confusing. I will address this issue in this post.
Path is simply a system wide variable that tells where to find your commands. Like you can run directory command (dir) from an path. This is so because Windows path is set in the path variable. To see the contents of path variable, use following command on command prompt:
The aim is to iterate through a collection of data using a common interface and more importantly, underlying implementation should not be known. We have a behavioral design pattern called ‘Iterator Pattern’ for this. I will introduce you to it in this post.
Iterator should be implemented as an interface. This allows the user to implement it anyway its easier for him/her to return data.
I will take an example of daily life and will show how to use iterator pattern in that. You
Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value. Thing to remember is that it I s not any encryption technique. It’s simply hashing. Same message will always generate the same hash code and a slight change in the message will generate completely different hash code. In the next few posts, I will talk about how we can generate message digests in our Java programs.
The popular message digest algorithms are MD5 and
In the next few posts, I will write about how to perform an XSLT transformation using StAX APIs: Cursor API and Event Iterator API.
TrAX (Transformations API for XML) is a Java API for performing XSLT transformations. In J2SE 1.5, there are three different ways to represent the source and the result of an XSLT transformation and they are: by XML stream source/result, by SAX events source/result, and by DOM tree source/result. In J2SE 1.6 an XSLT transformation is performed using a
ANT makes building complex Java applications easier. But many people argue that managing ANT scripts is itself a cumbersome task and involves a lot of effort. In the next few posts, I will talk about how one can adopt best practices for creating and managing ANT scripts that will make the life of developers and configuration managers easier.
First thing is, one should follow a consistent style while writing XML for ANT files. If you format your ANT script, it will be visually appealing
A lot of J2ME developers face problems when running multiple instance of Nokia Wireless Toolkit emulators. Some of the problems are linked with in.use file. This post is all about that.
When your run the emulator, a file named in.use is created in the emulator’s root directory with will be something like:
This file is removed when the emulator shuts down.
This emulator’s default directory will also
I will use several examples to show how to use RecordStrore in J2ME applications.
Creating record store is simple. We use a static method to create or open an existing record store. Java Code: public static RecordStore openRecordStore(String recordStoreName,
public static RecordStore openRecordStore(String recordStoreName,
Eclipse provides a window called ‘Open Resource’ window to locate the required files in no time.
You can find it under the Navigate. Its shortcut is Ctrl+Shift+R. ‘Open Resource’ window allows you to type a search for any file that exists in your workspace. You are free to use wildcards like ? to replace a single character or * to replace an entire string. It is amazingly fast and useful.
Let me present a use case.
To make your Java applications flexible, you might want to give properties that keep on changing in properties file. A property file is simply a text file that has key-value pairs relation.
If you are thinking of writing your parser to read the property files, then think again. Java provides java.util.Properties class, that will help you deal with propery files. Property files comprise of key-value pairs in a file, where the key and value are separated by an equal
Maven uses an XML project descriptor to generate the object model for project. This file is POM.XML and should be located at the top level directory of the project.
The XML file contains all the needed information for the project. Apart from core information, it also contains groupid, version, name and all the libraries needed for the project.
In this post, I will present how to read properties from XML files.
We have following XML file with the required properties:
<!--?xml version="1.0" encoding="UTF-8"?-->
DTD for the XML file