java study notes (Basics) - variables and expressions

A: local variables and instance variables

  • Define the variable refers to the name and data type of a variable set of variables, Java language requirements defined variables follow the first, then initialize, then the rules of use.

  • Scope: refers to the range of its existence, and only in this context, the program code to access it.
    Life-cycle variables refers to a variable is created and assigned memory space from the beginning to the process of this variable is destroyed and cleared it take up memory space.

    Local variables (parameter variables can also be seen as local variables):

    1) position: the definition in the method, or {} in the method
    2): first assignment using
    3) Scope: The method defined or as defined in {}
    4) Cycle Life: The method of calling from the definition of variables to End

    Examples of local variables:
    public void method1() {
        int a = 0;   //局部变量,作用域为整个method01方法;
         { int b = 0; //局部变量,作用域为所处的代码块;
             b = a;
         }
         b = 20;   //编译出错,b不能被访问;
    }

    Instance variables:

    1) Location: The method defined outside, inside the class
    2): the system will default initialization
    3) Scope: the role of the entire class
    4) the life cycle: the object created when the memory recovery end gc

    Examples of instance variables:
    class Test {
          private int n1=0;
          private int n2=0;
          public int add() {
             int result = n2 + n2;
             return result;
           }
    }

II: operator:

  • Operators can be composed of expressions corresponding to the type of data, corresponding to complete the operation.

    a) the mathematical operator that

    + Adding data type value or string is connected;
    System.out.println(1+2+"a");          //输出3a
    System.out.println(1+2.0+"a");        //输出3.0a
    System.out.println(1+2.0+"a"+true);   //输出3.0atrue
    System.out.println("a"+1+2);          //输出a12
    System.out.println(1+"a"+2);          //输出1a2
    / Divisible, such as the operands are integers, the calculation result for the integer part and Suppliers
    int a1=12/5;      //a1变量的取值为2
    int a2=13/5;      //a2变量的取值为2
    int a3=-12/5;     //a3变量的取值为-2
    int a4=-13/5;     //a4变量的取值为-2
    int a5=1/5;       //a5变量的取值为0
    double a6=-12/5;   //a6变量的取值为-2.0   
    double a7=-12/5.0; //a7变量的取值为-2.4   
    % Modulus operator, such as the operands are integers, the operation result is an integer part of the quotient
    int a1=1%5;      //a1变量的取值为1
    int a2=13%5;     //a2变量的取值为3
    double a3=1%5;    //a3变量的取值为1.0
    double a4=12%5.1;  //a4变量的取值为1.8000000000000007  /整除, 如操作数均为整数,运算结果为商的整数部分
    int a1=12/5;      //a1变量的取值为2
    int a2=13/5;      //a2变量的取值为2
    int a3=-12/5;     //a3变量的取值为-2
    int a4=-13/5;     //a4变量的取值为-2
    int a5=1/5;       //a5变量的取值为0
    double a6=-12/5;   //a6变量的取值为-2.0   
    double a7=-12/5.0; //a7变量的取值为-2.4   
    % Modulus operator, such as the operands are integers, the operation result is an integer part of the quotient
    int a1=1%5;      //a1变量的取值为1
    int a2=13%5;     //a2变量的取值为3
    double a3=1%5;    //a3变量的取值为1.0
    double a4=12%5.1;  //a4变量的取值为1.8000000000000007

    b) assignment operator:

          = :   int x=0,i=1,j=1;
          *=:   这里的"*="由操作符"*"和"="复合而成,它等价于 a=a*b,这种          复合操作符能使程序变得更加简洁。
          /=:   a/=b 等价于 a=a/b;
          %=: a%=b 等价于 a=a%b;
     注意:+=和+的区别
        如:short a=0;
           int b=123456;
        a+=b;和a=a+b;的区别:
             +=系统会进行隐式的数据类型转换,向=左边的数据类型进行转换。
             a+b会向数据类型高的类型转换

    c) comparing the operator

>大于
>= 大于等于
<  小于
<= 小于等于
以上操作符只适用于整数类型和浮点数类型;
 int a=1,b=1;
 double d=1.0;
 boolean result1 = a>b;  //result1的值为false;
 boolean result2 = a<b;  //result2的值为false;
 boolean result3 = a>=d; //result3的值为true;
 boolean result4 = a<=b; //result4的值为true;    

