Essential Java

Primary discussion about Creating An Java Application:

Here's a sample Java application that I'll develop over the next few sections, all the way through the compiling and running stages.

public class app
{
public static void main(String[] args)
{
System.out.println("Hello from JAVA!");
}
}

Output: Here's how it looks in a DOS window under windows:

C:\java app

Hello from JAVA!

Topñ

public class app:

Here's the first line in app.java,

public class app
{
....
....
}

=> This line indicates that we're creating a new java class named app.

Note: The keyword public in the preceding code. This keyword is an access specifier. Which you'll learn more about in later. The public access specified indicates that this class is available anywhere in a progress that makes use of it.

Note: If you make a class public, Java insists that you name the file after it. That is, you can only have one public class in a Java file. The reason for this is that the Java compiler will translate the .java file into a byte code file with the extension ".class", which means that app.java will be translated into app.class and if the JVM needs the app class, it'll know to look in the app.class file.

Note: Java always encloses block of code within curly braces - that is "{" and "}".

Topñ

public static void main(String[] args):

public class app
{
public static void main(String[] args)
{
....
....
}
}

=> main is a class method not an object method. It must not return a value when it's finished executing which is why we use the keyword void in this code (in other words, a return value of type void means that there actually is no return value). We are including that main is passed an array of string values called args. There stings values hold the values passed from the command line when you start the application for example java app Hello there, then "Hello" and "there" would be the two strings in the args array.

Topñ

System.out.println("Hello from JAVA!"):

public class app
{
public static void main(String[] args)
{
System.out.println("Hello from JAVA!");
}
}

=> The java.lang package's System class includes a field called out and this field, in turn, has a method named printin, which does the actual displaying of text.

Topñ

Basic Knowledge about Commenting in Java:

Java supports three types of comments.

1st type: You can surround a comment of any length with the characters /* and */.

/* This application prints out "Hello from JAVA!" */
public class app
{
public static void main(String[] args)
{
System.out.println("Hello from JAVA!");
}
}

=> The java compiler will ignore all the text between the /* and */ markers. You can split comments between this markers across multiple lines, such as,

/* This application prints out "Hello from JAVA!"
Created by: G. Whiz, 1/1/00 */
public class app
{
public static void main(String[] args)
{
System.out.println("Hello from JAVA!");
}
}

2nd type: Java also supports a one-line comment, using a double slash, //.

public class app // Create the app class
{
public static void main(String[] args)
{
// Print out the message
System.out.println("Hello from JAVA!");
}
}

3rd type: Finally, java also supports a documentation comment, which starts with /** and ends with */. This comment is designed to be used with the javadoc tool, which can automatically.

/** This application prints out "Hello from JAVA!" */
public class app
{
public static void main(String[] args)
{
System.out.println("Hello from JAVA!");
}
}

Topñ

Importing Java Packages And Classes:

The classes that Sun has created for you to use are stored in class libraries called packages. To make a class in a package available to your code. You have to import the package, which means the compiler will search that package for classes. You can also import individual classes that are not part of a package. By default, only the basic java statement are available to you in an application - that is, the ones in the core java.lang java package. The compiler automatically imports the java.lang package for you, but to use the rest of the classes that come with java, you'll have to do your own importing with the import statement. Here's how you use that statement.

Syntax:

Import[Package1 [.package2. . . .]](.class|*)

The stander java packages, themselves are stored in a large package called java. If you're going to use import statement to import classes into a program, the import statements should be at the top of the code.

import java.util.date;
public class app
{
public static void main(String[] args)
{
System.out.println("Today = "+ new Date());
}
}

There's also a shorthand technique that loads in all the classes in a package - you can use an asterisk (*) as a wildcard to stand for all classes in a particular package.

import java.util.*;
public class app
{
........
........
}

To import your own classes you should follow the process which given as example in below.

Suppose you have a classes named printer in a file named printer.java and that class has one method named print.

public class printer
{
public void print()
{
System.out.println(" Hello from Java! ");
}
}

Now, you might want to make use of the print method in other classes as in this case where we're creating a new object of the printer class using new operator and using that object's print method in a application named app.

import printer;
public class app
{
public static void main(String[] args)
{
(new printer()).print();
}
}

=> You can import the printer class (Own class) this way.

Topñ

Creating Applets:

Standard applets are built on the Applet class which is in the java.applet package. The java.applet.Applet class is the class that forms the base for standard applets and you can derive your own applet classes from this class using the extend keyword.

import java.applet.Applet;
public class applet extends Applet
{
.......
.......
}

Applets don't have a main method like applications do - in fact, that's the primary code difference between applets and applications.

import java.applet.Applet;
import java.awt.*;
public class applet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello from Java!",60,100)
}
}

=> The paint method is passed a java object of the Graphics class (this object is named g in the code). You can use this object's drawString method to actually draw the text. In this case, we'll draw the text "Hello from Java!" at location (60, 100) in the applet. Now you can compile applet.java to applet.class.

Topñ

Running Applets:

To display an applet, we can use a web page with an HTML tag in it. For now, here's a Web Page, applet.html, that will display the applet.



Applet











=> You can open this applet Web Page in a Web browser or you can also use the sun appletviewer, which comes with java to open applet.html.

C:\> appletviewer applet.html

Topñ

Creating Windowed Applications:

Creating a windowed application is much like creating an applet, except that you have to have a main method and you're responsible for creating tha window yourself. To create the window for the application, we'll derive a new class form AWT Frame class.

Now we'll create the application class itself, which we'll name app. This is the class that will have a main method and we'll use the new operator to create a new object of the AppFrame class.

import java.awt.*;
import java.awt.event.*;
class AppFrame extends Frame
{
public void paint(Graphics g)
{
g.drawString("Hello from Java=",60,100);
}
}

public class app1
{
public static void main(String[] args)
{
AppFrame f=(new AppFrame());
f.setSize(200,200);
f.addwindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
f.show();
}
}

Topñ

Running Windowed Applications:

To run a windowed application, you can use the java and javaw tools after compiling the program.

C:\> java app

C:\> javaw app

=> The java tool launches the application and makes the console window wait until the application is dismissed, whereas the javaw launches the application and doesn't wait until the application is dismissed.