Java Applet Getting Started 4

zhaozj2021-02-11  218

Getting started with Java Applet

YY435

Pacific Network Academy

the next day

Variable

Like other high-level languages, advanced language programs manipulate data in memory by variables, so the program should first establish a connection between the variable and the memory cell before using any variables, which is called defined variables, or called To allocate memory units for variables.

In the Java program, there are two main tasks for defining variables: First, give their own variables (own understanding), second is to define the data type of this variable, so that the compiler knows how much memory space should be given? . Please see the example below:

INT X; Char C; float fff; String ST1, ST2;

Defining variables are quite simple things, three should be aware of the three rules below: 1) identifiers can be composed of letters, numbers, underscores or $ calling, and have no limitations for length, 2) The identifier must be in the letter, underline, (_) Or character starting, the number cannot be placed first. 3) The identifier must be case sensitive in Java.

Why don't you know this, this is the definition of people who develop this language. Ha ha! Give everyone a few examples: A, _ISTRUE, $ A41, A_B, A1, are legal identifiers, and 123, @ istrue, 0_ab, etc. is not legal, it will be compiled, but for the size Write: char_class1 and char_class1 are different identifiers because case in Java is different. When we define variables, we should certainly do a certain connection with the content of its actual representative. This is useful in the applet. When the program is particularly large, it is useful.

In addition, when the variable is defined, we can also give the initial value for this variable, for example: int x = 0; char char_class1 = 'a', char_class2 = '2';

Well, give you a practical example, as follows:

import java.awt *;. import java.applet *;. public class Applet1 extends Applet {int x = 10; char char_class1 = 'A'; Label output1; Label output2; public void init () {output1 = new Label ( " Define the initial value of the INT type X, X is: " x); Output2 = New Label (" Defines the variable of the char type, the initial value is: " char_class1); add (output1); add (output2);} }

Ok, give you a detailed analysis of this program!

Import java.awt. *; // This has already been said, introduced into the class library AWT, input output class library import java.applet. *; // The same reason, introduce Applet class library

Public class Applet1 Extends Applet // Defines the primary class applet1 {int x = 10; // Defines integer type variable X and pays the initial value char char_class1 = 'a'; / / Define the character type variable, and pay the initial value

Label Output1; // Defines two tags label output2 used to output the results. // This will give you a detail in the next chapter to explain the entrance of the public void init () // small program, create a tag, display its content {// Add it to the Applet1 graphical interface of the primary class. Output1 = new label ("Defines the initial value of the INT type variable x, x is:" x); output2 = new label ("Defines the variable of the char type, the initial value is:" char_class1); add (output1); / / Display the first tag add (output2); // Display the second tab}}

How, I have a certain understanding of Java's variables. Look at the next section, Java's data type!

转载请注明原文地址:https://www.9cbs.com/read-4750.html

New Post(0)