Friday 22 July 2011

How to find the documentation of any java library class or interface

Suppose we want to check this class
java.util.Array
There are two ways
1.If you have an Internet connection then just google
oracle java.util.Array
This gives you the latest documentation
2.If you do not have Internet due to any reason then go to the java folder
open JDK
there will be a compressed folder with name src
open it and then open java folder
then open util
then you can find Array named java source file just open it in a IDE and generate documentation

Monday 18 July 2011

Providing command line arguments

Here is the code of the file
public class Hello
{
public static void main(String args[])
{
if(args.length==1)
System.out.println("Hello "+args[0]);
}
}
we save it in Hello.java and compile it by javac Hello.java
Then we run it like this
java Hello Codex
and it prints out
Hello Codex
but if we want to provide multiple words as our name then we do like this
java Hello "Codex Java"
and it prints out
Hello Codex Java

Sunday 17 July 2011

Setting tool tip Text for any JComponent

Tool Tip Text
Creating a tool tip for any JComponent object is easy. Use the setToolTipText method to set up a tool tip for the component. For example, to add tool tips to a button, you add only one line of code:
b1.setToolTipText("Button 1")

The following is the code of program we want to compile and run
public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
Save it with the name HelloWorld.java
Now Suppose we save it at D:\java
First we change to D:\java by using cd as shown in image


Now we compile and run it like this
javac HelloWorld.java
java HelloWorld
see the image


If you get an error saying javac was not recognized as an internal or external command or batch file
then you need to set path of javac
So right click on My Computer and select Properties
Now go to Advanced
Now create a new Environmental variable with the value to the bin folder of java

Thursday 7 July 2011

Using varargs
varargs allow you to pass more than one parameter to a function
example
class Example
{
pubic static void main(String args[])
{
Example ob=new Example();
ob.printNum(5,6,7);
ob.printNum(1,2,3,4);
}
void printNum(int... a)
{
int i;
for(i:a)
System.out.println(a);
}
}
How to compile a java program from inside another java program
Suppose you need to compile HelloWorld.java and run from another program
here is a code
Runtime r=Runtime.getRuntime();
Process p=r.exec("javac HelloWorld.java");
p.waitFor();
Process run=(Runtime.getRuntime).exec("java HelloWorld");

Sunday 3 July 2011

Anonymous inner classes


Anonymous inner class
An anonymous inner class is one that is not assigned a name. This section illustrates how an
anonymous inner class can facilitate the writing of event handlers. Consider the applet shown
in the following listing. As before, its goal is to display the string “Mouse Pressed” in the
status bar of the applet viewer or browser when the mouse is pressed.
// Anonymous inner class demo.
import java.applet.*;
import java.awt.event.*;
/*
<applet code="AnonymousInnerClassDemo" width=200 height=100>
</applet>
*/
public class AnonymousInnerClassDemo extends Applet {
public void init() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
showStatus("Mouse Pressed");
}
});
}
}
There is one top-level class in this program: AnonymousInnerClassDemo. The init( )
method calls the addMouseListener( ) method. Its argument is an expression that defines
and instantiates an anonymous inner class. Let’s analyze this expression carefully.
The syntax new MouseAdapter( ) { ... } indicates to the compiler that the code between the
braces defines an anonymous inner class. Furthermore, that class extends MouseAdapter. This
new class is not named, but it is automatically instantiated when this expression is executed.
Because this anonymous inner class is defined within the scope of
AnonymousInnerClassDemo, it has access to all of the variables and methods within
the scope of that class. Therefore, it can call the showStatus( ) method directly.
As just illustrated, both named and anonymous inner classes solve some annoying
problems in a simple yet effective way. They also allow you to create more efficient code.






 
You can find some useful programs at Example page