The basic concept of variables

  • When you need to record a single data content in the program, you can declare a variable. The essence of declaring a variable is to apply for a storage unit in the memory. Since the data content in the storage unit can be changed, it is named "variable" .
  • Because the size of the stored data is different, the size of the required storage unit is different. The data type is used in the Java language to describe it. In order to facilitate the next visit, you need to assign a name to the variable to record the corresponding variable. Storage unit.


personally sorted out some information, friends in need can click to get it directly.

Java basic knowledge

22 core Java architect books

Java learning routes and materials from 0 to 1

questions in 2021 160a61e10af906

Declaration and use of variables

Data type variable name = initial value;
Where = the initial value can be omitted, but; cannot be omitted

package com.lagou.Day02;

/**
 * 编程实现变量的声明和使用
 */
public class VarTest {
    public static void main(String[] args) {
        // 1.声明一个变量并初始化 数据类型 变量名 = 初始值
        int age = 18;
        // 2.打印变量的数值 + 字符串连接符 用于将两边的内存拼接/链接起来结果还是字符串
        System.out.println("age -" + age);//age -18
    }
}

Notes on the use of variables

Java is a strongly typed language, and variables must be declared to indicate their data types before they are used.
Variables must be initialized before use
Variables cannot be declared repeatedly

package com.lagou.Day02;

/**
 * 编程实现变量的声明和使用
 */
public class VarTest {
    public static void main(String[] args) {
        // 1.声明一个变量并初始化 数据类型 变量名 = 初始值
        int age = 18;
        // 2.打印变量的数值 + 字符串连接符 用于将两边的内存拼接/链接起来结果还是字符串
        System.out.println("age -" + age);//age -18

        System.out.println("------------------------");
        //使用变量之前需要声明
        //System.out.println("name -" + name);错误:找不到符号

        //使用变量之前需要初始化
        //String name;
        //System.out.println("name -" + name);错误:可能未初始化

        String name = "科比";
        System.out.println("name -" + name);//name -科比
    
        //变量不能重复声明
        //int age = 19;
    }
}

Identifier naming rules

  • Consists of numbers, letters, underscores, and $, etc., where the number cannot start
  • You cannot use keywords in the Java language. The so-called keywords are words used in the Java language to express special meanings.
  • It is case sensitive, and the length is unlimited but should not be too long.
  • Try to understand the name as much as possible, support Chinese but not recommended.
  • Identifiers can give names to classes/variables/attributes/methods/packages.

Case realization of variable input and output

package com.lagou.Day02;

import java.util.Scanner;

/**
 * 编程实现变量的输入输出
 */
//导入Java目录中Util目录的Scanner类
public class VarIOTest {
    public static void main(String[] args) {
        //1.声明两个变量用于记录姓名和年龄信息
        String name;
        int age;

        //2.提示用户从键盘输入姓名和年龄信息并放入到变量中
        System.out.println("请输入您的姓名和年龄信息:");
        //创建一个送啊米奇读取一个字符串数据放入变量name中
        Scanner sc = new Scanner(System.in);
        //通过扫描器读取一个字符串数据放入变量name中
        name = sc.next();
        //通过扫描器读取一个整数数据放入变量age中
        age = sc.nextInt();

        //3.打印变量的数值
        System.out.println("name = " + name);
        System.out.println("age = " + age);

        /**
         * 输出
         * 请输入您的姓名和年龄信息:
         * kobe
         * 20
         * name = kobe
         * age = 20
         */
    }
}

Classification of data types

Data types in the Java language are mainly divided into two categories:

(1) Basic data types (remember)

byte、short、int、long、float、double、boolean、char

(2) Reference data type (understand)

Array, class, interface, enumeration, annotation

Common base

In daily life, the decimal system is used to describe the data, and the decimal system weight is: 100, 101, 10^2,...

At the bottom of the computer, a binary sequence composed of 0 and 1 is used to describe the data. Every two is entered into one, and the binary weight is 20, 21, 2^2,...
The highest bit (leftmost) in the binary system is used to represent the sign bit. If the bit is 0, it means a non-negative number, and if the bit is 1, it means a negative number.
Octal and hexadecimal are actually abbreviations for binary

The first way to convert positive decimal to binary

How to convert positive decimal to binary
a. Divide by 2 and take the remainder method, use a decimal integer to continuously divide by 2 to get the remainder, until the quotient is 0, the remainder is sorted in reverse order.
0b/0B can be used as a binary prefix
45-binary 101101: It is a 64-bit binary.

The second way to convert positive decimal to binary

Split method, split the decimal integer into the sum of several binary weights, write 1 below the weight, otherwise write 0

How to convert positive binary to decimal

The weighting method uses each digit in the binary system to be multiplied by the weight of the current bit and then accumulated.

How to convert negative decimal to binary

First convert the absolute value of decimal to binary, then perform bitwise inversion and add 1.
Negative numbers need to be complemented: inverted by bit, plus 1

