Home / Internet / How to Write a Java Program

How to Write a Java Program

Java is a high-level, object-oriented programming language developed in 1995 by James Gosling at Sun Microsystems (now owned by Oracle). Much of the language derives from C, and it was written with the idea of “write once, run anywhere.” The source is compiled into byte code and uses the virtual machine model to create an environment in which to run. A Java program could be written and compiled on a Microsoft Windows machine and run on a Linux machine, provided they both used the same Java Virtual Machine.

This article will cover a lot of ground in a very simplistic manner in an attempt to introduce the reader to Java programming concepts. As such, the focus will be on program structure rather than explaining coding structures such as try-catch, if-else and exception handling. It is hoped that the reader has enough programming experience with other languages.

Writing, compiling and running a Java program requires the following tools: a development environment, the Java Development Kit (JDK), and the Java Runtime Environment (JRE).

The most popular development environments are NetBeans (http://netbeans.org/) and Eclipse (http://www.eclipse.org/). A simpler development environment that can also be used is TextPad (http://www.textpad.com/). The JDK and JRE are available on the Java website (http://www.oracle.com/technetwork/java/javase/downloads/index.html)

In order to be able to write Java programs, it is essential to understand classes and objects. A class is a template for the object, like the blueprints for a building. At run time, the class is instantiated to create the object. It is the objects in a program that perform tasks and store information.

Using grammar as an analogy, an object would be a noun: it is a representation of a person, place or thing that is used in the application. Objects have properties, the grammatical equivalent of adjectives, that describe the object. Objects have methods, the grammatical equivalent verbs, that represent what the object will do.

As an example, here is the beginning code for a Person class:

public class Person {
}

Properties are added to the Person class as follows:

public class Person {

private String firstName;
private String lastName;
private int age;

}

Special methods called getters and setters are added to the class that allow other classes to get and set the properties. This example adds the getter and setter for one property to illustrate how it’s done.

public class Person {

private String firstName;
private String lastName;
private int age;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) throws Exception {
if (firstName.length() <= 15) this.firstName = firstName; else throw new Exception("Name too long"); } } Special methods called the constructors defines how a class can be instantiated. The constructor is named the same as the class. public class Person { private String firstName; private String lastName; private int age; public Person() { } public Person(String firstName, String lastName, int age) throws Exception{ setFirstName(firstName); setLastName(lastName); setAge(age); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) throws Exception { if (firstName.length() <= 15) this.firstName = firstName; else throw new Exception("Name too long"); } } Additional methods define what other actions the object can take: public class Person { private String firstName; private String lastName; private int age; public Person() { } public Person(String firstName, String lastName, int age) throws Exception { setFirstName(firstName); setLastName(lastName); setAge(age); } public String getFirstName() { return firstName; } public void setFirstName(String firstName) throws Exception { if (firstName.length() <= 15) this.firstName = firstName; else throw new Exception("Name too long"); } public void walk() { System.out.println("I am walking."); } public void stand() { System.out.println("I am standing."); } } The Person class would be just one part of the Java program. In order to use the Person class, and any other classes, a special class to manage the program must be written. Here is the beginning of that class: public class PersonProgram { } A special method named “main” is required to run the program: public class PersonProgram { public static void main(String args[]) { } } The Person object can then be used: public class PersonProgram { public static void main(String args[]) { try { if (args.length == 3) { Person p = new Person(); p.setFirstName(args[0]); p.setLastName(args[1]); p.setAge(Integer.parseInt(args[2])); p.walk(); p.stand(); } else { System.out.println("Usage: PersonProgram firstName, lastName, age"); } } catch (Exception ex) { System.out.println(ex.toString()); } } }

About User Lin