Sunday, January 30, 2011

The 'final' keyword always confusing!!!

Nowadays, I am doing some stuffs in java, so, I am quite close to java. It is 12:56 AM, last day of Jan i.e Jan 31st!. I was about to go to bed to sleep, but I got something which is quite confusing for many programmers the keyword 'final'. Here are the points which will clearify you to some extent:

1) The final keyword can be used for classes, methods, variables, and arguments. (more confused!) wait I will clarify you.
2) First, let the final variables, the variables declared as final can be instantiated only ONCE!

3) Lets think about methods, lets guess what may be special if we make the methods final, it is related to inheritance. Those methods declared as final can not be overridden.

4) The arguments declared as final also can be initialized only once. (I have not checked it... so , don't believe until you check it out)

5) The final classes are certainly similar to sealed classes i.e the classes can not be inherited.

(I am going to elaborate this one when I have time to write sample code using final keyword for different cases and test the result. )

Thursday, January 20, 2011

Difference between abstract class and interace

It is my problem, I always forget the when to use interface and abstract classes. I used to develop so large applications, but I seem to be unaware of when is the time to use interface. Here are some notes to be clear about interface and abstract classes:

Creation of abstract classes:
1) The abstract class should have abstract keyword before the class name e.g.
public abstract class Animal
{
public void eat()
{
//method description
}
public void sleep(int miliseconds)
{
//do implementation
}
public abstract void makeNoise();
}
2) There should be at least one abstract method in the abstract class which is to be defined in the subclass. There are other methods also and we can implement those methods inside the abstract class. The subclass may implement the abstract classes according to their need.
Note: we can not create instances of abstract classes. Actually, it is not appropriate to give provision of creating instances of abstract classes because it contains unimplemented abstract methods and JVM will confused what to do if those methods were called by the instances.

Creation of Interfaces
1) The keyword interface is used to declare interfaces. The interfaces are created similar ways as classes are created.
public interface Player
{
public String name();
public int numberToken();
public void takeTurn(Pile pile, int maxOnATurn);
}
Note: the interface methods are public and abstract. So, we can omit the public and abstract keywords also i.e. the below code is equivalent to above code:
public interface Player
{
public abstract String Name();
int numberToken();
abstract takeTurn(Pile pile, int maxOnATurn);
}
2) The method implementation is not allowed. The implementation is carried out in implementing class.
Note: there is no meaning of creating private members in interface because they are inaccessible in the implementing class. And it is compulsory for the implementing class to implement all the methods in the interface.

The implementation is carried out in the following way
class lazyPlayer implements Player
{
public string name()
{
//implement here
}
public int numberToken()
{
//
}
public void takeTurn(Pile pile, int maxOnATurn)
{
//
}

}

From above, we got the main differences on how the abstract classes and interfaces are created.

Abstract Class and Interface

1) Abstract class is similar to class except we cant create instances, but interface is just the declaration of methods which to be implemented by implementing class.

2) Since, Java does not support multiple inheritance, but we can implement multiple interfaces.

3) We select abstract class in case when we need to define only a few methods in subclass (specialisation) while using all other methods defined by superclass. But, in interface, we can't use any method from interface because no methods are implemented in interface, we need to implement in the implementing class.

I will add more notes here if I get something new regarding interface and abstract class.
Your Comments are Welcome!!!

Wednesday, January 19, 2011

Java Coding Conventions

It is difficult to remember all the rules for coding in any language. Since, it is good to follow certain coding standards so that it is easy to maintain the source code because the code would be understandable.
So, I have presented sample example which tries to cover almost all the rules for coding standards:

/*
* %W% %E% Firstname Lastname
*
* Copyright (c) 1993-1996 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package java.blah;
import java.blah.blahdy.BlahBlah;
/**
* Class description goes here.
*
* @version 1.10 04 Oct 1996
* @author Firstname Lastname
*/
public class Blah extends SomeClass {
/* A class implementation comment can go here. */
/** classVar1 documentation comment */
public static int classVar1;
/**
* classVar2 documentation comment that happens to be
* more than one line long
*/
private static Object classVar2;
/** instanceVar1 documentation comment */
public Object instanceVar1;
/** instanceVar2 documentation comment */
protected int instanceVar2;
/** instanceVar3 documentation comment */
private Object[] instanceVar3;
/**
* ...method Blah documentation comment...
*/
public Blah() {
// ...implementation goes here...
}
/**
* ...method doSomething documentation comment...
*/
public void doSomething() {
// ...implementation goes here...
}
/**
* ...method doSomethingElse documentation comment...
* @param someParam description
*/
public void doSomethingElse(Object someParam) {
// ...implementation goes here...
}
}

Documentation on comments

Are you a Java programmer? yes, then the below snippet of code would fully illustrate how documentation comments are created and the required parameters for that.

