Definition concepts and naming rules of java variables

Source: Internet
Author: User

Variable refers to the amount of variable values that can be changed during program execution. In the data area of the memory, a storage space is allocated for the variable to store the variable value. The address of the memory space corresponds to the variable name, therefore, you can use variable names to differentiate and use the memory space in the program.

The syntax for declaring a variable in Java is as follows:
Type Identifier
Type indicates the data type, and identifier indicates the variable name. For example:
Int I; // declare an int-type variable to store int-type integers.
Float f; // declare a float variable to store float floating-point numbers.
Char c; // declare a char variable to store characters
Boolean B; // declare a boolen variable to store boolean values.
If multiple variables of the same type are declared, they can be separated by commas. The declaration syntax is as follows:
Int a, B, c;
Float f1, f2, f3;
When declaring a variable, the identifiers are not randomly selected and must follow the naming rules of the variable. Otherwise, the variable name is invalid. The following describes the naming rules for variables.
Initialize a dynamic variable of the instance.

Public class MainClass {
Public static void main (String args []) {
Double a = 3.0, B = 4.0;

// C is dynamically initialized
Double c = Math. sqrt (a * a + B * B );

System. out. println ("Hypotenuse is" + c );
  }
}

1. The variable name must be a valid identifier.
A valid identifier in Java, usually consisting of uppercase and lowercase letters, numbers, underscores (_), and dollar signs ($). For more information about identifiers, see

Public class MainClass {
Public static void main (String args []) {
Int x;

For (x = 0; x <3; x ++ ){
Int y =-1; // y is initialized each time block is entered
System. out. println ("y is:" + y); // this always prints-1
Y = 100;
System. out. println ("y is now:" + y );
    }
  }
}


Section 2.5. For example:

Int num1, num2;
Float bookprice _ $;
String userName;


The following variables are invalid:
Int 1num, 2num; // invalid variable name, cannot begin with a number
Float book-price; // invalid variable name, containing invalid characters
Note: In Java, Chinese and other languages can be used as variable names. For example, "int age = 24" is legal, however, we recommend that you do not use these languages as variable names.
2. Multiple identical variable names cannot be declared
In the same valid range, Duplicate variable names are not allowed. The website source code is as follows:

Public static void main (String [] args ){
Int num1, num2;
Float num1; // compilation error. The local variable num1 is repeated.
Char ch, ch; // compilation error. The local variable ch is repeated.
}

Instance

Public class MainClass
{
Static String helloMessage;

Public static void main (String [] args)
    {
HelloMessage = "Hello, World! ";
System. out. println (helloMessage );
    }
}

It may not be stated at the beginning of the program

Public class MainClass
{
Public static void main (String [] args)
    {
HelloMessage = "Hello, World! ";
System. out. println (helloMessage );
    }

Static String helloMessage;
}


The identifiers in Java are case-sensitive, so the following variable names are valid:
Int num, Num; // declares two different variables
3. The variable name cannot be named as a Java keyword.
Keywords are reserved for special purposes in Java. They cannot be used as variable names when declaring Variables. The keywords in Java are shown in Table 1.

Table 1 Java keywords
4. Select meaningful variable names
A valid variable name can be named based on the above rules.

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.