02. Java Basics

Directory Structure

  • Note: The red font is the unanswered knowledge point
  • 1. Java identifier rules [rules, precautions, naming conventions]
  • 2. Constants and variables [difference, types of constants]
  • 3. Hex [type, conversion, complement complement]
  • 4. Java data types [types, conversion rules, points to note]
  • 5. Character encoding [type]
  • 6. Operator [type, difference]
  • 7. Java basic statement structure [type, difference description]
  • 8. Method [definition, format]
  • 9. Array [one-dimensional array, two-dimensional array]
  • 10. Memory allocation and stack and heap

1. Java Identifier Rules

  • 1.1 Composition Rules
    • English uppercase and lowercase letters
    • numeric characters
    • $ or _
  • 1.2 Notes
    • cannot start with a number
    • cannot be a keyword in Java
    • case sensitive
    • no spaces
  • 1.3 Naming conventions [must know the meaning of the name]
    • Underscore naming: my_name
    • CamelCase: myName
    • Class, interface name: must start with a capital letter, such as PersonActivity
    • Method, variable [string] Name: getData(), otherName
    • Constant naming: generally all uppercase, such as MAX_VALUE

2. Constants and Variables

  • 2.1 What are constants and variables
    • A constant whose value cannot be changed is called a constant
    • A variable whose value changes is called a variable
  • 2.2 What are the constants
    • String constant "hello world" enclosed in double quotes
    • Integer constants all integers 199
    • decimal constant all decimals 4.50
    • Character constants are 'a' enclosed in single quotes
    • boolean constants true and false
    • empty constant null
  • 2.3 Variables
    • It is used to describe the amount by which the value can change, such as the outdoor temperature, the number of active users of the app, etc.

3. System

  • 3.1 What are the bases?
  • binary
    • 0 and 1 prefix 0B or 0b For example: 0B1001 converted to decimal is 9
  • Octal
    • 0-7 prefix 0 for example: 0103
  • Decimal [what we commonly use]
    • 0-9 without prefix
  • hex
    • 0-9 and AF prefix 0x or 0X eg: 0x123
  • Hex conversion
    • Convert any base to decimal formula: coefficient * base ^ exponent
0b1010 = 1 * 2^3 + 0 *2^2 + 1*2^1 + 0*2^0
       = 8 + 0 + 2 + 0
       = 10

0123  = 1*8^2 + 2*8^1 + 3*8^0
      = 64 + 16 + 3
      = 83

0x123 = 1*16^2 + 2*16^1 + 3*16^0
      = 256 + 32 + 3
      = 291
  • 3.2's complement
    • The complement of a positive number is the same as the original code
5
原码:0000 0101
补码:0000 0101
	* 负数的补码是它的反码加1

-5:
原码:1000 0101
反码:1111 1010
    +         1
------------------
补码  1111 1011

4. Java data types

  • 4.1 What are the data types

  • byte type

    • 1 byte, 8 bits [between -128 and 127]
  • short type

    • 2 bytes, 16 bits [-2^15 to 2^15 -1]
  • int type

    • 4 bytes, 32 bits [-2^31 to 2^31 -1]
  • float type [floating point]

    • 4 bytes, 32 bits
  • double type [floating point type]

    • 8 bytes, 64 bits
  • long type

    • 8 bytes, 64 bits [-2^63 to 2^63 -1]
  • char type

    • 2 bytes, 16 bits [1 Chinese character is exactly 2 bytes]
  • boolean type

    • 1 byte, true and false
  • 4.2 Conversion of data

  • Default conversion

    • 1:byte,short,char—int—long—float—double
    • 2: Byte, short, and char are complemented and converted to each other, and they are first converted to int type when they participate in the operation
  • cast

    • Format: target type variable name = (target type) value or variable name
    • int a = (int) 15.7f;
  • Points to Note

!!!!!!!注意!!!!!!!!
1:在java中,任何一个整数默认为 int 类型 (1)
2:在java种,任何一个小数,默认为 double 类型( 1.0)
3:123L 或者 1231 编译器会将该数当成long类型
4:12.345f 或者12.345F 编译器会将该数当成float类型

5. Character encoding

  • common character encodings
    • ASCII code
    • GBK code
    • Unicode code [occupies 2 bytes]

