zoukankan      html  css  js  c++  java
  • Think in Java 第三章操作符

    Think in Java 第三章操作符

    赋值

    对象赋值

    ​ 我们真正操作的是对对象的引用。所以倘若”将一个对象赋值给另一个对象“,实际上是将”引用“从一个地方复制到另一个地方。倘如对 对象使用 c= d 那么c和d都指向原本d指向的那个引用。

    别名机制

    修改c 的 同时也会改变d 他们不是相互独立的 指向相同的对象 而c原本指向的对象,会被垃圾回收自动清理

    正确做法

    t1.level = t2.level
    

    保持两个对象相互独立,而不是将t1 和 t2 绑到相同对象身上

    算数操作符

    练习3

    class VelocityCalculator1{
        static float velocity(float d,float t)
        {
            if(t == 0) return 0f;
            else  return d/t;
        }
    }
    
    public class VelocityCalculator {
    
        public static void main(String[] args) {
            float d = 902.3f;
            float t = 100.4f;
            System.out.println("Distance =" +d);
            System.out.println("time =" + t);
            float a = VelocityCalculator1.velocity(d,t);
            System.out.println("Velocity =" + a);
    
        }
    }
    

    对象的等价

    == !=

    比较的是 对象的 引用

    若想比较 对象的实际内容是否相同, 应该使用 特殊方法 equals()

    练习五

    class Doog{
        String name;
        String say;
        void setName(String n){
            name = n;
        }
        void setSay(String s){
            say = s;
        }
        void showName(){
            System.out.println(name);
        }
        void showSay(){
            System.out.println(say);
        }
    }
    public class dog {
        public static void main(String[] args) {
            Doog dog1 = new Doog();
            Doog dog2 = new Doog();
            dog1.setName("spot");
            dog1.setSay("Ruff");
            dog2.setName("Scrufy");
            dog2.setSay("Ruff");
            dog1.showName();
            dog1.showSay();
            dog2.showName();
            dog2.showSay();
    
    
        }
    }
    

    练习七 抛硬币simulates_coin_flipping

    nextInt()用法:

    会随机生成一个整数,这个整数的范围就是int类型的范围-2^31 ~ 2^31-1,但是如果在nextInt()括号中加入一个整数a那么,这个随机生成的随机数范围就变成[0,a)。
    import java.util.*; 
    import org.greggordon.tools.*;
    
    public class CoinToss {
    	public static void main(String[] args) {
    		Random rand = new Random();
    		int coin = rand.nextInt();
    		if(coin % 2 == 0) P.rintln("heads");
    		else P.rintln("tails");		
    	}
    }
    

    移位操作符

    有符号移位 >> 左移 低位补0 右移

    正数 高位 插入0

    负数 高位插1

    无符号移位 >>> 无论正负 高位插0

    练习11

    public class RightShiftTest {
        public static void main(String[] args) {
            int h = 0x10000000;
            System.out.println(Integer.toBinaryString(h));
            for (int i=1;i<28;i++){
                h >>>= 1;
                System.out.println(Integer.toBinaryString(h));
            }
        }
    }
    ======================================================
    10000000000000000000000000000
    1000000000000000000000000000
    100000000000000000000000000
    10000000000000000000000000
    1000000000000000000000000
    100000000000000000000000
    10000000000000000000000
    1000000000000000000000
    100000000000000000000
    10000000000000000000
    1000000000000000000
    100000000000000000
    10000000000000000
    1000000000000000
    100000000000000
    10000000000000
    1000000000000
    100000000000
    10000000000
    1000000000
    100000000
    10000000
    1000000
    100000
    10000
    1000
    100
    10
    

    练习12

    public class RightShiftTest {
        public static void main(String [] args) {
            int h = -1;
            System.out.println(Integer.toBinaryString(h));
            h <<= 10;
            System.out.println(Integer.toBinaryString(h));
            for(int i = 0; i < 32; i++) {
                h >>>= 1;
                System.out.println(Integer.toBinaryString(h));
            }
        }
    }
    =============================================================
    11111111111111111111111111111111
    11111111111111111111110000000000
    1111111111111111111111000000000
    111111111111111111111100000000
    11111111111111111111110000000
    1111111111111111111111000000
    111111111111111111111100000
    11111111111111111111110000
    1111111111111111111111000
    111111111111111111111100
    11111111111111111111110
    1111111111111111111111
    111111111111111111111
    11111111111111111111
    1111111111111111111
    111111111111111111
    11111111111111111
    1111111111111111
    111111111111111
    11111111111111
    1111111111111
    111111111111
    11111111111
    1111111111
    111111111
    11111111
    1111111
    111111
    11111
    1111
    111
    11
    1
    0
    

    习题13

     public static void main(String [] args) {
            char c = 'a';
            System.out.println(Integer.toBinaryString(c));
            c = 'b';
            System.out.println(Integer.toBinaryString(c));
            c = 'c';
            System.out.println(Integer.toBinaryString(c));
            c = 'd';
            System.out.println(Integer.toBinaryString(c));
            c +=1;
            System.out.println(Integer.toBinaryString(c));
            c = 'A';
            System.out.println(Integer.toBinaryString(c));
            for(int i = 0; i < 6; i++) {
                c +=1;
                System.out.println(Integer.toBinaryString(c));
            }
        }
    ====================================================
    1100001
    1100010
    1100011
    1100100
    1100101
    1000001
    1000010
    1000011
    1000100
    1000101
    1000110
    1000111
    

    提升

    如果对 基本类型 执行算数运算或者按位运算,只要比int小 (char,byte,short),在运算之前就 自动转换成int。最终结果就是int类型。 如果把结果赋值给较小的类型,需要使用类型转换。通常表达式中出现大的数据类型决定了表达式最终结果的数据类型。 folat值与一个double值相乘,结果是double

    练习14

    import org.greggordon.tools.*;
    
    public class StringCompare {	
    	static void f(boolean b) {if(b == true) P.rintln(true);
    		else P.rintln(false);}
    	static void stringTest(String s, String t) {
    		f(s == t);
    		f(s.equals(t));
    		f(t.equals(s));
    		f(s != t);
    		// f(!s);
    		//f(!t);
    		// s = s && t;
    		// s = s || t;
    		// s = ~t;
    		// s = s  & t;
    		// s = s | t;
    		// s = s ^ t;
    		// s &= t;
    		// s ^= t;
    		// s |= t;
    	}	
    	public static void main(String[] args) {
    		String s = "one", t = "two";
    		StringWork.stringTest(s, t);		
    	}
    } 
    ========================================================
    false
    false
    false
    true
    
  • 相关阅读:
    [JLOI2015]有意义的字符串
    二阶常系数线性齐次递推式的特征方程
    CH1812 生日礼物
    CH1809 匹配统计
    CH1808 Milking Grid
    BZOJ4025 二分图
    BZOJ3514 GERALD07加强版
    NOI2014 魔法森林
    WC2006 水管局长数据加强版
    LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测
  • 原文地址:https://www.cnblogs.com/AronJudge/p/14317093.html
Copyright © 2011-2022 走看看