Saturday, January 19, 2008

Java Tips for ...novice programmers...

Object-Oriented Programming• An abstract method cannot (obviously) be final.
• An abstract method cannot be static because static methods cannot be
overridden.
• An instance method can be both protected and abstract. A static method
can be protected.
• The JVM does not call an object’s constructor when you clone the object.
• Classes can be modified from their default state using any of the three
keywords: public, abstract, and final. So, can’t have a static class,
only static methods.
• A final variable is a constant, and a final method cannot be overridden.
• A call to this in a constructor must also be on the first line.
Note: can’t have an explicit call to super followed by a call to this
in a constructor - only one direct call to another constructor is allowed.


Memory and Garbage Collection• Can’t predict when garbage collection will occur, but it does run
whenever memory gets low.
• If you want to perform some task when your object is about to be
garbage collected, you can override the java.lang.Object method called
finalize(). This method is declared as protected, does not return a value,
and throws a Throwable object, i.e. protected void finalize() throws Throwable.
• Always invoke the superclass’s finalize() method if you override finalize().
• The JVM only invokes finalize() once per object. Since this is the case,
do not resurrect an object in finalize as when the object is finalized
again its finalize() method will not be called. Instead you should create a
clone of the object if you must bring the object back to life.
• Remember that Java passes method parameters by value and not by
reference. Obviously then, anything that happens to a primitive data
type inside a method does not affect the original value in the calling code.
Also, any reassignment of object references inside a method has no effect
on the objects passed in.


Exceptions
• Invoking a method which declares it throws exceptions is not
possible unless either the code is placed in a try-catch, or the calling
method declares that it throws the exceptions, i.e. checked
exceptions must be caught or rethrown. If the try-catch
approach is used, then the try-catch must cope with all of the
exceptions which a method declares it throws.
• RuntimeException and its subclasses are unchecked exceptions.
• Unchecked exceptions do not have to be caught.
• All Errors are unchecked.
• You should never throw an unchecked exception in your own code, even
though the code will compile.

Methods

• A native method does not have a body, or even a set of braces,
e.g. public native void method();
• A native method cannot be abstract.
• A native method can throw exceptions.
• A subclass may make an inherited method synchronized, or it may
leave offthe synchronized keyword so that its version is not synchronized.
If a methodin a subclass is not synchronized but the method in the
superclass is, the threadobtains the monitor for the object when it enters
the superclass’s method.• You cannot make a method in a subclass more
private than it is defined in thesuperclass, ‘though you can make it more public.


Threads

• A Java program runs until the only threads left running are daemon threads.
• A Thread can be set as a user or daemon thread when it is created.
• In a standalone program, your class runs until your main() method
exists - unless your main() method creates more threads.
• You can initiate your own thread of execution by creating a Thread
object, invoking its start() method, and providing the behaviour that
tells the thread what to do. The thread will run until its run() method
exists, after which it will come to a halt - thus ending its life cycle.
• The Thread class, by default, doesn’t provide any behaviour for run().
• A thread has a life cycle. Creating a new Thread instance puts the thread
into the "new thread" state. When the start() method is invoked, the
thread is then "alive" and "runnable". A thread at this point will repond
to the method isAlive () by returning true.
• The thread will continue to return true to isAlive() until it is "dead",
no matter whether it is "runnable" or "not runnable".
• There are 3 types of code that can be synchronized: class methods, i
nstance methods, any block of code within a method.
• Variables cannot take the synchronized keyword.
• Synchronization stays in effect if you enter a synchronized method
and call out to a non-synchronized method. The thread only gives
up the monitor after the synchronized method returns.



Inner Class

• If you define an inner class at the same level as the enclosing class’
instance variables, the inner class can access those instance variables -
no matter what their access control.
• If you define an inner class within a method, the inner class can
access the enclosing class’ instance variables and also the local
variables and parameter for that method.
• If you do reference local variables or parameters from an inner
class, those variables or parameters must be declared as final to
help guarantee data integrity.
• You can also define an anonymous class - a class without a name.
• If you’d like to refer to the current instance of the enclosing class,
you can write EnclosingClassName.this.
• If you need to refer to the inner class using a fully qualified name,
you can write EnclosingClassName.InnerClassName.
• If your inner class is not defined as static you can only create new
instances of this class from a non-static method.
• Anonymous classes cannot have const


