[Super invincible and detailed Han Shunping's java notes] From beginner to proficient---the use of variables and plus signs

1. Introduction to variables

A variable is equivalent to the representation of a data storage space in memory . You can think of a variable as the house number of a room. We can find the room through the house number , and the variable ( value ) can be accessed through the variable name .

Variables have three basic elements (type + name + value)

public class Test{
    public static void main(String [] args){
int a=1;//定义了一个变量,类型int整型,名称a,值1
int b=3;//定义了一个变量,类型int整型,名称b,值3
b=89;//把89值赋给b变量
System.out.println(a);//输出a变量的值
System.out.println(b);//输出b变量的值
    }
}
        
1) Declare variables
int a;
2) Assignment
a = 60; // It should be said : assign 60 to a
Use System.out.println(a);
// It can also be done in one step [int a = 60; usually we do it in one step ]

Example: Presenter’s information code

public class var01{
	public static void main(String []args){
		int age =30;
		double score = 88.9;
		char  gender = '男';
		String name = "king";

		System.out.println("人的信息如下:");
		System.out.println(name);
		System.out.println(age);
		System.out.println(score);
		System.out.println(gender);
	}
}

Notes on variables:

1. A variable represents a storage area in memory [different variables have different types and occupy different spaces, for example: int 4 bytes, double 8 bytes].

2. This area has its own name [variable name] and type [data type]

3. Variables must be declared first and then used, in order

public class a{
    public static void main (String[] args){
            int a = 50;
            System.out.println(a);
//要注意输出a不是"a",a"是把a当字符串输出了,当然是输出a,要想输出5,就去掉引号,才是输出变量a ,输出一个对象的值不需要加引号 .
    }
}

4. The data/values ​​in this area can continuously change within the same type range

public class a{
    public static void main (String[] args){
            int a = 50;
            System.out.println(a);
            a = 88;
            //a=JACK;错误的,类型不对编译报错
            System.out.println(a);
    }
}

5. Variables within the same scope (within the same type scope) cannot have the same name.

public class a{
    public static void main(String[] args){
    int a = 50;
    System.out.println(a);
    //int a = 30;
    //System.out.println(a);
    //不能定义两个a,变量在同一个作用域内(同一类型范围内)不能重名
    }
}
    class b{
    public static void main(String[] args){
    int a = 30;
    System.out.println(a);
    }
}

6. Variable = variable name + value + data type

2. The use of + sign in the program

1. When both the left and right sides are numeric, do the addition operation
2. When one of the left and right sides is a string. Then do the splicing operation
3. The order of operations is from left to right.
public class a{
    public static void main(String []args){
    System.out.println("100"+98);
    System.out.println(100+98);
    System.out.println(100+3+"hello");
    System.out.println("hello"+100+3);
    }
}

doubt:

Why System.out.println("hello"+100+3); output is hello1003, not hello103.

answer:

In Java, when a string and a number are concatenated using the "+" operator, the string and the first number are added, then the result is converted to a string, and then continues to be added to subsequent numbers. Therefore, the expression "hello"+100+3 actually adds "hello" to 100 to get "hello100", and then adds "hello100" to 3 to get "hello1003". If you want to get the result "hello103", you can use parentheses to change the order of operations, such as System.out.println("hello"+(100+3));.

Guess you like

Origin blog.csdn.net/qq_45206556/article/details/131703761