Constants and variables for Java

Source: Internet
Author: User
Tags naming convention

One, constant

A Java constant is a value that cannot be changed once the value is assigned. Java constants are decorated with the keyword final. General constant names each word has uppercase letters, and multiple words are separated by underscores.

    • Classification of constants
    1. Integer constants: All integer values
    2. Decimal constants: So the numeric value of the floating-point type
    3. Boolean constant: Only two, false and true
    4. Character constants: such as, ' 1 ', ' B ', ' C ' .....
    5. String constants: such as, "123", "Love", "ABDCD" ....
    6. NULL constant: Only one value
    • Four representations of integers
    1. Binary: 0,1 composition, full two in one
    2. Octal: 0~7 composition, full eight in one, starting with 0
    3. Decimal: 0~9 composition, full 10 into a
    4. Hex: 0~9 and ABCdef, full 16 in one, start with 0x

  Note: In Java, if you want a constant to be used in more than one method of a class, these constants are often referred to as class constants.

Second, the variable

In Java, there is a value that can be changed as the program runs, and the value is called a variable. In Java, each variable belongs to a type.

    • Declaration of a variable
    1. The type to which the variable belongs is before the variable name: int A;
    2. The specification of a variable is a naming convention for identifiers
    3. Range of variables: between a pair of {}
    4. Declaration format: Data type variable name = initialization value;
    • Initialization of variables
    1. After declaring a variable, you must initialize the display
    2. Using uninitialized variables, the program will error

Attention:

      • In Java, you can place the declaration of a variable anywhere in the program.
      • The declaration of a variable is as close as possible to where the variable was first used.
      • When defining variables, consider the use period of the variables and release them as early as possible. For example, if a variable needs to be used only in a function, do not declare it as a class variable, etc.

Expand Knowledge Points:

    • Explanation of the variable:

A variable is actually a stored area in memory, and the region has its own name and type, and the region's data can be constantly changed within the same type range.

Java is a strongly typed language with a strict type, with different bytes allocated for each type, so that one type of data cannot be used in other types. A type of memory can only be a type that uses or is smaller than the same type.

Variable name: a name defined to facilitate the use of this memory

Constants and variables for Java

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.