Getting started with Java is much simpler than I thought. So far, I already have an understanding of the basic syntax structure of Java. But I know that in-depth study of any language requires the accumulation of time and practice.
Applet is a code written in Java that can be run on the browser side. The obvious difference between it and an application lies in its execution method. Application For example, the C program starts running from the main() program, while the Applet is more complicated. I don't know exactly how complicated it is, but I will gradually understand it. An important property about Applet is that I can pass values in HTML as parameters to Applet (get the parameter value through getParameter()). In this way, in order to produce different effects, we do not need to recompile the Java program, but just Just modify the HTML parameter values. Since the HTML code can also be dynamically generated, I can control the dynamic effects of the web page as I wish.
There are three main methods in the Applet life cycle: init, start, and stop.
init(): Responsible for the initialization of the Applet. This method is only executed once during the entire Applet life cycle. It is the same as the OnCreate() event in Delphi.
start(): After the system calls init(), it will automatically Call start(), and each time the current window is reactivated, this method will be called, which is similar to the OnShow() event in Delphi.
stop(): This method is called after the user leaves the page where the Applet is located. It allows you to stop the work of some resources when the user is not paying attention to the Applet so as not to affect the system operating efficiency. And we do not need to artificially remove this method. This method is similar to the OnClose() event in Delphi.
The following is the file name of an Applet version of HelloWorld
: HelloWorld.java
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet
{
String title;
public void init(){
title="Hello World";
}
public void paint(Graphics g)
{
g.drawString(title, 50, 20);
}
}
We can see that there is no main function in the program, so how does it run? Since the Applet runs in the browser environment, we need to call it in the HTML file. The relevant tags that need to be used are <Applet> tag, we first create the HTML file test.htm, the source code is as follows:
<html>
<body>
Here comes my first applet:
<br>
<applet code=HelloWorld.class width=650 height=500>
</applet>
</APPLET>
</body>
</html>
Put this file in the same directory as HelloWorld.java, then compile HelloWorld.java, click test.htm to open it directly, and you can see that the Applet program is started. Or use the AppletViewer command AppletViewer test.htm You can also run the Applet directly without a browser.
The following program can better help us understand how Java Applet calls the methods we introduced above throughout its life cycle.
File name: StartStop.java
import java.awt.*;
import java.applet.*;
public class StartStop extends Applet
{
StringBuffer message;
public void init()
{
message=new StringBuffer("Init done...");
}
public void start()
{
message.append("Started...");
}
public void stop()
{
message.append("Stopped...");
}
public void paint(Graphics g)
{
g.drawString(message.toString(), 150, 50);
}
}
The operation method is the same as above. (For this program, please refer to the <Java Concise Tutorial> of the Machinery Industry)
Unlike the C language, it is much easier to implement GUI using Java. Since it is a purely object-oriented language, Java's AWT provides various interface elements for us to call, just like the components in Delphi. The following is a comparison table of GUI objects in Java and corresponding components in Delphi.
Java Delphi
Button TButton
Canvas TCanvas
Checkbox TCheckbox
CheckboxGroup TRadioGroup
Choice TComboBox
Label TLabel
TextField TEdit
TextArea TMemo
However, JDK is not a visual RAD (rapid application development) development tool. The use of objects cannot only be done by dragging and shifting like Delphi, but requires us to write calling code. This creates a trouble, I How can I make the elements placed in the interface according to my requirements? When there are not many elements, I can let Java automatically layout (Layout). But when there are many elements, or when the elements need to be placed according to the requirements of the application, you need to Use a panel (Panel). The panel also has corresponding components (TPanel) in Delphi, but it is mainly used to segment the interface and make a rough layout. The precise positioning requires manual adjustment by the developer. In Java, it can only be used Panel is used to position it, which is a flaw. Maybe I haven’t learned it yet.
After getting started, it's time to dive into the concept of objects.
Assume that a custom data type called Date is created in Java as follows
public class Date{
int day;
int month;
int year;
}
So for the following three statements that declare variables, is there any difference when Java allocates memory for them?
(1) int i;
(2) Date mybirth;
(3) Date mybirth=new Date();
Obviously there is, and the allocation is as follows:
(1) Java automatically allocates the memory of an integer variable for the integer i, usually two bytes
(2) Java declares a Date class The instance variable mybirth is allocated with storage space, but what is stored in this storage space is only a reference, or an address. There is nothing in the current address, so we cannot use this instance variable or reference it. its members.
(3) Java creates an instance variable mybirth of the Date class, allocates enough storage space for its member variables, and finally returns a reference to this storage space, that is, returns the first address of this storage space, and then passes mybirth , that is, this first address is used to access each member of this instance variable, such as mybirth.day, mybirth.month, mybirth.year.
When we declare a variable of a basic data type (such as boolean, byte, short, char, int, long, flat, double), the system will automatically allocate memory for the variable. But if String or user-defined variables are declared, the system will not allocate memory for them immediately. Why is this?
This is because both String and user-defined variables belong to the category of classes. A declared as a class The variable is no longer a piece of data, but a reference to the data. That is to say, mybirth can be regarded as a pointer to an instance of the class, and the address is stored in it. This is easy to understand.
Going deeper, since the instance variable value of a class is a pointer, and this pointer points to an instance of a class, then we can obviously define instance variables of multiple classes with different names, and point them all to one instance. For example:
University u=new University();//An instance variable u of the University class is defined, and the storage space of the object is allocated to it.
University u2=u;//An instance variable u2 is also defined, and the value of u is assigned to it. Given u2
, then obviously u2 and u are actually the same thing except for their different names, because they point to the same address.
I think it is still important to clarify this point. These data structures are what a programmer needs to know.