2020-12-02char,布尔型/单、双目运算符/按位计算符

char字符类型

public class CharDataType{
	public static void main(String []args){
		char sex = '男';
		sex = '女';
		sex = 'M';
		System.out.println("性别 "+sex);
		//特殊符号需要使用转义处理
		System.out.print("输出字符tab键"+'\t'+"前面是tab符号");
		System.out.print('\n'+"输出回车(换行符号)键");
		System.out.print('\\'+"输出斜线");
		System.out.print("输出单引号"+'\'');
/*char 类型与int兼容,char可以直接赋值给int类型*/
		int age = 'A';
		char ch = 'a';
		int age2 = ch;
		System.out.print("年龄是 "+age);
	}
}

布尔类型

import java.util.Scanner;

public class BooleanDataType{
	static Scanner in = new Scanner(System.in);
	public static void main(String []args){
		int num = 0;
		boolean bool;//存储是否已婚信息
		System.out.println("已婚输入数字1,否则输入数字0");
		num = in.nextInt();
		if(num == 1){//使用if进行逻辑判断
			bool = true;
		}else{
			bool = false;
		}
		System.out.println("婚姻结果"+bool);
	}
}

数据类型的转换

/**
java数据类型转换
1.自然转换,从低精度到高精度转换
2.强制转换,从高精度到低精度转换
*/
public class DataTypeConvert{
	public static void main(String []args){
		int num = 5000;
		double dou = 234;
		dou = num;
		num = (int)0.0;//必须进行强制转换
		System.out.println("转换后的结果 "+num);
		short sh = (short)dou;//必须进行强制转换
		System.out.println("转换后的结果 "+sh);
		
		byte by = (byte)130;
		System.out.println(130+"转换为byte后的结果"+by);
	}
}

复合赋值运算符

public class AssignOperator{
	static final int MAX = 1000;
	public static void main(String []args){
		int age;
		age = MAX;
		double heigh = age;
		System.out.println(heigh);
		
		int num = 100;
		//复合赋值运算符
		num += age;//相加后赋值
		System.out.println(num);
		num -= age;//相减后赋值
		System.out.println(num);
		num *= age;//相乘后赋值
		System.out.println(num);
		num /= age;//相除后赋值
		System.out.println(num);
		num %= age;//取余数后赋值
		System.out.println(num);
	}
}

算数运算符(双目运算符和单目运算符)

public class ArithmeticOperator{
	public static void main(String []args){
		//双目运算符,需要2个操作数,从左侧向右侧计算
		int age0 = 12,age2 = 13;
		System.out.println("相加运算"+(age0+age2));
		System.out.println("相减运算"+(age0-age2));
		System.out.println("相乘运算"+(age0*age2));
		System.out.println("相除运算"+(age0/age2));
		/*
		单目运算符,只对变量进行操作 ++【自增1】 --【自减1】
		*/
		int n = 5,m = 6;
		n++;
		System.out.println(5+"自增1的结果是"+n);
		int g = m++;
		System.out.println(g);
		System.out.println(m);
	}
}

要点:i++先赋值后自增

关系运算符
最终对2个操作数进行运算得到boolean类型结果

public class RelationalOperator{
	public static void main(String []args){
	int  mAge = 34,fAge = 40,chiAge = 4;
	boolean bool0 = mAge >fAge;
	System.out.println("妈妈的年龄大于爸爸的年龄"+bool0);
	boolean bool1 = mAge < fAge;
	System.out.println("妈妈的年龄小于爸爸的年龄"+bool1);
	boolean bool3 = (mAge + chiAge) == fAge ;
	System.out.println("妈妈的年龄加上孩子的年龄等于爸爸的年龄"+bool2);
	
	}
}

逻辑运算符
&&逻辑与 ||逻辑或 !逻辑非

public class LogicOperator{
	public static void main(String []args){
		int groupOne = 34,groupTwo = 41,groupThree = 37;
		//第二小组大于第一小组并且小于第三小组人数
		boolean bool = groupTwo > groupOne && groupTwo <groupThree;
		System.out.println(bool);
		//第一小组小于第二小组并且小于第三小组人数
		System.out.println(groupOne < groupTwo && groupOne < groupThree);
	
		/**
		 * 逻辑或||,只要左侧或右侧计算结果都为真(true),结果就是true,左右都为flase最后结果才为flase
		 */
		boolean bool2 ;
		bool2 =groupThree > groupOne || groupThree > groupTwo;
		System.out.println(bool2);
		//第一小组大于第二小组或大于第三小组人数
		System.out.println(groupOne > groupTwo || groupOne > groupThree);
		
		/**
		 * 逻辑非!,对一个布尔结果进行取反操作
		 */
		System.out.println(!!!!!!true);
		System.out.println(!!false);
		System.out.println(!((23+34) > 70) && 1!=2);
		
		int apple = 50,peach = 49,banana = 48;
		
		boolean bool3 = peach > apple && peach == ++banana;
		System.out.println(bool3);
		System.out.println(banana);
	}
}

