Variables for Java applets

Source: Internet
Author: User

1. Variable

As with other high-level languages, programs in the advanced language manipulate the data in memory through variables, so the program should first establish a link between the variable and the memory unit before using any variable, known as defining a variable, or assigning an internal deposit to a variable.

In Java programs, there are two main tasks for defining variables: one is to name your own variable (your own good understanding), and the other is to define the data type of the variable so that the compiler knows how much memory space to give it. Take a look at the following example:

int x;

char c;

float FFFF;

String St1,st2;

Defining variables is fairly straightforward, and three of the following three rules should be noted:

1 identifiers can consist of letters, numbers, underscores, or $ call signs, and there is no limit to length.

2 The identifier must begin with a letter, an underscore, (_), or a character, and the number cannot be placed in the first place.

3 identifiers must be case-sensitive in Java.

Why, I don't know, it's defined by the person who developed the language. Oh! Let me give you a few examples:

A, _istrue, $a 41,a_b,a1, are all legal identifiers, and 123, @isTrue, 0_AB, etc. are not legitimate, will not compile the past, and for the case: Char_class1 and Char_class1 are different identifiers, Because the capitalization in Java is different. When we define variables, of course, we should try to make the variable and its actual representation of the content has a certain connection, which is not seen in the small program, when the program is particularly large, it is useful.

In addition, when defining a variable, we can also give an initial value for the variable, for example:

int x=0;

Char char_class1= ' A ', char_class2= ' 2 ';

Okay, let's 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 ("定义int型的变量x,x的初始值为:"+x);
output2=new Label ("定义char类型的变量,初始值为:"+char_class1);
add(output1);
add(output2);
}
}

Well, give you a detailed analysis of this program!

import java.awt.*; //这个已经说过了,引入类库awt,输入输出类库
import java.applet.*;//同样的道理,引入Applet类库
public class Applet1 extends Applet //定义主类Applet1
{
 int x=10; //定义整数型的变量x并付初值
 char char_class1='A'; //定义字符型变量,并付初值
 Label output1; //定义用来输出结果的两个标签
 Label output2; //这在下一章将给大家详细讲解
 public void init() //小程序的入口,创建标签,显示其内容
 {
  //并加入到主类的Applet1图形界面中,显示出来。
  output1=new Label ("定义int型的变量x,x的初始值为:"+x);
  output2=new Label ("定义char类型的变量,初始值为:"+char_class1);
  add(output1); //显示第一个标签
  add(output2); //显示第二个标签
 }
}

How, the Java variables have a certain understanding of it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.