[Brick Java] Variables and Constants

  • content

    What is a variable?

    Details of variables

    variable concept

    How to use variables

    constant

    what is a constant

    Notes on the application of constants in Java


  • What is a variable?

  • A variable is the basic unit of a program.
  • Details of variables

  • In java, every variable has a type. When declaring a variable, the type of the variable comes before the variable name.
  • data type variable name = initialization value;
  • Give chestnuts: double money = 10.0;
  • Note: The format is fixed, remember the format so that it can be changed.
  • In the above example, the data type is double type, money is the variable name, and the initialization value is equal to 10.0.
  • The variable name must be a sequence of letters or numbers starting with a letter.
  • There is basically no limit to the length of variable names.
  • You cannot use reserved words as variable names in Java. (java reserved words are strings that have special meaning to the Java compiler), multiple variables can be declared in one line.
  • A variable represents a storage area in memory [different variable types are different, and the size of the space occupied is different, for example: int 4 bytes, double is 8 bytes]
  • Variables must be declared first and then used, that is, there is an order.
  • Variables cannot have the same name within the same scope.
  • variable concept

  • A variable is equivalent to a representation of a data storage space in memory. You can think of a variable as the ratio number of a courier shelf. We can find our courier through the number, and the variable (value) can be accessed through the variable name.
  • How to use variables

  • (1) declare variables
  • int a;
  • (2) Assignment
  • a = 20;//It can be said that assign 60 to a
  • (3) Use System.out.println(a);
  • constant

  • what is a constant

  • Java constants are values ​​that are fixed in the program and cannot be changed. For example "a", float 6.6, etc. In Java, constants include integer constants, floating point constants, boolean constants, character constants, etc.
  • Syntax of constants
  • final constant type constant name [= initial value];
  • Example of using constants in java
  • final int A=0,B;//Define a constant a and b of type int, and initialize the constant a with a value of 0
  • B = 1; //Subsequent assignment to constant B.
  • Application of character constants
  • Each character constant corresponds to a Unicode code
  • Notes on the application of constants in Java

  • 1. Exponential form
  • For example: 12e2 or 12E2 means 12*102
  • 1.234e1 means 1.234*101
  • 2. 1 and 1.0 are two different types of numbers, 1 is an integer value, and 1.0 is a double-precision floating-point type.
  • 3. '6' and 6 and "6" in Java are three different constants.
  • '6' is a character constant; 6 is an integer constant; "6" is a string constant

 

Guess you like

Origin blog.csdn.net/m0_62069409/article/details/124443151