第三章 操作符

第三章 操作符

第三章 操作符

Java中的数据都是通过操作符来操作的

3.1 更简单的打印语句

3.2 使用Java操作符

操作符接受一个或多个参数,并且生成一个新值。
有些操作符可能会改变操作数本身的值。
几乎所有的操作符都只能操作基本类型。
但是= == !=可以操作所有的对象

3.3 优先级

当一个表达式中有多个操作符时,优先级决定了计算顺序。先乘除,后加减,有括号先算括号

public class Precedence{
   
	public static void main(String args[]){
   
		int x =1, y = 2, z = 3;
		int a = x + y - 2 / 2 + z;// a = 5
		int b = x + (y - 2) / (2 + z);// b =1 
		System.out.println("a = " + a + "b = " + b);
	}
}

3.4 赋值

操作符= :取右边的值,把它复制给左边

a = 4;

基本类型赋值。基本类型存储了实际的数值,并不是对象的引用,赋值的时候,是将一个值复制给另一个值。

a = b;
//b的值复制给a,如果修改了a是不会改变b的值的。

但是为对象赋值的时候,对对象进行操作时,实际操作的是对象的引用。将一个对象赋值给另一个对象,实际上是将一个引用复制到另一个地方

c = d;
//d所存储的引用复制给c,c和d都是指向同一个引用
//Assignment.java
public class Assignment{
   
	public static void main(String args[]){
   
		Tank t1 = new Tank();
		Tank t2 = new Tank();
		t1.level = 9;
		t2.level = 47;
		System.out.println("t1.level = " + t1.level + "t2.level = " + t2.level); //9 47
		t1 = t2;
		System.out.println("t1.level = " + t1.level + "t2.level = " + t2.level); // 47 47 
		t1.level = 27;
		System.out.println("t1.level = " + t1.level + "t2.level = " + t2.level); // 27 27
	}
}
class Tank{
   
	int level;
}

别名现象:不同名字的变量指向的却是同一个引用。

练习二:创建一个包含float域的类,并用这个类来展示别名机制

//Demo.java
public class Demo{
   
	public static void main(String args[]){
   
		Test t = new Test();
		t.f = 1.0f;
		Test t2 = new Test();
		t2.f = 2.9f;
		System.out.println(t.f);//1.0
		System.out.println(t2.f);//2.9
		t = t2;
		System.out.println(t.f);//2.9
		System.out.println(t2.f);//2.9
		t.f = 3.9f;
		System.out.println(t.f);//3.9
		System.out.println(t2.f);//3.9
	}
}
class Test{
   
	float f ;
}

3.4.1 方法调用中的别名问题

将一个对象传递给方法的时候,也会出现别名问题

//PassObject.java
public class PassObject{
   
	public static void main(String args[]){
   
		Letter x = new Letter();
		x.c = 'a';
		System.out.println(x.c);//a
		f(x);
		System.out.println(x.c);//z
	}
	static void f(Letter y){
   
		y.c = 'z';
	}
}

class Letter{
   
	char c;
}

常见一个包含float域的类,展示方法调用时的别名机制

//Demo.java
public class Demo{
   
	public static void main(String args[]){
   
		Test t = new Test();
		t.f = 3.0f;
		System.out.println(t.f);
		change(t);
		System.out.println(t.f);
	}
	static void change(Test t){
   
		t.f = 1.0f;
	}
}
class Test{
   
	float f;
}

3.5算术操作符

  • +
    
  • - 
    
  • *
    
  • /
    
  • %
    运算和赋值操作符 x += 4;
import java.util.Random;
public class MathOps{
   
	public static void main(String args[]){
   
		Random rand = new Random(47);
		int i, j, k;
		j = rand.nextInt(100) + 1;
		System.out.println("j = " + j);
		k = rand.nextInt(100) + 1;
		System.out.println("k = " + k);
		i = j + k;
		System.out.println("j + k = " + i);
		i = j - k;
		System.out.println("j - k = " + i);
		i = k / j;
		System.out.println("k / j = " + i);
		i = k * j;
		System.out.println("k * j = " + i);
		i = k % j;
		System.out.println("k % j = " + i);
		
		// float
		float u, v, w;
		v = rand.nextFloat();
		System.out.println("v = " + v);
		w = rand.nextFloat();
		System.out.println("w = " + w);
		u = v + w;
		System.out.println(" v + w = " + u);
		u = v - w; 
		System.out.println("v - w = " + u);
		u = v * w;
		System.out.println("v * w = " + u);
		u = v / w;
		System.out.println("v / w = " + u);
		u = v % w;
		System.out.println(u);
	}
}

练习四:计算速度的程序,使用的距离和时间都是常量

public class Demo{
   
	public static void main(String args[]){
   
		double distance = 10.3;
		double time = 3;
		double speed = distance / time;
		System.out.println(speed);
	}
}

3.6 自动递增和递减

递增和递减是java提供的快捷运算
前缀式:++ a 先执行运算,在计算值
后缀式:a++ 先生成值,在执行运算

public class AutoInc{
   
	public static void main(String args[]){
   
		int i = 1;
		System.out.println("i = " + i); // i = 1
		System.out.println("i" + ++i); //i = 2
		System.out.println("i" + i++); // i = 2
		System.out.println("i" + i); // i = 3
		System.out.println("i" + --i); // i =2 
		System.out.println("i" + i--); // i =2 
		System.out.println("i" + i); // i =1 
	}
}

3.7 关系操作符

关系操作符生成的是一个boolean值,计算的是操作数之间的数值的关系。
关系操作符由 > < >= <= == !=

== 和 !=可以适用与所有的基本类型,其他的操作符不可以操作Boolean类型

3.7.1 测试对象的等价性

关系操作符== 和!=适用于所有的对象

public class Equivalence{
   
   public static void main(String args[]){
   
   	Integer n1 = new Integer(47);
   	Integer n2 = new Integer(47);
   	System.out.println(n1 == n2); //false
   	System.out.println(n1 != n2); //true
   }
}
//值虽然是相同的,但是对象的引用确实不同的。== 和!=比较的式对象的引用

如果想要比较对象的实际内容是否相同怎么办呢。使用equlas方法,但是并不适用与基本类型

public class EqualsM
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值