Serializing Objects

•It is now possible to read and write objects as well as primitive data
types, using classes that implement ObjectInput and ObjectOutput.
These two interfaces extend DataInput and DataOutput to read or
write an object. ObjectInputStream and ObjectOutputStream
implement these interfaces.
• If a class implements the Serializable interface, then its public and
protected instance variables will be read from and written to the
stream automatically when you use ObjectInputStream and
ObjectOutputStream.
• If an instance variable refers to another object, it will also be
read or written, and this continues recursively.
• If the referenced object does not implement the Serializable
interface, Java will throw a NotSerializableException.
• The Serializable interface serves only to identify those instances
of a particular class that can be read from and written to a stream.
It does not actually define any methods that you must implement.
• If you create your own class and want to keep certain data from
being read or written, you can declare that instance variable
using the transient keyword. Java skips any variable declared as
transient when it reads and writes the object following the Serializable
protocol.


Reflection

• Using the Reflection API, a program can determine a class’ accessible
fields, methods and constructors at runtime.


will be adding moreeeeeeeeeeeee later, guys....... you are also free to add comments on this and more points........

Friday, December 14, 2007

MVC and Swing

The Model-View-Controller (MVC) architecture factors function code from the GUI design using a controller module. The controller module ties event listeners in the view module to their actions in the model module. Good programming practice implies private properties with public accessor methods for those needing access from outside their container object. These three object modules can be designed at different times by different programmers.

The well-known MVC paradigm is generally recommended as the fundamental architecture for GUI development. Many variations of the pattern are available, like MVC++, HMVC (Hierarchical MVC), MVC Model 2, MVC Push, and MVC Pull, with each emphasizing slightly different issues.

The model object should have methods for run(), about(), help(), and exit() as these are common to most utilities. The view object constructor should accept a string that incorporates the utility title. The view object also requires methods to build the listeners. buttonActionListeners() includes addActionListener() and a setActionCommand(string) which is used to pass a reference of the pressed button. The controller module uses getActionCommand() to call the correct action method in the model. An example of MVC architecture using the MyGUI example is:



/**

* @author Deepak Singhvi

* @version v7.4

*/



import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/* myGUI demonstrates separation of functionality

from GUI design by using MVC architecture */



// first comes the root class that builds the architecture

public class MyGUI {



public static void main(String args[]) {

MyGUIModel model = new MyGUIModel();

MyGUIView view = new MyGUIView("myGUI MVC Demo");

MyGUIController controller = new MyGUIController(model, view);

}

}



// the model is where functionality (ie properties & methods) goes

class MyGUIModel {



public void exit() {

System.exit(0);

}



public void run() {

JOptionPane.showMessageDialog(null, "Deepak hear you!",

"Message Dialog", JOptionPane.PLAIN_MESSAGE);

}

}



// the view is where the GUI is built

class MyGUIView extends JFrame {



JButton run = new JButton("Run the Utility");

JButton exit = new JButton("Exit After Save");

JPanel buttons = new JPanel(new GridLayout(4, 1, 2, 2));



MyGUIView(String title) // the constructor

{

super(title);

setBounds(100, 100, 250, 150);

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

buttons.add(run);

buttons.add(exit);

this.getContentPane().add("Center", buttons);

setVisible(true);

}



//method to add ActionListener passed by Controller to buttons

public void buttonActionListeners(ActionListener al) {

run.setActionCommand("run");

run.addActionListener(al);

exit.setActionCommand("exit");

exit.addActionListener(al);

}

}



// the controller listens for actions and reacts

class MyGUIController implements ActionListener {



MyGUIModel model;

MyGUIView view;



public MyGUIController(MyGUIModel model, MyGUIView view) {

  // create the model and the GUI view

  this.model = model;

  this.view = view;

  // Add action listener from this class to buttons of the view

  view.buttonActionListeners(this);

}



// Provide interactions for actions performed in the view.

public void actionPerformed(ActionEvent ae) {

  String action_com = ae.getActionCommand();

  char c = action_com.charAt(0);

  switch (c) {

    case'r':

      model.run();

      break;
 
    case'e':

      model.exit();

      break;

    }

  }

}





