<>JAVA Basic grammar

* notes , identifier , keyword
* data type
* Type conversion
* variable , constant
<> notes

* Usually we write code , When the amount of code is small , We can also read what we write , But when the project structure becomes complicated , We need to use annotations .
* Comments are not executed , It's for our code writers
* Writing notes is a very good habit
* We must pay attention to the standard when writing code .
<>java There are three types of annotations in :

*
Single-Line Comments //

*
multiline comment /* */

*
Documentation Comments /** * */

<> identifier

<>* keyword

* Keywords cannot have names

<>JAVA All components need names , Class name , Variable names and method names are called identifiers

1. Class name ; 2. Method name ; 3. Variable name ;

<> Identifier considerations

* All identifiers should be alphabetic (A-Z perhaps a-z), Dollar sign ($), Or underline (_) start ;" Case sensitive “
* The first letter can be followed by a letter (A-Z perhaps a-z), Dollar sign ($), Or underline (_) Or any character combination of numbers ;“ Special symbols cannot be used ”
* You cannot use keywords as variable or method names .
* Examples of legal identifiers :age, $salary, _value, __1_value
* Example of illegal identifier :123abc, -salary, #abc
* It can be named in Chinese , However, it is generally not recommended to use it in this way , Pinyin is also not recommended , very Low
<> data type

<> Strongly typed languages

* It is required that the use of variables should strictly comply with the regulations , All variables must be fixed before they can be used ; High security , But it's slow .
<> Weakly typed language

<>Java The data types of are divided into two categories

*
Basic types (primitive type)

integer
byte 1 byte 8 position java Range 2(7)-1 reach -2(7)
short 2 byte Rarely used 2(15)-1 reach -2(15)
int 4 byte Most commonly used 2(31)-1 reach -2(31)
long 8 byte Long plastic surgery 2(63)-1 reach -2(63)

decimal
float 4 byte
double 8 byte

other
char 2 byte No sign 2(15)-1
boolean 1 byte On behalf of truth true or false false

*
reference type (reference type)

String character string
String s = " abccccd "; System.out.println(s.charAt(2));// Display page 2 Coordinate symbol System.out.
println(s.indexOf("a"));// display "a" Coordinate position System.out.println(s.length());// Display coordinate length
System.out.println(s.equals("ccdda"));// Is the comparison correct true or false System.out.println(s
.compareTo("ABCD"));// Compare size a<b, Returns a positive value , Otherwise, the opposite is true . Equal to 0. System.out.println(s.toLowerCase()
);// Change to lowercase "abcd" System.out.println(s.toUpperCase());// Capitalization "ABCD" System.out.
println(s.trim());// When spaces exist " abcd ", Remove Spaces . System.out.println(s.split("b"));
// Cut from the designated position

<> What is a byte

*
position (bit): It's a computer internal data Minimum unit of storage ,11001100 Is an eight bit binary number .

*
byte (byte): It's in the computer data processing The basic unit of , It's customary to capitalize B To express .

*
1B(byte, byte ) = 8bit( position )

*
character : It refers to the letters used in the computer , number , Words and symbols

*
1bit express 1 position

*
1Byte Represents a byte 1B=8b

*
1024B=1KB

*
1024KB=1M

*
1024M=1G

<> Integer expansion :

<> Base system :

*
Binary 0b (Integer.toBinaryString) Binary code

1010 transformation 0* 2(0)+1* 2(1)+0* 2(2)+1* 2(3)=0+2+0+8===>10

*
decimal system

(1011)2 Power , transformation 1* 2(3)+0* 2(2)+1* 2(1)+1* 2(0)=8+0+2+1=11(11)10 Power

123 transformation 3* 10(0)+2* 10(1)+1* 10(2)=3+20+100

*
octal number system 0 (0-7)

067 transformation 7* 8(0)+6* 8(1)=48+7=(55)10 Power

*
hexadecimal 0x 0-9 A-F(10-16) (Integer.toHexString) Hexadecimal code

transformation 10* 16(0)+15* 16(1)+14* 16(2)+9* 16(3)=10+240+3584+36864

<> Base conversion

<>n Base conversion 10 Base system

formula : first place n(0)+ Second place n(1)+…+ The third x position * n(x-1) Power **

​ Binary to decimal
​ Decimal to binary
​ decimal system Binary
​ 1 0001
​ 2 0010
​ 3 0011
​ 4 0100
​ 5 0101
​ 6 0110
​ 7 0111
​ 8 1000
​ 9 1001
​ 10 1010
​ 11 1011
​ 12 1100
​ 13 1101
​ 14 1110
​ 15 1111

<>10 Base conversion 2 Base system

*
Short division :

35:(35/2=17( more than 1)/2=8(1)/2=4(0)/2=2(0)/2=1(0)/2=0(1) Reverse reading )=100011
100011 1* 2(0)+1* 2(1)+0* 2(2)+1* 2(5)=1+2+32=35( Positive )

*
Find power :

