Java variables and constant declarations

Source: Internet
Author: User

One, variable

1. Definition of variables

variable is a storage area in memory that has its own name (variable name) and type (data type), each variable in Java must first be declared, and then the data used in that region can be changed within the same type range

2. Classification of variables

1) by Data type

Defines specific data types for each type of data, allocating different sizes of memory space in memory

Data types are divided into basic data Types and reference data types, reference data types have classes (class), interfaces (interface), arrays, basic data types have Boolean (Boolean), character (char), numeric " Integer type (byte, short, int, long), floating-point type (float, double) "

Note: Boolean type data only allows values of true and false, no null

2) According to the location of the declaration

A variable declared within a class is called a member variable outside the method body, and a variable declared inside a method body is called a local variable

3. Declaration format

Define the format of the variable: data type variable name = Initialize value

Variable in-memory area:

Code Instance

Package Com.yyx.pratice;public class Javapratice {public    static void main (string[] args) {        // When declaring a long type of data, an ' l ' or ' l ' long lon=54365465346765768l is added to the end of the range of int values        ;        Floating-point data declared as float type, with ' F ' or ' F ' float flo=34.5f at the end        ;                Boolean blo=true; System.out.println (LON); System.out.println (FLO); System.out.println (Blo); }}

Second, constant

A constant is a special variable whose value is set and is not allowed to change in the course of a program's operation, usually with uppercase characters

syntax: final constant name = value; For example: final String love = ' java ';

Note: it must be initialized at the time of the constant declaration, or a compilation error will occur. Constants cannot be changed once they are initialized

Three representations of character constants:

1. character constants are single characters enclosed in single quotation marks ("'), covering all written words in the world. For example: char c1 = ' a '; char C2 = ' Medium '; char C3 = ' 9 ';

The escape character ' \ ' is also allowed to be used in 2.Java to convert subsequent characters to special-character constants. For example: char c3 = ' \ n '; ' \ n ' indicates a newline character.

3. Use Unicode values directly to represent the character type constants: ' \uxxxx '. where xxxx represents a hexadecimal integer. such as: \u000a means \ n

Java variables and constant declarations

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.