#### instanceof

Determining whether a reference object types is referenced by an instance of the class. The operator of an object is left, the right is a class or interface name. Form as follows:

如:String str="hello"
   System.out.println(str instanceof String);
   System.out.println(str instanceof Object);
   System.out.println(str instanceof Student); //false

java.lang.Object is the parent of all classes, each class will by default inherit Object
subclass is a class that is a parent's relationship

d) the equality operator

== equal
! = Not equal to
either the base type, it can be reference types:
basic data type comparison is the real value of
a reference type of comparison is the address, if you want to compare reference types using real data equals

如:int a=1,b=1;
System.out.println(a==b);  //输出true;
如:String s1="hello";
   String  s2="hello";
   String s3=new String("hello");
   String s4=new String("hello");
   System.out.println(s1==s2); //true
   System.out.println(s3==s4); //false
   System.out.println(s1==s3); //false
   System.out.println(s3.equals(s4)); //true

e) the shift operator for binary operations

算术右移位运算,也称做带符号右移位运算。最高为补符号位。
> 逻辑右移位运算,也称为不带符号右移位运算。
> << 左移位运算,也称为不带符号左移位运算。

f) Bitwise Operators

&:  与运算,运算规则为:1&1->1, 1&0->0, 0&1->0, 0&0->0;   
|:  或运算,运算规则为:1|1->1, 1|0->1, 0|1->1, 0|0->0;
^:  异或运算,运算规则为:1^1->0, 1^0->1, 0^1->1, 0^0->0;
    两个值相同为0,不同为1;
~ : 取反运算, ~1->0, ~0->1;

例如:8>>2====>8/2^2=2
8:0000...00001000 
  0000.........10 ====>2
8>>>3=====>8/2^3=1
8:0000...00001000 
  00000000.000001=====>1
8<<2======>8*2^2=32
8:0000...00001000 
  000000000100000======>32
1&1=1 1&0=0 0&0=0
1|1=1 1|0=1 0|0=0
1^1=0 1^0=1 0^0=0
~1=0
~0=1 
8&2=0  8|2=10  8^2=10
~8=
~2=

1010
10===>第三位置1===>14
10===>第二位清0===>8
10===>第一位置反===>11
10===>输出相应的二进制

f) Logical Operators

短路操作符:  如果能根据操作左边的布尔表达式就能推算出整个表达式的布尔值,将不执行操作符右边的布尔表达式;
&&:左边的布尔表达式的值为false, 整个表达式值肯定为false, 
         此时会忽略执行右边的布尔表达式。
||:左边的布尔表达式的值为true, 整个表达式值肯定为true, 
         此时会忽略执行右边的布尔表达式。 
if(条件1&&条件2){}  
   if条件1为假,不会执行条件2
   if条件1为真,会执行条件2
if(条件1||条件2){}  
   if条件1为真,不会执行条件2
   if条件1为假,会执行条件2 

g) conditional operator

布尔表达式 ? 表达式1 : 表达式2
如果布尔表达式的值为true,就返回表达式1的值,否则返回表达式2的值。
int score = 61;
String result = score>=60?"及格":"不及格";

h)++,--
前++与后++
前--与后--
int a=10;
b=++a   =====>a=11,b=11,先计算,后赋值
b=a++   =====>a=11,b=10,先赋值,后计算
System.out.println("a:"+a+" b:"+b);

III. Data type conversion

1) The basic data type conversion

隐式的数据类型转换:精度小的数据给精度大的数据
强制(显式)的数据类型转换:(type)精度大的数据给精度小的数据
System.out.println((int)(char)(byte)-1);
数据类型在转换的时候注意:
a)如果将精度小的数据转为精度大的数据时。
      如果转之前是有符号数,在前面补符号位
      如果转之前是无符号数,在前面补0
b)如果将精度大的数据转为精度小的数据时。
      从低位窃取位数   

2) reference data type conversion

隐式的数据类型转换:子类转换父类
强制(显式)的数据类型转换:父类转换子类
      String str="hello";
      Object o=str;
      String str2=(String)o;

Guess you like

Origin www.cnblogs.com/chlinlearn/p/11209279.html