6. Operators

  • 6.1 What are the operators?
    • arithmetic operators
    • assignment operator
    • relational operator
    • Logical Operators
    • Ternary operator
    • Special Operators: Bitwise Operators
  • 6.2 Arithmetic operators
      • , - , * , / , % [remainder] , ++ [self-addition], --[self-subtraction]
  • 6.3 Assignment Operators
    • = , += , -= , *= , /= , %=
  • 6.4 Relational Operators
    • == , > , < , >= , <= , !=
  • 6.5 Logical Operators
    • & ,^ ,| ,&& ,|| ,! , ^
  • 6.6 Ternary operator [many used in Android]
    • a == b ? c : d
    • Format: (conditional expression)? Expression 1: Expression 2
  • 6.7 Bitwise operators
    • & , | , ^ , ~ , << , >> , >>>

7. Java basic statement structure

  • 7.1 Types of Statement Structures
    • sequential structure
    • choose structure
    • loop structure
  • 7.2 Sequence structure
    • Execute from top to bottom
  • 7.3 Select structure [can be nested]
    • if , if else , if else if …… else
    • switch case【支持byte,short,char,int,String】
  • 7.4 Looping Structures
  • for
for(初始化语句;判断条件语句;控制条件语句) {
         循环体语句;
}
第一步:先执行初始化语句,只执行一次
第二步:执行判断条件语句
第三部:执行循环体语句
第四步:执行控制条件语句,然后再执行循环体语句【循环】
  • while
初始化语句;
while(判断条件语句) {
    循环体语句;
    控制条件语句;
}
  • do while
do {
         循环体语句;
}while((判断条件语句);

8. Java methods

  • 8.1 Definitions

  • A method is a block of code that performs a specific function

    • 1: System method, only need to be able to use, do not need to know the internal structure
    • 2: Custom method: The internal implementation of the method requires us to write
    • 3: Methods cannot be nested inside methods
  • 8.2 Format

  • method format

修饰符   返回值类型   方法名(参数类型 参数 , ……){
     函数体
     return 返回值
}
  • Modifier: public static or directly public or other private
  • Return type: all data types (basic types: int, char, float, long, reference type: String)
  • Method name: 1: See the name and know the meaning 2: getMax
  • Parameter types: all data types, if a method has no return value, the return value type is void
  • parameter name: variable name
  • Return value: is the result to be returned (this result must be consistent with the return type)

9. Arrays

  • 9.1 Array Concepts

    • An array is a container that can store multiple variables, and the data types of these variables must be consistent
    • Arrays can store both primitive and reference data types
  • 9.2 One-dimensional arrays

  • array definition format

    • Format 1: data type [] array name
    • Format 2: data type array name []
int[] a;     定义了一个int类型的数组a;
int a[];    定义了一个int类型的a数组;
推荐使用第一种定义方式。
  • array initialization

    • Arrays in Java must be initialized before they can be used.
    • The so-called initialization: is to allocate memory space for the array elements in the array, and assign values ​​to each array element.
  • Initialize the classification:

    • a: Dynamic initialization: only the length is specified, and the initialization value is given by the system
    • b: static initialization: give the initialization value, the length is determined by the system
    • Note: Only one of these two methods can be used, and the combination of dynamic and static cannot be used.
  • 9.3 Two-dimensional arrays

  • array definition format

    • datatype[][] variablename = new datatype[m][n];
    • m indicates how many one-dimensional arrays there are in this two-dimensional array
    • n represents the number of elements in each one-dimensional array
  • Example:

int[][] arr = new int[3][2];
定义了一个二维数组arr
这个二维数组有3个一维数组,名称是arr[0],arr[1],arr[2]
每个一维数组有2个元素,可以通过arr[m][n]来获取,表示获取第m+1个一维数组的第n+1个元素

10. Java memory allocation and the difference between stack and heap

  • 10.1 First understand a few concepts
    • stack
    • heap
    • method area
    • native method to
    • register
A:栈: 存放的是局部变量
局部变量:在方法定义或者方法声明上的变量都是局部变量。
B:堆: 存放的是所有new出来的东西
特点:
    a: 每一个new出来的东西都会为其分配一个地制值。
    b: 每一个变量都有一个默认的值
        byte,short,int,long  -- 0
        float,double            -- 0.0
        char                    -- '\u0000'
        boolean                -- false
        引用数据类型               -- null       
    c: 使用完毕就变成了垃圾,等待垃圾回收器对其回收
C:方法区:(面向对象部分讲解)
D:本地方法区:(和系统相关)
E:寄存器:(cpu使用)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325025831&siteId=291194637