1. Variables: The number of stored, pronouns, refers to the number of its existence
1) Declaration of variables
int a;//declares an integer variable named a
int b,c,d;//declares three shaping variables, named B,c,d
2) naming
2.1) can only contain letters, numbers, _ and $ characters, and cannot start with a number
2.2) strictly case-sensitive
2.3) cannot use keywords
2.4) can be named in Chinese, but it is not recommended to suggest "see the name of the known" Hump name method
3) Initialization: First time assignment
3.1) Simultaneous initialization of the declaration:
int a = 250;//declares shaping variable A and assigns a value of 250
3.2) First declaration after initialization:
int A;//declare reshape variable a
A = 250;//assigns a value of 250 to variable a
4) Use:
4.1) must match the data type
int a = 5.678;//compilation error, data type mismatch
4.2) The operation of the variable is the operation of the number it is stored in.
int a = 5;//declares shaping variable A and assigns a value of 5
int b =a+10;//take out the value of a of 5, plus 10, and then assign a value to a
A = a+10;//Remove the value of a of 5, plus 10, and then assign a value to a
System.out.println (a);//value of output a 15
System.out.println ("a");//a, output as-is
4.3) variables must be declared and initialized before they are used
System.out.println (m);//Compilation error, m not declared
int m;
System.out.println (m);//Compilation error, m not initialized
int m = 250;
System.out.println (m);//output M value 250
2 Basic data types
1) Int: Shaping, 4 bytes,-21 billion to 21 multi-billion
1.1) Integer Direct quantity defaults to int type, cannot be over range, otherwise compile error
1.2) divide two integers, the result is an integer, the decimal place is unconditionally discarded
1.3) Overflow occurs when the operation is out of range, and should be avoided when overflow
2) Long: Length, 8 bytes, very large very large
2.1) The direct amount of the long integer is after the number plus L or l
2.2) arithmetic operation may be over-range, it is recommended to add L after the 1th number
2.3) System.currenttimemillis () to get the number of milliseconds that have elapsed from 1970.1.1 0 o'clock to this moment
3) Double: float type, 8 bytes, very large
3.1) floating-point direct volume by default is double, indicating that float type needs F or f
3.2) Double type data operation, rounding error may occur
4) Char: Character type, 2 bytes
4.1) with Unicode encoding, each character (char) has a corresponding code (int)
The representation is a char character, which is essentially an int code
4.2) characters must be enclosed in single quotes, with only one
4.3) Special symbols need to be escaped through \
5) Boolean: Boolean, 1-byte
5.1) Only values are true and false
3. Conversions between basic types
Type from small to large: byte,short,int,long,float,double
Char
1) Two kinds of ways
1.1) Automatic type conversion: From small type to large type
1.2) Coercion of type conversions: from large types to small types
Strong turns have the potential to overflow and loss of precision
2) Two-point rule:
2.1) The direct amount of the shaping can be directly assigned to the Byte,short,char, but not beyond the range
2.2) Byte,short,char, type variables participate in the operation, the first will be converted to int re-operation
Usage of 4.Scanner
1) under the package:
Import Java.util.Scanner;
2) in the Main method:
Scanner scan = new Scanner (system.in);
3) below the 2nd step
System.out.println ("Please enter Age:");
int age = Scan.nextint ();
System.out.println ("Please enter the Unit price:");
Double price = Scan.nextdouble ();
Task:
1. Create the DAY03 package and complete the following in the DAY03 package:
1) Create Class age,
Requirement: Receive user input age (int), and output
2) Create Class Leapyear,
Requirement: Receives the year (int) entered by the user and outputs
3) Create Class Scorelevel,
Requirements: Receive user input of the score score (int), and output
4) Create Class Commandbyswitch
Requirements: Receive command commands (int) entered by the user, and output
5. Assign a value to a variable in three ways:
1) Assign a value to a fixed value:
int a = 5;
2) accept a value entered by the user
int a =?
3 The system randomly generates a value:
int a =? ;
6.
BYTE b1=5;
BYTE b2=6;
byte b3= (Byte) (B1+B2);
7.
Enter, TAB----------control character-------need to be escaped
Char c1= ';
8.
Char c1= ' A ';
SYSTEM.OUT.PRINTLN (C1); A
Char c2=65;
System.out.println (C2); A
Character Char, essentially a code int
SYSTEM.OUT.PRINTLN (+); 4
System.out.println (2 '); 52
System.out.println (' 2 ' + ' 2 '); 100
' 0 '--48
Character-----A
Yards-----between 0 and 65535
ASCII code:
' A '--97, ' a '--65, ' 0 '--48
char int
Enemy-------------1234
Military-------------5678
To-------------4321
Attack-------------8765
A character (char) is a form of expression, essentially its corresponding code (int)
Datatypedemo--------Hump Naming method
Datatypedemo--------Pascal Nomenclature (class name)
int a=10000000000; Compilation error (direct volume range)
int a=1000000000*10; Overflow (over-range during operation)
Operation may be outside the int range, after the 1th number plus L
Long a = 1000000000*2*10l;
System.out.println (a); 20 billion
Long B = 1000000000*3*10l;
System.out.println (b); Not 30 billion.
Long C = 1000000000l*3*10;
System.out.println (c); 30 billion
int balance = 2147483647;
balance=balance+1; -2147483648
1g=1024m (MB)
1M=1024KB (Kbytes)
1kb=1024b (bytes)
1b=8bit (BIT)
1g=1024*1024*1024----1073741824
Panax Notoginseng------Int,long
Recommended Choice int------4 bytes
Suffice, because each type takes up a different amount of memory space
There are three types of annotations:
1. Single-line Comment://
2. Multiline Comment:/* */
3. Documentation Notes:
Java Foundation the next day