/**
* Graphics is the abstract base class for all graphics contexts
* which allow an application to draw onto components realized on
* various devices or onto off-screen images.
* A Graphics object encapsulates the state information needed
* for the various rendering operations that Java supports. This
* state information includes:
*

    *
  • The Component to draw on
    *
  • A translation origin for rendering and clipping coordinates
    *
  • The current clip
    *
  • The current color
    *
  • The current font
    *
  • The current logical pixel operation function (XOR or Paint)
    *
  • The current XOR alternation color
    * (see setXORMode)
    *

*


* Coordinates are infinitely thin and lie between the pixels of the
* output device.
* Operations which draw the outline of a figure operate by traversing
* along the infinitely thin path with a pixel-sized pen that hangs
* down and to the right of the anchor point on the path.
* Operations which fill a figure operate by filling the interior
* of the infinitely thin path.
* Operations which render horizontal text render the ascending
* portion of the characters entirely above the baseline coordinate.
*


* Some important points to consider are that drawing a figure that
* covers a given rectangle will occupy one extra row of pixels on
* the right and bottom edges compared to filling a figure that is
* bounded by that same rectangle.
* Also, drawing a horizontal line along the same y coordinate as
* the baseline of a line of text will draw the line entirely below
* the text except for any descenders.
* Both of these properties are due to the pen hanging down and to
* the right from the path that it traverses.
*


* All coordinates which appear as arguments to the methods of this
* Graphics object are considered relative to the translation origin
* of this Graphics object prior to the invocation of the method.
* All rendering operations modify only pixels which lie within the
* area bounded by both the current clip of the graphics context
* and the extents of the Component used to create the Graphics object.
*
*

@author Sami Shaio
* @author Arthur van Hoff
*

@version %I%, %G%
*

@since 1.0
*/
public abstract class Graphics {

/**
* Draws as much of the specified image as is currently available
* with its northwest corner at the specified coordinate (x, y).
* This method will return immediately in all cases, even if the
* entire image has not yet been scaled, dithered and converted
* for the current output device.
*


* If the current output representation is not yet complete then
* the method will return false and the indicated
* {@link ImageObserver} object will be notified as the
* conversion process progresses.
*
*

@param img the image to be drawn
* @param x the x-coordinate of the northwest corner
* of the destination rectangle in pixels
* @param y the y-coordinate of the northwest corner
* of the destination rectangle in pixels
* @param observer the image observer to be notified as more
* of the image is converted. May be
* null
*

@return true if the image is completely
* loaded and was painted successfully;
* false otherwise.
*

@see Image
* @see ImageObserver
*

@since 1.0
*/
public abstract boolean drawImage(Image img, int x, int y,
ImageObserver observer);


/**
* Dispose of the system resources used by this graphics context.
* The Graphics context cannot be used after being disposed of.
* While the finalization process of the garbage collector will
* also dispose of the same system resources, due to the number
* of Graphics objects that can be created in short time frames
* it is preferable to manually free the associated resources
* using this method rather than to rely on a finalization
* process which may not happen for a long period of time.
*


* Graphics objects which are provided as arguments to the paint
* and update methods of Components are automatically disposed
* by the system when those methods return. Programmers should,
* for efficiency, call the dispose method when finished using
* a Graphics object only if it was created directly from a
* Component or another Graphics object.
*
* @see #create(int, int, int, int)
* @see #finalize()
* @see Component#getGraphics()
* @see Component#paint(Graphics)
* @see Component#update(Graphics)
* @since 1.0
*/
public abstract void dispose();

/**
* Disposes of this graphics context once it is no longer
* referenced.
*
* @see #dispose()
* @since 1.0
*/
public void finalize() {
dispose();
}
}

Tuesday, January 18, 2011

SVN startup and Commands

Startup

SVN stands for subversion and it is a very useful tool for parallel development of an application. If you are developing a software application with collaboration with other developers, then it is a good idea to use SVN to develop parallel because all the burdens and hassles to manage and create consistency of the files is done by it.

The concept of SVN is to distribute files to users from a central file, i.e. all the controls is carried out by central server, so, although, the files are modified by multiple programmers, the actually managing is carried out by the server. Different from the central file server, the SVN actually keeps the file versions so that it is easy for developer to track the files to find out the changes in files, so it is called version control.

As stated earlier, to use SVN, we first of all, need SVN server which carries out the tasks file management. So, you can make your computer to work as SVN repository so that you can use it as SVN testing server. For that, do the following steps:
1) Create a directory. I created temp_repos directory under /home/krishna/tmp
2) Run the below command
     svnadmin create /home/krishna/tmp/temp_repos
This new repository uses the default FSFS database layer.
3)Run the following command for confirmation:
svn info file:///home/krishna/tmp/temp_repos


The above command will print below information:
Path: temp_repos
URL: file:///home/krishna/tmp/temp_repos
Repository Root: file:///home/krishna/tmp/temp_repos
Repository UUID: db10e1a4-0059-44d6-8a0f-289514cf4bb9
Revision: 0
Node Kind: directory
Last Changed Rev: 0
Last Changed Date: 2011-01-18 23:09:24 +0100 (Tue, 18 Jan 2011)

Now we can use the above mentioned URL as repository.
Note: We do not need to start SVN server to use in this way, and we can use it locally.


If we want to run SVN to be accessible by remote computers, we must run the SVN server using the following command:
svnserve --daemon --root /home/krishna/tmp/temp_repos
Now you can access this repository using svn URLs.
svn info svn://svnhostname
The default access is readonly, but we can change the access by changing the file
/home/krishna/tmp_repos/conf/svnserve.conf
Similary, we can create and assign the users in that file by using the passwd file in the location. The file is readable and we can easily understand where to modify in the file.


Commands:

The commond commands for SVN are shown below:
(Note: the commands help can be obtained by typing svn --help)
The command summary is shown below:
add
blame (praise, annotate, ann)
cat
changelist (cl)
checkout (co)
cleanup
commit (ci)
copy (cp)
delete (del, remove, rm)
diff (di)
export
help (?, h)
import
info
list (ls)
lock
log
merge
mergeinfo
mkdir
move (mv, rename, ren)
propdel (pdel, pd)
propedit (pedit, pe)
propget (pget, pg)
proplist (plist, pl)
propset (pset, ps)
resolve
resolved
revert
status (stat, st)
switch (sw)
unlock
update (up)


In linux, I could not actually remove the SVN repository connection to the local working directory. So, one way of doing, although it would be cumbersome, to deleted all .SVN folders. So, there is a shortcut command in linux which removes the all .SVN files:
find . -iname ".svn" -print0 | xargs -0 rm -r