Analyzing the basic structure of Java code

java code

//Exam class definition
class Exam {
	int c;
	public int add(int a, int b) [
	c = a + b;
	return c;
}

//Create an object of Exam class and print the result returned from the object
class ExamTest {
	public static void main(String args[]) {
		int sum;
		int x, y;
		x = Integer.parseInt(args[0]);
		y = Integer.parseInt(args[1]);
		Exam examObject = new Exam();
		sum = examObject.add(x, y);
		System.out.println("The sum of the entered values is " + sum);
	}
}

In the above Java code, the main () method is a special method that must be present in a Java application. It is automatically executed when you run the program. Java’s main () method acts like main () in the C or C ++ language. The Java main () method is defined as follows:

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

Java applications can create objects of other classes in the main() method and then send messages to them to get the desired results. The argument part of the above main() method takes as a parameter args[]. This means that you will receive an array of strings as arguments.

The parameter is responsible for passing the necessary information when running the program. In programming, these parameters are called command-line arguments. Command line parameters are stored in an array in the order in which they are entered.

The basic example shown above converts data stored in args[] using the parseInt() method of the Integer class to use as a number. The parameters store data as strings and require conversion to use them as integers.

x = Integer.parseInt(args[0]);
y = Integer.parseInt(args[1]);

The main() method creates a new object, examObject, from the Exam class. At this point, you are prompted to perform the add () method of the examObject object with the x and y values entered as command line arguments as parameters.

The add () method of the Exam class adds the two numbers received as parameters and returns the result, and the returned value is stored in the sum variable.

Exam examObject = new Exam();
sum = examObject.add(x, y);

After creating the object, use the System.out.println() method for output. Java’s input and output are designed to be independent of the various types of hardware devices, and take the System class out object for standard output and use the println() method.

The println() method takes a string as a parameter and prints it to the screen. In the example, the value of the integer variable sum used in the parameter is automatically converted to a string and output.

System.out.println("The sum of the entered values is " + sum);

Comments are displayed as / * * / or //. / * * / Means to ignore everything between / * and * / and // means to ignore one lines from that mark. In the html file generated by javadoc, it is used as / ** * /.

If you execute the above example at the prompt, you can see the following result.

>> java Examtest 20 40
The sum of the entered values is 60

Java applet code

The Java applet code is slightly different from the Java base code. Java applet code does not have a main () method and can be executed in a network environment by a web browser with a built-in Java virtual machine.

A Java applet is a small, simple application that is executed by calling an applet file (* .class) from HTML code. The Java applet program has to import the library classes used in the program.

import java.applet.*;
import java.awt.*;

public class Example extends Applet {
	public void paint(Graphics g) {
		g.drawString("Hello! It is a Java program!", 100, 60);
	}
}

The applet program must inherit the java.applet.Applet class as shown in the code above.

public class Example extends Applet {
	...
}

The Java applet is inserted into the html code as shown below and executed.

<!DOCTYPE html>
<html>
	<head>
		<title>Java Applet Test</title>
	</head>
	<body>
		<applet code="Example.class" width="400" height="200"></applet>
	</body>
</html>

Share This Post

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

Latest Post