Tuning Garbage Collection with the 5.0 Java[tm] Virtual Machine

Hi Friends,

I found this white paper on Garbage collection in Sun's website intersting. It discusses on options of choosing the GC.

In the J2SE platform version 1.4.2 there were four garbage collectors from which to choose but without an explicit choice by the user the serial garbage collector was always chosen. In version 5.0 the choice of the collector is based on the class of the machine on which the application is started.

This “smarter choice” of the garbage collector is generally better but is not always the best. For the user who wants to make their own choice of garbage collectors, this document will provide information on which to base that choice. This will first include the general features of the garbage collections and tuning options to take the best advantage of those features. The examples are given in the context of the serial, stop-the-world collector. Then specific features of the other collectors will be discussed along with factors that should considered when choosing one of the other collectors.

When does the choice of a garbage collector matter to the user? For many applications it doesn't. That is, the application can perform within its specifications in the presence of garbage collection with pauses of modest frequency and duration. An example where this is not the case (when the serial collector is used) would be a large application that scales well to large number of threads, processors, sockets, and a large amount of memory.





http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html

I hope it will help in optimizing the GC related problems

Sunday, November 25, 2007

Inheritance, Dependency, Association, Aggregation, Composition....a comparative study...

The ‘is a’ relationship is expressed with inheritance and ‘has a’ relationship is expressed with composition. Both inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for code reuse are class inheritance and object composition.
Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses extends key word.
Composition: is used when House has a Bathroom. It is incorrect to say House is a Bathroom. Composition simply means using instance variables that refer to other objects. The class House will have an instance variable, which refers to a Bathroom object.

Which one to use?
Inheritance should be only used when subclass ‘is a’ superclass.
􀂃 Don’t use inheritance just to get code reuse. If there is no ‘is a’ relationship then use composition for code reuse. Overuse of implementation inheritance (uses the “extends” key word) can break all the subclasses, if the superclass is modified.
􀂃 Do not use inheritance just to get polymorphism. If there is no ‘is a’ relationship and all you want is polymorphism then use interface inheritance with composition, which gives you code reuse.


Difference between dependency, association,aggregation and composition:


Though Java is a true object-oriented language that thoroughly supports inheritance, careful thought should be given to the use of this feature, since in many cases an alternative is to use a more flexible object relationship. The commonly identified object relationships are as follows:
a) dependency
b) association
c) aggregation
d) composition

The distinction between these relationships is based on the duration and the nature of the relationship.
The aggregation and composition relationships involve a tighter binding between the related objects. The related objects have a long-term relationship and have some level of mutual dependency, which may be exclusive, that defines their existence.

Dependency
An object dependency exists when there is a short-term relationship between the objects. For instance, the relationship between a shopping cart and a checkout object would be short term, since once the checkout operation is complete, the checkout object would no longer be needed. The same relationship would exist for a stock transfer object that needed to use a stock item object to get the information on the stock item being transferred. The stock transfer object would have a dependency relationship with the stock item object; the stock transfer object would read the transfer information and could then discard the stock item object and continue processing.













AssociationAn object association represents a more long-term association than the dependency relationship. The controlling object will obtain a reference to the association object and then use the reference to call methods on the object. The relationship between a car and a driver is representative of this relationship. The car will have a driver who will be associated with the car for a period of time.




Aggregation is an association in which one class belongs to a collection. This is a part of a whole
relationship where a part can exist without a whole. With an aggregation relationship, the contained object is part of the greater whole. There is a mutual dependency between the two objects, but the contained object can participate in other aggregate relationships and may exist independently of the whole. For example, a FileReader object that has been created using a File object represents a mutual dependency where the two objects combine to create a useful mechanism for reading characters from a file. The UML symbols for expressing this relationship as shown in following figure which involve a line connecting the two classes with a diamond at the object that represents the greater whole.




