JAVA Practice Notes - 4 from scratch

We can use java program operation, we need to master data types, constants, variables and operators

 

About the data type I have summarized a Excel spreadsheet, the range in which the single-precision floating-point and double-precision floating-point calculation is not accurate.

 

 

Constants are running during the java fixed data, can be printed

public  class Demo {
     public  static  void main (String [] args) {
         final  int C =. 1;                          // create a constant, integer type, constant final keyword to define 
        System.out.println (C);                    // output: 1     
    } 
}

 

Refers to the variable data can change during the java program is running, it can be printed

public  class Variable {
     public  static  void main (String [] args) { 

        int NUM = 50;                           // create a variable format: Data type variable name = data value 
        System.out.println (NUM);              // Output: 50 
        
        int Number;                             // create a variable format: data type variable name; 
        Number = 3;                             // stored data into a variable: variable name = data value         
        System.out.println (Number);             // output: 3. 
        
        Double d;                               // create a double-precision floating-point type of a variable, the variable name is named d
        = 3.3 D;                                // stored data to a variable, the data value 3.3 
        System.out.println (D);                  // print output variable d, the output: 3.3 
        
        a float KK; 
        KK = 3.2F;                              // float which The default type is double, if we must use the float type, the need to add a suffix F. 
        System.out.println (KK);                 // output: 3.2 
    } 
}

 

How to verify constant during the run java program fixed, variable can change?

public  class the Test {
     public  static  void main (String [] args) { 
        
        final  int A =. 1;                         // create a constant, constants used to define the final keyword 
        System.out.println (A);                   // Output:. 1 
        
        final  int 10 = b;                        // create a constant
         // SB = "20";                             // modify the values of the constants, will complain, can not run.
        // workaround, delete or modify comments, because in doing exercises, so select the Notes to facilitate understanding during this review. 
        System.out.println (B);                   // Output: 10 
        
        int C = 100;                             //Create a variable assignment 100 
        C = 200;                                 // modify a variable 
        System.out.println (C);                   // Output: 200 
    } 
}

 

Operators are symbols constant and variable operating a equal sign operator is used in practice, the assignment operator.

Arithmetic:

public class Demo1 {
    public static void main(String[] args) {
        
        int a = 66;
        int b = 66;
        
        int r1 = a * b;                        
        System.out.println(r1);             //输出:4356,想要验算,可以使用手机or电脑上的计算器再计算一遍。
        
        int r2 = a + b;                      //加法运算符
        System.out.println(r2);              //输出:132
        
        int r3 = a - b;                       //减法运算符
        System.out.println(r3);              //输出:0
        
        int r4 = a / b;                       //除法运算符
        System.out.println(r4);              //输出:1
        
        
        int x =10;
        int y =3;
        
        int result = x%y;                      //模运算符        
        System.out.println(result);            //10除以3,等于3余数1
        
        
    }
}

 

逻辑运算符:

什么是逻辑?

简单打个比方,我们去超市购物,想要买一把雨伞

在收银台结算时,通常问默会自己,买没买雨伞。

这里的回答就是一种逻辑上的运算,买or没买。

是与否、有与无也都是逻辑的判断

public class Demo2{
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        
        System.out.println(a&&b);                 //输出:false。&&逻辑与运算符,仅当两个操作都为真,条件才为真。
        
        System.out.println(a||b);                 //输出:true。逻辑或运算符,两个操作其中有一个为真,条件为真。
        
        System.out.println(!a);                  //输出:false。逻辑非运算符,取反。

/*  这就是多行注释 
* 与“&&”,或“||”,具有短路效果:
* 如果根据左边已经可以判断得到最终结果,那么右边的代码不再执行,从而节省一定的性能。
* 
* 类比:斗地主时,我的牌是4和10,对方只有一张牌,我要如何出?
* 先出10,如果对方剩下的牌是5-10,那么我这局就赢了。
* 这在就是斗地主中,短路效果的应用。
*/
    }
}

 

自增自减运算符

public class Demo3 {
    public static void main(String[] args) {
        
        int x = 1;                    
        x++;                                      //变量x自增1    
        System.out.println(x);                     //输出:2。因为变量x自增了1
        
        int y = 1;
        y--;                                      //变量y自减1
        System.out.println(y);                     //输出:0。因为变量y自减了1。
        

        //自增自减符号,单独使用时,符号在前在后都一样。
        //与其它混合使用时,如果,运算符在前,那么结果就要先增减,后打印。反之,符号在后,就先打印。
        int a = 1;                            //运算符在前,那么结果就要先增减,后打印    
        System.out.println(++a);             //输出:2。  
        
        int b = 1;
        System.out.println(b--);              //符号在后,就先打印,后自减。所以输出是1,输出后b的值自减1。
//如何看到b的值是否自减了呢? 将变量b打印输出即可
System.out.println(b);        //自减之后变量b的值是0,所以输出:0 } }

 

 

Guess you like

Origin www.cnblogs.com/H742/p/11588719.html