Java_008 variables, ++a and a++, for each loop


Points to note about variables

  • The variable is created in its scope and is revoked when it leaves its scope.
  • Variables defined in a method will no longer save their values ​​between several calls to the method.
  • The variable defined in the block will also discard its value when leaving the block.
  • The lifetime of a variable is limited to its scope.
public class example03 {
    
    
    public static void main(String[] args) {
    
    
        int x;                  /*如果一个声明定义包括一个初始化,那么每次进入声
                                明它的程序块时,该变量都要被重新初始化*/
        for (x=0; x<2; x++){
    
    
            int y = -1;
            System.out.println("y is: " + y);
            System.out.println("y is now: " + y);
        }
    }
}

The difference between ++a and a++

public class example05 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("Integer Arithmetic");
        int a = 1 + 1;
        int b = a * 3;
        int c = b / 4;
        int d = c - a;
        int e = -d;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println("d = " + d);
        System.out.println("e = " + e);
        System.out.println("--------------------------");//浮点型除法和整型除法之间的差别
        System.out.println("\nFloating Point Arithmetic");
        double da = 1 + 1;
        double db = da * 3;
        double dc = db / 4;
        double dd = dc - a;
        double de = -dd;
        System.out.println("da = " + da);
        System.out.println("db = " + db);
        System.out.println("dc = " + dc);
        System.out.println("dd = " + dd);
        System.out.println("de = " + de);

    }
}

Output result

Integer Arithmetic
a = 2
b = 6
c = 1
d = -1
e = 1
--------------------------

Floating Point Arithmetic
da = 2.0
db = 6.0
dc = 1.5
dd = -0.5
de = 0.5

== for each loop: ==
Java has a powerful loop structure that can be used to process each element in an array in turn (other types of element collections are also possible) without having to be distracted by specifying the subscript value. The format is:

for (variable : collection)  statement

Define a variable to temporarily store each element in the collection and execute the corresponding statement (of course, it can also be a statement block). E.g:

for (int element:a)
    System.out.println(element);

Print each element of array a, one element occupies one line.

The loop variable of the for each loop statement will traverse each element in the array without using subscript values.
————————————————
Copyright statement: This article is the original article of the CSDN blogger "Z zehao", and it follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and the copy. statement. Thank you
Original link: https://blog.csdn.net/Zzehao11/article/details/105168967

Guess you like

Origin blog.csdn.net/weixin_49207937/article/details/114489620