Composition is an association in which one class belongs to a collection. This is a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted. So composition has a stronger relationship.
With the composition relationship, the client object is owned by the greater whole. The contained object cannot participate in more than one compositional relationship. An example of this is a customer object and its related address object; the address object cannot exist without a customer object. This relationship is shown with a darkened diamond at the object, which represents the greater whole, as shown in the follwing figure


....will be updating with example code later.....

Java Tools Collections

Users can find all tools realted to java at this link
http://javatoolbox.com/categories

For example, if you are looking for some testing tool, but not sure which one to use or from where to get, you can check it here
http://javatoolbox.com/categories/tests




Friday, November 2, 2007

Common causes for memory leaks in Java applications

1) Unbounded caches
A very simple example of a memory leak would be a java.util.Collection object (for example, a HashMap) that is acting as a cache but which is growing without any bounds.

public class MyClass {
static HashSet myContainer = new HashSet();
public void leak(int numObjects) {
for (int i = 0; i < numObjects; ++i) {
String leakingUnit = new String("this is leaking object: " + i);
myContainer.add(leakingUnit);
}
}
public static void main(String[] args) throws Exception {
{
MyClass myObj = new MyClass();
myObj.leak(100000); // One hundred thousand
}
System.gc();
}


In the above program, there is a class with the name MyClass which has a static reference to HashSet by the name of myContainer. In the main method of the class: MyClass, (in bold text) within which an instance of the class: MyClass is instantiated and its member operation: leak is invoked. This results in the addition of a hundred thousand String objects into the container: myContainer. After the program control exits the subscope, the instance of the MyClass object is garbage collected, because there are no references to that instance of the MyClass object outside that subscope. However, the MyClass class object has a static reference to the member variable called myContainer. Due to this static reference, the myContainer HashSet continues to persist in the Java heap even after the sole instance of the MyClass object has been garbage collected and, along with the HashSet, all the String objects inside the HashSet continue to persist, holding up a significant portion of the Java heap until the program exits the main method.

This program demonstrates a basic memory leaking operation involving an unbounded growth in a cache object. Most caches are implemented using the Singleton pattern involving a static reference to a top level Cache class as shown in this example.


Here is the GC information for you for the above snippet....

[GC 512K->253K(1984K), 0.0018368 secs]
[GC 765K->467K(1984K), 0.0015165 secs]
[GC 979K->682K(1984K), 0.0016116 secs]
[GC 1194K->900K(1984K), 0.0015495 secs]
[GC 1412K->1112K(1984K), 0.0015553 secs]
[GC 1624K->1324K(1984K), 0.0014902 secs]
[GC 1836K->1537K(2112K), 0.0016068 secs]
[Full GC 1537K->1537K(2112K), 0.0120419 secs]
[GC 2047K->1824K(3136K), 0.0019275 secs]
[GC 2336K->2035K(3136K), 0.0016584 secs]
[GC 2547K->2248K(3136K), 0.0015602 secs]
[GC 2760K->2461K(3136K), 0.0015517 secs]
[GC 2973K->2673K(3264K), 0.0015695 secs]
[Full GC 2673K->2673K(3264K), 0.0144533 secs]
[GC 3185K->2886K(5036K), 0.0013183 secs]
[GC 3398K->3098K(5036K), 0.0015822 secs]
[GC 3610K->3461K(5036K), 0.0028318 secs]
[GC 3973K->3673K(5036K), 0.0019273 secs]
[GC 4185K->3885K(5036K), 0.0019377 secs]
[GC 4397K->4097K(5036K), 0.0012906 secs]
[GC 4609K->4309K(5036K), 0.0017647 secs]
[GC 4821K->4521K(5036K), 0.0017731 secs]
[Full GC 4521K->4521K(5036K), 0.0222485 secs]
[GC 4971K->4708K(8012K), 0.0042461 secs]
[GC 5220K->4920K(8012K), 0.0018258 secs]
[GC 5432K->5133K(8012K), 0.0018648 secs]
[GC 5645K->5345K(8012K), 0.0018069 secs]
[GC 5857K->5558K(8012K), 0.0017825 secs]
[GC 6070K->5771K(8012K), 0.0018911 secs]
[GC 6283K->5984K(8012K), 0.0016350 secs]
[GC 6496K->6197K(8012K), 0.0020342 secs]
[GC 6475K->6312K(8012K), 0.0013560 secs]
[Full GC 6312K->6118K(8012K), 0.0341375 secs]
[GC 6886K->6737K(11032K), 0.0045417 secs]
[GC 7505K->7055K(11032K), 0.0027473 secs]
[GC 7823K->7374K(11032K), 0.0028045 secs]
[GC 8142K->7693K(11032K), 0.0029234 secs]
[GC 8461K->8012K(11032K), 0.0027353 secs]
[GC 8780K->8331K(11032K), 0.0027790 secs]
[GC 9099K->8651K(11032K), 0.0028329 secs]
[GC 9419K->8970K(11032K), 0.0027895 secs]
[GC 9738K->9289K(11032K), 0.0028037 secs]
[GC 10057K->9608K(11032K), 0.0028161 secs]
[GC 10376K->9927K(11032K), 0.0028482 secs]
[GC 10695K->10246K(11032K), 0.0028858 secs]
[GC 11014K->10565K(11416K), 0.0029284 secs]
[Full GC 10565K->10565K(11416K), 0.0506198 secs]
[GC 11781K->11071K(18956K), 0.0035594 secs]
[GC 12287K->11577K(18956K), 0.0042315 secs]
[GC 12793K->12082K(18956K), 0.0043194 secs]
[GC 12843K->12390K(18956K), 0.0030633 secs]
[GC 13606K->13494K(18956K), 0.0085937 secs]
[Full GC 13782K->13613K(18956K), 0.0646513 secs]




2) Infinite Loops

