Home > Java > javaTutorial > body text

Memo for beginners to learn Java (2)

黄舟
Release: 2016-12-20 13:46:54
Original
1315 people have browsed it


Getting started with Java is much simpler than I thought. So far, I have an understanding of the basic syntax structure of Java. But I know that in-depth study of any language requires time and accumulation of 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. Applications such as C programs start running from the main() main program, while Applets It’s complicated. I don’t know exactly how complicated it is, but I’ll figure it out gradually. An important property about Applet is that I can pass the value in Html as a parameter 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 parameter values ​​of the HTML. 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 , 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(): The system finishes calling init() After that, start() will be called automatically, and this method will be called every time the current window is reactivated, 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 does not pay 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 an Applet version of HelloWorld
File name: 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 in the program There is no main function, so how does it run? Since the Applet runs in the browser environment, we need to call it in the HTML file. The relevant tag that needs to be used is the tag. We first create HTML file test.htm, the source code is as follows.


Here comes my first applet:



< ;/applet>




Put this file in the same directory as HelloWorld.java, and then after compiling HelloWorld.java, click test.htm directly Open it and you can see that the Applet program has been started. Or you can use the AppletViewer command AppletViewer test.htm to run the Applet directly without a browser.

The following program can better help us understand the entire life cycle of Java Applets. How to call the methods we introduced above.

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. (This program can refer to the "Java Concise Tutorial" of the Machinery Industry)

Unlike the C language, it is much easier to use Java to implement GUI. Because 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 It 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, how can I make it Should the elements be placed in the interface according to my requirements? When there are not many elements, you can let Java automatically layout (Layout). But when there are many elements, or when elements need to be placed according to the requirements of the application, you need to use panels (Panel). ). Panels also have corresponding components (TPanel) in Delphi, but they are mainly used to segment the interface and perform rough layout. The precise positioning requires manual adjustment by the developer. In Java, Panel can only be used for positioning, and cannot be Not to mention it's a flaw. Maybe it's because I haven't learned it yet.

After getting started, it’s time to delve into the concept of objects.
Suppose a custom data type called Date is created in Java as follows
public class Date{
int day;
int month;
int year;
}
Then for the following three statements declaring variables, java is assigning them Is there any difference when using memory?
(1) int i;
(2) Date mybirth;
(3) Date mybirth=new Date();

Obviously there is, and the allocation is as follows:
(1) Java automatically allocates integer variables to integer i Memory, usually two bytes
(2) Java declares an instance variable mybirth of the Date class and allocates storage space for it, but what is stored in this storage space is only a reference, or an address. The current If there is nothing in the address, then we cannot use this instance variable or reference 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, the first address to access each member of this instance variable, such as mybirth.day, mybirth.month, mybirth.year.
When we declare a variable of 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 happening? This is because both String and user-defined variables belong to the category of classes. A declaration as a class The variable is no longer a 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.
Go 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();//Defines an instance variable u of University class, and allocates object storage space for it
University u2=u;//Another instance variable u2 is defined, and the value of u is assigned to u2
So obviously Except for their different names, u2 and u are actually the same thing, 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.

The above is the content of the memo (2) for beginners learning Java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!