Java constants, variables and operators

  Whether the data in the program will be changed during operation based on the data it can be divided into constant and variable.

1. Constant

  Constant is the amount the program can not be changed in Java with final to define constants, indicating that the constant can only be assigned once, requires all caps is customary.

final double CM_PER_INCH = 2.54; // CM_PER_INCH is assigned to the constant 2.54

  Class constants: represented by static final modified constant, indicates the constant using a plurality of methods in a class, if it is declared as public, the other methods may also be used in the class of such constants.

public class Constants{
      public static final double CM_PER_INCH = 2.54;
  
      public static void main(String[] args){
           double paperWidth = 8.5;
           double paperHeight = 11;
           System.out.println("Paper size in centimeters:" + 
                     paperWidth * CM_PER_INCH + "by" + paperHeight * 
                    CM_PER_INCH);  
    }        
}        

  const is a reserved keyword in java for future expansion.

 

2. Variable

  The value of the variable can be assigned multiple times in the program, usually need to declare a variable, you can use the variable initialization variables.

  1. declare variables

  When you declare a variable, you need to declare the data type of a variable, the variable name after the data type, with a location in memory (RAM), respectively. Naming rules Variable names are:

  • The first character must be uppercase letters (AZ), lowercase letters (az), underscore (_), dollar sign ($)
  • The second and subsequent characters of the character, numeric (0-9)
  • Keywords can not be used as variable names

  2. initialize variables

  After declaring the completion of variables, you must use an assignment statement of variable initialization display, or can not use. In general, it is recommended to initialize when you declare a variable, such as:

int a = 10;

  3. instance and class variables and member variables, local variables

  • Member variables: variables defined but in addition to the methods of, the variable can be accessed throughout the class in the class, there is a default initial value, the variable is present in the object heap memory. Such as:
class Clock {public 
    Private int Hour; 
    Private int Minute; 
    Private int Second; // Hour, Minute and Second are member variables, but also for instance variables, class disappears after Clock will release its occupied memory 

    public ShowTime (int hour, int minute, int second) {// ShowTime Clock in the class, the class member variable can be used 
          this.hour = Hour; 
          this.minute = minute; 
          this.second = Second;   
    } 
}

  

  • Local variables: define a method or process parameter, the variable definition method can only be used or definition statement, no default initial value, the variable is present in the stack memory.
void Test public () { 
    int TEMP = 10; // variable TEMP is a partial, only test () which can be used, after the test () method will release completed its share memory 
}
  • Instance variables: no use modified static member variables.
  • Class variables: Also called a static variable, with modified static member variables, methods stored in the area where the static area, other classes can get the value of the variable class by class name + variable name.

3. Operator

  Arithmetic operators: +, -, *, /,%, +, -.

  Relational operators:!>, <,> =, <=, ==, = relational expression is always boolean.

  Logical operators: &&, || ,!

  Bitwise operators: The principle is the operand into a binary value, and for every operation in binary. Operation types & (bitwise and), | (bitwise or) ^ (bitwise exclusive or) ~ (bitwise), << (bitwise left shift), >> (bitwise right shift),> >> (bitwise right padded with zeros).

  Assignment operator: =, + =, - =, * =, / =, (%) =, << =, >> =, & =, | =, ^ =

  Conditional operator: Analyzing the Boolean expressions to determine the value assigned to the variable. The format is:

    X variable = ( expression The ) ? value IF to true : value IF to false type of comparison operator instanceof: Check if the object is of a particular type (class type or interface type). Format:     
  
   
( Object Reference variable ) instanceof ( class / interface of the type )
  These types of priority:
  
brackets ()、[]
Unary (right to left) !、+、~、++、--
Arithmetic operators *、/、%、+、-、<<、>>、>>>
Relational Operators <、<=、>、>=、instanceof、==、!=
Bitwise Operators &、^、|
Logical Operators &&、||
Conditional operator ?=
Assignment operator (right to left) =、+=、-=、*=、/=、%=、&=、|=、^=

Guess you like

Origin www.cnblogs.com/thwyc/p/12233290.html