35:35-2(5)=3-2(1)=1-2(0)=100011( To the power of several 1, There's no good remedy 0)

216:216-2(7)=88-2(6)=24-2(4)=8-2(3)=11011000

<> Negative conversion

java It adopts binary complement
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
hold 1 The beginning is considered negative
1000 -8
1001 -7
1010 -6
1011 -5
1100 -4
1101 -3
1110 -2
1111 -1

4 How many digits can a binary complement represent ? 2(4)=16
Maximum number ?2(4-1)-1 ====》7
Minimum decimal ?-2(4-1) ====》-8

Suppose that a language is written in English 8 The binary complement represents a number, then the range is ?
2(8-1)-1 reach -2(8-1)

The binary complement indicates that the data range is related to the number of bits, if the number of bits is n
So the scope is -2(n-1) reach 2(n-1)-1

java Middle integer (int) Yes, it is 32 Bit binary complement representation
java int What's the scope of this -2(31) reach 2(31)-1

summary 2: 4 A bit of a look
7 0111 Negation = = =》1000 +1 = = = 》1001
-7 1001 Negation = = = 》0110 +1 = = =》0111
negative = Negation of positive number +1
Positive number = Negative negation +1



<> Floating point expansion :

* float; double Not accurate enough ; In practice, floating-point numbers are infinite and discrete , however float It's limited , He will automatically round the error , About close , But it's not equal to ;
* Accurate calculation tools such as banks are best used BigDecimal Mathematical tools
* It is best to avoid using floating point numbers for accurate reconciliation
* It is best to avoid using floating point numbers for accurate reconciliation
* It is best to avoid using floating point numbers for accurate reconciliation
<> Character expansion :

The essence of all characters is still numbers

Java use Unicode Character set encoding , format ’\u0000’-’\uffff’

(Unicode It is a universal fixed length character set in the world , All characters are 16 position ,2 Bytes )

For characters that are not convenient to input, escape characters are used

Escape character :

as :’\n’“ Line feed ”,‘\t’“ Space ”,‘\’,‘\b’,‘\r’,‘"’,‘\u4e2d’.
Digital coding :‘0’:48,‘1’: 49…
English code :‘A’:65,‘B’:66…
Small English :‘a’ : 97,‘b’:98…

practice :
public void zifu(String a){ char[] zi = a.toCharArray();
//(String)a Convert character array toCharArray, And assignment int mu = 0; // letter String m1 = ""; int s = 0;
// number String s1 = ""; int k = 0; // Space int f = 0; // other String f1 = ""; for (int i
= 0; i <zi.length ; i++) { int p = (int)zi[i]; // take zi Is converted to Int numerical value , Compare with numerical value if ((
65 <= p && p<=90)||(97<= p && p <= 122)) {//A-Z The letter is 65-90,a-z The letter is 97-122 mu++; m1 =
m1+zi[i]+" "; }else if (48<= p && p <= 57){ //0-9 by 48-57 s++; s1 = s1 + zi[i] +
" "; }else if (p==32){ // The space is 32 k ++; }else { f++; f1 = f1 + zi[i] +" "; } }
System.out.println(" Number of existing letters :" + mu); System.out.println(" The existing letters are :" + m1); System.
out.println(" Number of existing numbers :" + s); System.out.println(" The existing number is :" + s1); System.out.
println(" Number of existing characters :" + f); System.out.println(" The existing characters are :" + f1); System.out.println(
" The number of spaces that exist :" + k); } public static void main(String[] args) { System.out.println(
" Please enter a string :"); Scanner scanner = new Scanner(System.in); String a = scanner.
nextLine(); ZiFuSuZhu ziFuSuZhu = new ZiFuSuZhu(); ziFuSuZhu.zifu(a); scanner.
close(); }
<> Type conversion :

*
because Java It's a strongly typed language , All the time when some operations are needed , Type conversion is required .

low --------------------------------------------------------------> high

byte, short, char -> int -> long -> float -> double

​ ( Decimals take precedence over integers )

*
In operation , Different types of data are converted to the same type first , And then we do the calculation .

*
Force type conversion high - - low

*
Automatic type conversion

Direct input from low to high , No cast required

*
be careful

* Boolean values cannot be converted
* Object types cannot be converted to unrelated types
* When converting high capacity to low capacity , Cast required
* There may be memory overflow during conversion , Or precision !
char turn int

Spillover problem

<> variable

*
What are the variables : It's a variable quantity !

*
Java Is a strongly typed language , Each variable must declare its type .

*
Java Variable is the most basic storage unit in program , Its elements include variable names , Variable type and scope .

*
matters needing attention :

* Each variable has a type , Types can be base types , It can also be a reference type .
* The variable name must be a legal identifier .
* Variable declaration is a complete statement , So every statement must end with a semicolon .
<> Variable scope

*
Class variable

In class , It can be used in the whole class , It must be added in front static;“static int a = 666;”

*
Instance variables

In class , Subordinate to object ;“int age;//0”

If it does not initialize itself , The default value for this type 0,0.0

Boolean value : The default is false; Except for the basic types , All other default values are null;

*
local variable

In the method , Values must be declared and initialized when used ;“int cba = 666;"

<> constant

*
constant (Constant): initialization (initialize) You can't change the value after ! A constant value .

*
Constant can be understood as a special variable , When his value is set , It is not allowed to be changed during program running .
final Constant name = value ; final double PI = 3.14;
*
Constant names generally use uppercase characters .

<> Variable naming conventions

*
All variables , method , Class name : See the name and know the meaning

*
Class member variable : Initial lowercase and hump principle :monthSalary

*
Except for the first word , Capitalize the following words

*
local variable : Initial lowercase and hump principle

*
constant : Capitals and underscores MAX_VALUE

*
Class name : Capitalizing and humping principles :Man,GoodMan

*
Method name : Initial lowercase and hump principle :run(),runRun()

Technology