Monday, April 13, 2009
Comments in java
// text
where the compiler ignores everything from // to the end of the line
/* text */
where the compiler ignores everything from /* to */ and
/** documentation */
which is ignored by the compiler the same way as the previous comment type. This one is a special comment as this indicates a documentation comment. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.
I am not going to tell you how to use them. There are many articles and documentation written about that. We all know how to use them and most of us use them on a daily basis. Over time we can develop a stereotype. Mine looks like this:
/** first name */
private String firstName;
/** last name */
private String lastName;
/**
* Constructs and returns the full name
* @return full name
*/
public String getFullName() {
if (firstName == null) {
if (lastName == null) {
// user does not have a name specified
return "[no name]";
}
else {
return lastName;
}
}
else {
if (lastName == null) {
return firstName;
}
else {
// construct full name from first and last name
return firstName + " " + lastName;
}
}
}
As you can see, I use doc comments to describe the meaning and function of my class members. I also like to use line comments, rather than block comments. Even if my comment spans multiple lines I prefer to use // comments. It's due to my IDE settings. When I press Ctrl+/ it un-/comments the selected lines. It's very neat feature and I use it a lot.
Recently, I was doing some code modifications and I wanted to re-visit some code later. I decide to break the code by putting a comment in the middle if it, so it would not compile and I would be forced to come back to it.
I did somethig like this:
myObject.getAnother()/* change to ID */.getName();
To my surprise the code did not break. Even when I changed it to
myObject.getAnother()./* change to ID */getName();
Obviously, when I did this
myObject.getAnother().ge/* change to ID */tName();
the code broke. At the end I made it to break in a nice way, anyway
myObject.getAnother()./* change to ID */.getName();
Sunday, April 12, 2009
how to detect usb device connected to system using java code
Here's d code..
import java.io.*;
/**
* Waits for USB devices to be plugged in/unplugged and outputs a message
*
*
*@author Ujjwal Soni
*@version 1.0, 16/04/2009
*/
public class FindDrive
{
/**
* Application Entry Point
*/
public static void main(String[] args)
{
String[] letters = new String[]{ "A", "B", "C", "D", "E", "F", "G", "H", "I"};
File[] drives = new File[letters.length];
boolean[] isDrive = new boolean[letters.length];
// init the file objects and the initial drive state
for ( int i = 0; i < letters.length; ++i )
{
drives[i] = new File(letters[i]+":/");
isDrive[i] = drives[i].canRead();
}
System.out.println("FindDrive: waiting for devices...");
// loop indefinitely
while(true)
{
// check each drive
for ( int i = 0; i < letters.length; ++i )
{
boolean pluggedIn = drives[i].canRead();
// if the state has changed output a message
if ( pluggedIn != isDrive[i] )
{
if ( pluggedIn )
System.out.println("Drive "+letters[i]+" has been plugged in");
else
System.out.println("Drive "+letters[i]+" has been unplugged");
isDrive[i] = pluggedIn;
}
}
// wait before looping
try { Thread.sleep(100); }
catch (InterruptedException e) { /* do nothing */ }
}
}
}
Tuesday, October 14, 2008
Simple Defination for EJB Session Facade
difference b/w EJB2.1 and EJB3.0
- No need of Home Interface (EJBHome),but it is needed in EJB2.0
- No more confusions to make an EJB remote or local,it's the client which would decide and cast to appropriate.
- Just write SINGLE simple Java class and annotate it to be Stateless/Stateful/Entity/MessageDriven.Container
- No Deployment Descriptors , MetaData Annotations are explored which is introduced in J2SE5.0
- Forget all EJB life cycles.For example Entity bean life cycle in 3.0 is new,managed,detached,removed.
- Ejb 3.0 siplifies the developement of the application
Ready to develop complex query,inner/outer join with EJB3.0.
The main difference lies in the persistence In case of EJB 3.0 there is JPA Java persistence API which makes the mapping of EntityBeans with the database easy with the help of a service called as EntityManager.
Mapping is done with the help of annotations unlike in EJB2.0.
Home interfaces are eliminated.
Deployment descriptors are an option in EJB 3.0.
EJB3.0 also supports webservice client through SOAP and WSDl.
Finally, concluding this topic I think the main difference is that EJB 3.0 is moved towards annotations based programming model and dependency injection to make our life easy .
For more information please visit http://javaknowledgestorm.blogspot.com/2009/04/differences-between-ejb20-and-ejb30.html
Saturday, August 30, 2008
Java's way of decompiling a class file
Hi Friends,
I finsished up with my project work early today. So, i thought of writing to this blog. I found an inbuilt way of decompiling the java class file that contains the binary data. Yes, jdk provides an inbuilt command that is javap.
Using javap you can decompile the java file. Folks, many of us might be using 3rd party tools like cavaj to decompile the class file, but java has its own way of doing this. Below is the example on how to do this.
c:\> javap Ujjwal.class
Cheers!
Ujjwal Soni
Tuesday, August 19, 2008
Trick to make netbeans fast
Hi,
There is a file named 'netbeans.conf' which can be found in
Example:
netbeans_default_options="-J-Xms384m -J-Xmx512m -J-XX:PermSize=32m -J-XX:MaxPermSize=96m -J-Xverify:none"
This example will allow NetBeans to run with a minimum of 384 megabytes of RAM and a maximum of 512 megabytes of RAM.
Cheers!
Ujjwal B Soni