Getting the basics of Java variables

1, a variable
refers to a memory space in the computer memory, is the basic unit for storing data

Example:
Hotel - Memory
Hotel rooms each room contains the equivalent of a variable is
the room number - variable name
variable's data type - the type of room
guest room - variable values

2, the use of variable
declarations: variable name data type; for example: int money;
the assignment: variable name = value; for example:money=100;//将数值赋给money

3, declare variables
defined variables method:

  1. First statement, then assignment: data type variable name; variable name = value;
  2. Statement and assignment: data type variable name = value; // unassigned variables can not be used
  3. A plurality of variable declaration and the same type of assignment: data type variable 1, variable 2, the value of the variable 3 = 3, 4 variable, the variable value 5 = 5;
    the PS: the Java is a strongly typed language, a variable type must match the type of the value

4, the data type of
the data type into basic data types and the reference data type

  1. Basic data types: integer, decimal, character, Boolean
    ① integer
    The range of integers
    byte (byte) 1Byte = 8bit Type 2 can store a total of 8 ^ 256 = an integer of 000 million to 127 plus 128 1111
    1111 128 128 -1 to negative number 1 represents a high negative zero positive sign bit byte data type of data indicated range: -128 to 127
    For type long, to be added later l or L

    ② decimal, floating-point
    Here Insert Picture Description
    decimal / float: using scientific notation
    2E3 equivalent to 2 10. 3 ^ = 2000.0;
    3E5 equivalent. 3
    10. 5 ^ = 300000.0;
    Double float to the default type, To is float type assignment, the need to add F / f behind literal assigned For double type can be added D / d after the literal;

    ③ char
    char 2 bytes 0-65535 Unicode encoding (Unicode)
    the Unicode character set support ASCII code, wherein each character corresponds to a decimal integer;
    character assignment: char c1 = 'A';
    is an integer assignment: char c2 = 65;
    for the band assignment: char c3 = '\ u0041' ;
    escape character: \ n newline \ t indentation (tab stop) \ backslash 'single quotation marks "double quotes

    ④ boolean
    boolean 1 byte ture / false

  2. Reference data types: string, array, objects, interfaces, classes
    string: String Any "" literal sequence of Unicode characters between the
    value of the method: String str1 = "hello";

5, conversion type
automatic type conversions: Two types compatible with each other and greater than the target type source type
byte-> SHORT-> INT-> Long
float-> Double
char can be converted to Short
char-> int
INT-> a float / Long
casts conversion: Two types compatible with each other and less than the target type source type

6, the expression

Use variables or literal operator connected, and a final result can be obtained.
Automatic lifting Type: two operands is a double, the calculation result is automatically elevated to Double; if not double, a float, the upgraded flaot; if not float, there is a long, to improve the Long; if the operation the number is not long, there is a int, then promoted to int; if not int, are short or byte, still promoted to int;

7, the operator

  1. Arithmetic operator (two operands operation)
    + Add, summing
    - Save, difference
    * multiplication, quadrature
    / in addition, quotient
    % modulus, modulo
    ++ increments the value of the variable +1
    - decrements the value of the variable Save 1
    ++ / - to be used after the variable x, + 1 after the assignment operator (the first use, after +/- 1);

  2. Assignment operator
    = direct assignment
    + = summed after the assignment
    - assignment after differencing =
    * = Quadrature after assignment
    / assignments = quotient after
    the assignment% = Remainder;

  3. Relational operators
    greater than>
    Less than <
    greater than or equal to> =
    Less than or equal <=
    equal ==
    not equal! =

  4. Logical operators
    && with (and) two data operands is true, the result is true;
    || or (or) has two operands is true, the result is true;
    ! NOT (negation) meaning "not" , true or false, false is truth;

  5. Logical Operators shorting
    && with (and) short-circuited, when the left side of the expression is false, the right is not calculated, result is false;
    || or (or) or short-circuited, the left expression is true, the right is not calculated side, result is true;
    & with (and) a non-short-circuited, bitwise aND operator
    | or (or) the non-short circuit or, bitwise oR operator

  6. Ternary operator
    first determines and then obtain corresponding results
    :? @ Semantics: boolean expression? Results 1: 2 results when the expression is true, the result obtained result; 2 results obtained;

  7. Bitwise operators: two operands based on the calculated bit binary number

     &     位与    非短路与。两个数位,同时为1,结果为1
     |     位或    非短路或。两个数位,有一个为1,结果为1
     ^     异或     对应位上的数,相同为0,不同为1;
     ~     求反    按位求反1变0,0变1; 	
     >>	右移(带符号)    按位右移1位(高位补原符号位);
     <<   左移    按位 左移1位;
    >>>  右移(无符号)    按位 右移1位(高位补0);`
    

8, the console (keyboard) input

Program is running, the console may (terminal) manual data entry, let the program continues.

java.util.Scanner input = new java.util.Scanner(System.in);

Guide package syntax: import 包名.类名;// will introduce an external class file functionality into its own file
writing position: packageafter (if any), outside class, how can the sentence.
Each class has a defaultimport java.lang.*;

package *;
//引入Scanner包后
import java.util.Scanner;
//或者可以写为:import java.util.*;
public class * {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	}
}

Order of use:
1, into java.util.Scanner;
two, a variable declared Scanner type Scanner sc = new Scanner(System.in);
3, using the Scanner function corresponding to the class (type distinction):
sc.nextInt();// get integer
sc.nextDouble();// obtained decimal
sc.next();//sc.nextLine (); obtaining the string
sc.next().charAt(0);// obtain a single the first letter of characters that the string

Released four original articles · won praise 3 · Views 181

Guess you like

Origin blog.csdn.net/qq_44664231/article/details/104648651