Some memory leaks occur due to program errors in which infinite loop in the application code allocates new objects and adds them to a data structure accessible from outside the program loop scope. This type of infinite loops can sometimes occur due to multithreaded access into a shared unsynchronized data structure. These types of memory leaks manifest as fast growing memory leaks, where if the verbose GC data reports a sharp drop in free heap space in a very short time leads to an OutOfMemoryError.
Friends please add more to this post... dying to see more in this post....

Simple performance improvement suggestions.

I have found the following simple way of making small improvements to performance for projects in the past.


- Use Boolean.valueOf(b) instead of new Boolean(b)
Note: however, some classes use a flag and rely on new Boolean() object being
different. A bad practice
synchronized(myInRelServFlg) {
myInRelServFlg = new Boolean(theFlg.booleanValue());
}

- Avoid the use of new String(String)
- Avoid the use of a single character String in a string concatenation. Or better
yet use StringBuilder where possible. Most times the synchronization from
StringBuffer is not required. Sun just released StringBuilder in 1.5, an
unsynchronized variant of StringBuffer.

- Avoid the use of StringBuffer().toString() in string concatenation.
- Avoid creating temproary objects to convert to a String or from a String. E.g. new
Integer(int).toString() and new Integer(String).intValue()
- Use constants for zero length arrays rather than creating the dynamically. Note:
however, some classes use zero length array objects for locks. A bad practice
terminationLock = new int[0];

- Avoid the use of loop to copy arrays, use System.arraycopy() instead.
- Avoid creating an instance of a class just to get the classes name. e.g.
(new java.sql.Date(123456)).getClass().getName ()

-About the history of Vector: Vector was in the 1.0 libraries
By the time of 1.2 ArrayList had come along and people were switching to it for
better performance. Vector was synchronised and because of that slower than the
unsynchronised ArrayList.
By the time of 1.4 however the synchronisation mechanism was much much better -
and Vector actually had a slight performance advantage over ArrayList.

-Performance with inner classes: When considering whether to use an inner class,
keep in mind that application startup time and memory footprint are typically
directly proportional to the number of classes you load. The more classes you
create, the longer your program takes to start up and the more memory it will take.
As an application developer one has to balance this with other design constraints
the person may have. I am not suggesting you turn your application into a single
monolithic class in hopes of cutting down startup time and memory footprint — this
would lead to unnecessary headaches and maintenance burdens.

Does anyone have any comments or other suggestions?

Heroku Custom Trust Store for SSL Handshake

  Working with Heroku for deploying apps (java, nodejs, etc..) is made very easy but while integrating one of the service ho...