What variables are: that amount may vary
Java is a strongly typed language, each variable must declare its type
Java program variable is the most basic storage unit elements include a variable name, variable type and scope
type varName [=value] [{,varName[=value]}];
Data type variable name = value; commas may be used to declare the plurality of variables of the same type
Precautions:
-
-
Variables must be a valid identifier
-
Variable declaration is a complete sentence, so each statement must end with a semicolon
public class Demo3 { public static void main ( String [] args) { / * int A, B, C; can write, but not recommended write int A =. 1, B = 2, C =. 3; * / / * recommendations Thus said focusing readability int. 1 = A; int B = 2; int. 3 C =; * / String name = "Jiang Tao"; char X = 'X'; Double PI = 3.14; } }
Variable Scope
-
Class variables
-
Instance variables
-
Local variables
Demo4 class {public // class variable static static Double the salary = 2500; // Properties: Variable @ instance variables: subordinate objects; if you do not own initialization, the default value of this type 0.0 0 @ Boolean: the default is to false // addition to the basic type, the rest of the default values are the null (empty) String name; int Age; // main method public static void main (String [] args) { // variables in the process Board , which must be declared and initialized value of int I = 10; System.out.println (I); // variable type variable name Demo4 new new = (); Demo4 demo4 new new Demo4 = (); System.out.println ( demo4.age); System.out.println (demo4.name);
System.out.println (the salary);
// class variable static
} // other methods public void the Add () { System.out.println (); } }
constant
Constant: can not change the value after initialization! Value will not change.
The so-called constants can be understood as a special kind of variable, after his values are set, not allowed to be changed in the course of running
General constant use uppercase characters
static: Static
final: decisive
Demo5 class {public // modifier, there is no order static final double PI = 3.14; // constants can be defined directly referenced public static void main (String [] args) { System.out.println (the PI); } }
* Variable naming conventions
-
All variables, methods, classes name: See name EENOW
-
Class member variables: the first letter lowercase and hump principles: monthSalary
-
Local variables: the first hump principles and lowercase characters
-
Constants: uppercase river picture decline line: MAX_VALUE
-
Class Name: capitalized and hump principle: Man, Good
-
Method name: the first letter lowercase and hump principle: run (), runRun ()
-