java变量、常量、运算符

变量
public class HelloWorld {
	//变量是可以改变的
	public static void main(String[] args) {
		//String:字符串
		String name = "iphone 100";
		//int:整数类型
		int memory = 256;
		//double:小数类型
		double price = 10.99;
		//boolean: 布尔值 true或false
		boolean isRain = true;
		//char 必须用单引号,只能放一个字
		char sex = '女';
		
		System.out.println("手机名:" + name);
		System.out.println("内存大小:" + memory);
		System.out.println("价格:" + price);
		System.out.println(sex);

		float pai = (float) 3.14;
		System.out.println(pai);
		
		pai = (float) 3.141592657;
		System.out.println(pai);
	}
}
常量
public class HelloWorld {
	//常量的值是不能改变的
	public static void main(String[] args) {
		//final声明一个常量
		final float pai = (float) 3.14;
		System.out.println(pai);
		
		pai = (float) 3.141592657; //因为常量不可修改,所以这一步会报错
	}
}
运算符
public class HelloWorld {
	//常量的值是不能改变的
	public static void main(String[] args) {
		//算数运算符: +-*/%
		//%:取余
		int x = 25,y = 20;
		System.out.println(x + y); //45
		System.out.println(x - y); //5
		System.out.println(x * y); //500
		System.out.println(x / y); //1
		System.out.println(x % y); //5
		
		//赋值运算符:
		x += y;
		System.out.println(x); //45
		
		//关系运算符: == != < > <= >=
		System.out.println(x == y); // false
		x = 20;
		y = 20;
		System.out.println(x == y); // true
		
		//逻辑运算符: &&并且 ||或 !非
		int z = 30;
		// &&运算符: 所有条件成立,才会返回true,否则返回false
		System.out.println(x == y && x != z); // true
		z = 20;
		System.out.println(x == y && x != z); // false
		// ||运算符: 只要有一个条件成立,就会返回true,所有条件都不成立,才会返回false
		System.out.println(x == y || x != z); //true
		System.out.println(x != y || x != z); //false

		//位运算符: & | ^
		//& 每一位都是1,结果为1,否则为0
		//| 每一位都是0,结果为0,否则为1
		//^ 每一位的值相同,结果为0,否则为1
		int c = 3,d = 5;
		//c: 011
		//d: 101
		System.out.println("二进制:" + Integer.toBinaryString(c)); //11
		System.out.println(c & d); // 1
		System.out.println(c | d); // 7
		System.out.println(c ^ d); // 6
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_45679977/article/details/105106455