Declaration, initialization, and scope of 3.Java variables

Source: Internet
Author: User

I. Declaration of a Java variable

In Java programming, each declared variable must be assigned a type. When declaring a variable, you should declare the type of the variable and then declare the name of the variable. The following shows how variables are declared.
Double salary;
int age;
Boolean op;
The first item is called the variable type, and the second item is called the variable name. The semicolon is required, which is the end symbol for the Java statement.

Different variables of the same type can be declared on a single line, or in different rows, if you want to declare in the same row, separate variables separated by commas, such as the following example.
int studentnumber,people;

You can assign a value to a variable while declaring it, or you can declare it later. such as:
int a=1; Assigning values when declaring
int A;    A=1; Assign value after declaration

Note: In the process of running the program, the value in the space is changed, and this memory space is called a variable. For ease of operation, a name is given to the space, called the variable name, and the value in the memory space is the value of the variable. Therefore, the application of memory space, the variable does not necessarily have a value, to think that the variable has a value, you must put the value.

For example: "int x"; The variable is defined but not assigned, i.e. the memory space is applied, but the value is not put; int x=5; Not only did you apply for memory space but also put a value of 5.

Note: There are no assigned variables, the system is initialized by the following default values.

Data Type Initial value
Byte 0
Short 0
Int 0
Long 0L
Char ' \u0000 '
Float 0.0f
Double 0
Boolean False
All reference types Null (does not reference any object)
Two. Scope of variables (global variables | local variables)

Depending on the scope (scope), the variables are generally divided into global variables and local variables. Literally, a global variable is a variable that works within the scope of a program, and a local variable is valid within a part of the program.

In Java, a global variable is a variable that is valid within the entire scope of the class. The local variable is in the class within a method function or within a subclass, valid variables, the following will be from the actual program code slowly experience.

1. Examples of global variables
public class var{///a is a global variable
int a=10;
public static void Main (string[] args) {
var v=new var ();
V.print ();
}
void print () {
SYSTEM.OUT.PRINTLN ("Global variable A=" +a);
}
}
Operation Result:
Global variable a=10

As can be seen from the above example, the value of the variable "a" is valid throughout the class.

2. Examples of local variables
public class math1{///c is a local variable
public static void Main (string[] args) {
Math1 v=new Math1 ();
System.out.println ("This is a local variable c=" +c);
}
void print () {
int c=20;
}
}
When the above code is compiled, an error occurs, that is, the variable "C" is not found. This indicates that the variable "C" only works in the method "print ()" and cannot be called outside of the method.

as can be seen from the above code, if a variable is defined in a class, then the variable is a global variable, whereas a variable defined in a method or function in a class is a local variable.

Three. Declaration of global variables and local variables

public class var{
byte x;    Short y;    int z;    Long A; float B;
Double C;    Char D; Boolean e;
public static void Main (string[] args) {
var m=new var ();
SYSTEM.OUT.PRINTLN ("Print data x=" +m.x);
SYSTEM.OUT.PRINTLN ("Print data y=" +m.y);
SYSTEM.OUT.PRINTLN ("Print data z=" +m.z);
SYSTEM.OUT.PRINTLN ("Print data a=" +M.A);
SYSTEM.OUT.PRINTLN ("Print data b=" +M.B);
SYSTEM.OUT.PRINTLN ("Print data c=" +M.C);
SYSTEM.OUT.PRINTLN ("Print data d=" +M.D);
SYSTEM.OUT.PRINTLN ("Print data e=" +M.E);
}
}
Operation Result:
Print Data x=0
Print Data y=0
Print Data z=0
Print Data a=0
Print Data b=0.0
Print Data c=0.0
Print Data d=
Print Data E=false

as can be seen from the above example, as a global variable without initialization, the system automatically assigns a value to the variable. Except that the character data is assigned NULL, the Boolean data is assigned a value of false, and all others are assigned a value of 0. Let's look at a section of program code below.

public class var1{
void Printnumber () {
byte x;    Short y;    int z; Long A;
float B;    Double C;    Char D; Boolean e;
}
public static void Main (string[] args) {
Var1 m=new var1 ();
SYSTEM.OUT.PRINTLN ("Print data x=" +m.x);
SYSTEM.OUT.PRINTLN ("Print data y=" +m.y);
SYSTEM.OUT.PRINTLN ("Print data z=" +m.z);
SYSTEM.OUT.PRINTLN ("Print data a=" +M.A);
SYSTEM.OUT.PRINTLN ("Print data b=" +M.B);
SYSTEM.OUT.PRINTLN ("Print data c=" +M.C);
SYSTEM.OUT.PRINTLN ("Print data d=" +M.D);
SYSTEM.OUT.PRINTLN ("Print data e=" +M.E);
}
)

This program is compiled with an error, because all local variables are not initialized. From the above two program code to derive a result: global variables can not be initialized assignment work, and local variables must be initialized assignment work.

Declaration, initialization, and scope of 3.Java variables

Related Article

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.