2018-04-11
Variables and constants
One, constant
1. Concept
fixed constant values in the program
2. Classification
1) Literal constants
A direct quantity that represents a direct given value (which can be integers, decimals, ture, false, and so on)
For example: integer value constant, 3.14, 2.25, Boolean constant Ture,false, etc.
2) Final modified variables
Second, the variable
1. Concept
Represents a storage space used to hold constants of a certain type, with no fixed value, can be reused, or can be used to store unknown data of some kind
2. Features
- Occupy a piece of an area in memory
- The zone has its own name (variable name) and type (data type)
- can be reused
- Data in this region can be constantly changed in the same type
3 . Defining Variables
1) syntax
Data type variable name
such as: int age;
2) assigning values to variables (initializing)
Variable name = constant value of the declared variable type
Age = 17;
You can also initialize variables when declaring variables: int age = 18;
Note: variables must be initialized (assigned) before they can be used, and initialization is the true memory allocation.
Iii. Classification of variables-scope-usage rules
1. Classification of variables
Variables fall into two categories depending on the location of the definition
1) member variables/fields: variables directly defined in the class {} (outside the method)
2) Environment variables: variables other than member variables, variables defined in the method
Note: variable access is the nearest principle
2. Scope of variables: areas that can work
1) member variable/field: function in the defined class
2) Environment variables: From the definition of the place to the close immediately after the end of the
Iv. Java expressions
First, the Java Language Foundation (2) _ Types and operations--literals and constants