How to convert negative binary to decimal

First subtract 1 and then invert by bit, merge into a decimal integer and add a minus sign

The range of integers that can be represented by a single character

In a computer, a single byte represents an eight-bit binary bit, where the highest (leftmost) represents the sign bit, 0 represents a non-negative number, and 1 represents a negative number. The specific integer range is as follows:

The concept of integer types

The types of integer data described in the Java language are: byte, short, int, long, and the int type is recommended.

Programming Use of Integer Type (Part 1)

package com.lagou.Day02;

/**
 * 编程实现整数类型的声明和使用
 */
public class IntTest {
    public static void main(String[] args) {

        //1.声明一个byte类型的变量并初始化
        byte b1 = 25;
        System.out.println(b1);//25

        //byte b2 = 250;//超过byte的范围
        //250这样直接写出来的整数数据叫做直接量/常量/字面值 默认int类型

        short s1 = 250;
        System.out.println(s1);//250 short 3万多

        int i1 = 250250;
        System.out.println(i1);//21亿多
    }
}

Programming Use of Integer Type (Part 2)

The integer data written directly in the Java program is called direct quantity/literal quantity/constant, and the default is int type. If you want to express a larger direct quantity, add l or L after the direct quantity. L is recommended.

//int i1 = 2502505006;//错误:整数太大,默认为int类型,这个数据自身已经出错,无法表示
        //int i1 = 2502505006L;//错误:不能兼容的类型:从long转换到int可能会有损失

        //声明一个long类型到变量并初始化
        long g1 = 2502505006L;
        System.out.println(g1);

If the description hits the data more than the long type, use the java.math.BigIntger type

Written test site for integer type

package com.lagou.Day02;

/**
 * 面试题
 */
public class IntTest01 {
    public static void main(String[] args) {
        int i = 25;
        //byte b = i;错误:不兼容的类型:从int转换到byte可能会损失精度
    }
}

The concept of floating-point types

  • Java language used to describe the types of decimal data: float and double, double type is recommended
  • Among them, the float type occupies 4 bytes in the memory space, called a single-precision floating-point number, which can represent 7 significant digits, ranging from -3.403E38 to 3.403E38
  • The double type occupies 8 bytes in the memory space and is called a double-precision floating point number, which can represent 15 significant digits, ranging from -1.798E308 to 1.798E308.

Programming use of floating point types

package com.lagou.Day02;

public class DoubleTest {
    public static void main(String[] args) {
        //1.声明一个float类型的变量并初始化
        //错误:不兼容的类型:从double转换到float可能会有损失;默认是double
        //float f1 = 3.1415926;
    }
}

The concept and programming use of Boolean type

Java language is used to describe the type of true and false information: boolean, the value is only true and false
The size of the Boolean type in the memory space is not clearly defined, and can be regarded as 1 byte.
package com.lagou.Day02;

package com.lagou.Day02;

/**
 * 编程实现布尔类型的使用
 */
public class BooleanTest {
    public static void main(String[] args) {
        //1.声明一个boolean类型的变量并初始化
        boolean b1 = true;
        //2.打印变量的数值
        System.out.println(b1);
    }
}

The concept of character types

The data type used to describe a single character in the Java language: the char type. Such as:'a','中', etc.
Among them, the char type occupies 2 bytes in the memory space and has no sign bit. The range of representation is: 0-65535. Since few data in real life can be described by a single character, it is more used in future development. A string composed of multiple strings is described by the String type, such as:'hello','Kobe', etc.
The bottom layer of the computer only recognizes the binary sequence composed of 0 and 1. For groups such as the character'a', this rule is not satisfied, so the data cannot be stored directly in the computer, but such pattern data in real life requires computer storage , In order to make the data can be stored, you can assign a number to the data, and then store the number, the number is called ASCII.

Use of character types and numbers

package com.lagou.Day02;

public class CharTest {
    public static void main(String[] args) {
        char c1 = 'a';
        System.out.println(c1);//a
        System.out.println((int)c1);//97
    }
}

Remember: '0'=48;'A'=65

The concept and use of Unicode character set

Java character types use Unicode character set encoding. Unicode is a universal fixed-length character set, and all characters are 16 bits.

The concept and use of escape characters

Use of special characters

The concept and use of automatic type conversion

The conversion between basic data types in the Java language: automatic type conversion and forced type conversion.
Among them, automatic type conversion mainly refers to the conversion from small type to large type

The concept and use of forced type conversion

Big type to small type
Target type variable name = (target type) source type variable name;

package com.lagou.Day02;

public class Demo04 {
    public static void main(String[] args) {
        byte b1 = 10;
        short s1 = 20;

        b1 = (byte) s1;
        System.out.println(b1);//20
    }
}

At last

Thanks for seeing here, I feel helpful to you, remember to like it!


前程有光
936 声望618 粉丝