一.自动类型提升(隐式转换)
当byte、short、char在参与运算时,会被自动提升为int类型,如果还有long、float、
double参与运算,则自动转换为long、float、double类型
package org.westos;
public class Demo1 {
public static void main(String[] args) {
byte a = 1;
short b = 3;
//short c= (a+b);a和b在运算前被提升为int类型,short类型的c无法接收
int d = a + b;//d=4
System.out.println(d);
char e='a';
int f=a+b+e;
System.out.println(f);//f=101
long g=4l;
float f1=3.14f;
double f2=5.28;
//float f3=g+f1+f2;//有double类型参与,应该转为double类型,报错
//long f4=g+f1+f2;//long类型无法存储小数,报错
//int h=a+b+g;//报错,int类型的h无法接收long类型的g
}
}
二、强制类型转换
丢弃多余字节,比如int类型接收byte类型,可丢弃前三个字节,但是这样会带来精度的损失
public class Demo1 {
public static void main(String[] args) {
byte a = 1;
short b = 127;
//short c= (a+b);a和b在运算前被提升为int类型,short类型的c无法接收
byte c= (byte) (a+b);//强制类型转换,将原来为int类型的a+b强制转换为byte类型
System.out.println(c);//c=-128;
short c2=(short)(a+b);
System.out.println(c2);//c2=128
三、常量优化机制
多个常量在参与运算时,会先计算结果,看结果是否在该数据类型的范围内,
如果在,直接将该值赋值给变量(注意是常量而不是变量)
int a=3;
int b=4;
int d=127;
//byte c1=a+b;报错,需要强制类型转换
byte c2=3+4;
System.out.println(c2);//c2=7
//byte c3=3+4+127;报错,c3所表示的数值已经超过byte类型
四、Java语言中的汉字存储
问:java中能否存储汉字?
答:可以,Java中采用的是Unicode编码,可以用来存储汉字
若采用的编码方式为GBK,一个汉字需要占到两个字节(默认)
若采用的编码方式为UTF-8,一个汉字需要占到三个字节
char ch='张';
System.out.println(ch);//张
五、运算符
定义:操作变量的常量的符号
运算符“+号:
作用:(1)起相加作用,用于数值相加
(2)起拼接作用,当+号两端有字符串时,就会起到拼接作用
记住ASCII表中的三个值:
0:48
'a':97
'A':65
package org.westos;
public class Demo3 {
public static void main(String[] args) {
//1.数值相加
System.out.println('a'+1);//97+1
System.out.println(200+34+2);
System.out.println('A'+25);//65+25
System.out.println('0'+20);//48+20
//2.字符串拼接作用
System.out.println(1+"Hello");//1Hello
System.out.println('a'+1+"Hello");//97+1+"Hello"="98Hello"
System.out.println("Hello"+'a'+1);//"Helloa1"
System.out.println("8+8="+8+8);//"8+8=88"
System.out.println(8+8+"=8+8");//"16=8+8"
}
}
1、算数运算符:+,-,*,/,%,++,--
注意事项:
(1)整数相除只能得到整数,用浮点数相除可得到小数
(2)/得到的是商,%得到的是余数
%得到结果的符号,取决于左边的符号
单行注释:Ctrl+/
多行注释:ctrl+shift+ /
package org.westos;
public class Demo4 {
public static void main(String[] args) {
/*
* 单行注释:Ctrl+/
* 多行注释:Ctrl+Shift+/
* 算数运算符: +, - , * , / , % ,++, --
* */
int a=20;
int b=200;
System.out.println(a+b);//220
System.out.println(a-b);//-180
System.out.println(a*b);//4000
int c=10;
int d=3;
System.out.println(c/d);//3
System.out.println(c%d);//1
System.out.println(-10%3);//-1
System.out.println(-10%-3);//-1
int e=10;
double f=3.0;
double g=e/f;
double h=e%f;
System.out.println(g);//3.3333333333333335
System.out.println(h);//1.0
}
}
++和--的作用:
++i:置前自增 --i:置前自减:先运算,后使用
i++:置后自增 i--:置后自减:先使用,后运算
package org.westos;
public class Demo5 {
public static void main(String[] args) {
int i=3;
System.out.println(i++);//执行完语句后,再进行增加的操作 3
System.out.println(i);// 4
System.out.println(++i);//先增加,后打印 5
int j=4;
System.out.println(j--);//4
System.out.println(j);//3
System.out.println(--j);//2
int v=1;
int w=2+(v++)+2-(++v)-1-(v--)+2+(--v)+3;
System.out.println("v="+v); //v=1
System.out.println("w="+w);//w=4
}
}
2.赋值运算符:
=,+=,-=,/=,*=,%=…
注意事项:
以byte类型的a为例:
a+=10支持自动装箱,即自动类型转换
a=a+10不支持
package org.westos;
public class Demo6 {
public static void main(String[] args) {
int a=20;//将20赋值给a
System.out.println(a);
//a=a+20;
//System.out.println(a);a=40
// a+=20;
//System.out.println(a);//相当于a=a+20=40;
byte b=10;
b=b+50;//报错,byte类型的c无法接收int类型的60
b+=50;
System.out.println(b);//b=60
}
}
3.关系运算符:==,!=,<,>,<=,>=
返回值只有两种:true 或者false
package org.westos;
public class Demo7 {
public static void main(String[] args) {
int a=10;
int b=20;
boolean flag=a<b;
System.out.println(flag);//true
System.out.println(a==b);//false
System.out.println(a!=b);//true
System.out.println(a<=b);//true
System.out.println(a>=b); //false
}
}