要点:逻辑与第一个条件为false时不判断下一个条件,逻辑或第一个条件为true时不判断下一个条件

按位计算符

按位与 &
1并1 为 1
0并1 为 0
0并0 为 0

按位或 |
1或1 为 1
1或0 为 1
0或0 为 0

按位非
0 结果为 1
1 结果为 0

按位异或
相同为0,不同为1

public class BitOperator {

	public static void main(String[] args) {
		/*
		 * 按位与&,不短路底层按位进行计算
		 */
		//对布尔进行计算
		int n = 5,m = 6,k = 7;
		
		boolean bool = m < n & m == --k;
		System.out.println(bool);
		System.out.println(k);
		
		/*
		 * 按位或|,不短路底层按位进行计算
		 */
		boolean bool2 = m < n | ++n == m++;
		System.out.println(bool2);
		System.out.println(m);
		System.out.println(n); 
		
		/**
		 * 按位与对整数进行计算
		 */
		
		System.out.println("5和5按位计算结果"+(5 & 4));
		/*
		 *  4二进制表示 0 0100
		 *  5       0 0110
		 *          0 0100
		 */
		System.out.println("5和-5按位计算"+(-5 & -5));
		
		/**
		 * 按位非操作,对一个整数进行按位计算,如果当前位是1则变成0,如果是0则变成1
		 */
		
		System.out.println("5的按位非计算结果为"+ ~5);
		/*
		 * 5 0 0101
		 *   1 1010 按位去翻操作后
		 */
		System.out.println("-6的按位非计算结果为"+ ~-6);
		
		/**
		 * 按位异或 对布尔进行计算,相同为false , 不同为true
		 */
		System.out.println("true 异或 true 结果为"+(1 == 1 ^ 3>2));
		System.out.println("true 异或 false 结果为"+(true ^ false));
		System.out.println("false 异或 false 结果为"+(2>3 ^ 9<2));
		
		/**
		 * 按位异或对整数进行计算,对应位0异或0结果为0,1异或1结果为0,否则为1(相同为0,不同为1)
		 */
		System.out.println("5异或6计算结果"+(5 ^ 6));
		//5 0 0101
		//6 0 0110
		//  0 0011
	}

}

特殊运算符

<<左移
*>>右移

public class MoveBitOperator {
	public static void main(String []args) {
		//左移运算
		int n = 2;
		System.out.println(n+"左移3位的结果是"+(n << 3));
		// 2    0000 0010
		// 16   0001 0000
		
		//移动位数超出最大位限制(实际要移动的位数%32)
		System.out.println(n+"左移34位的结果是"+(n << 34));
		System.out.println(n+"左移34位的结果是"+(n << 34 % 32));
		
		//右移运算
		int m = 5;
		System.out.println(m+"向右移动2位 结果是"+(m >> 2));
		// 0 0101
		// 0 0001
		System.out.println(-9+"向右移动3位 结果是"+(-9 >> 3));
		System.out.println(-9+"向右无符号移动2位 结果是"+(-9 >>> 2 ));
	}
}

特殊运算符

new 只用做创建某个引用数据类型的对象
. 某个对象对属性或方法的引用访问符号
() 包围被优先运算的表达式或方法标志
[] 数组类型数据的标志
instanceof 判断某对象或引用是否属于某种类型

import java.util.Scanner;

public class OtherOperator{
	public static void main(String []args){
		//new 运算符,用来新建对象
		Scanner scanner = new Scanner(System.in);

		//.点号运算符,用来访问对象的属性或方法,或者表示常数的小数标志
		System.out.println("输入一个整数回车");
		int a = scanner.nextInt();
		System.out.println(a);
		
		//( ) 典型的方法标志,在定义方法或调用方法时必须提供
		
		//[ ] 通常用来表示数组
		
		//instanceof 判断某个对象是否属于某个类型的运算符关键字
		
		boolean bool = scanner instanceof Scanner;
		System.out.println(bool);
 	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值