知识点一 变量之间的运算(boolean不能进行运算) 则可以参与运算的类型有 byte short int long float double 运算主要有两种: 1自动类型转换 容量小的数据类型和容量大的数据类型进行运算,容量小的会自动转换为容量大的数据类型 byte与short char 混合远算得到结果是int int与long原酸是long long与float运算是float float与double运算是double 第二种 强制类型转换 例子: int i1 = i2; short s1 =2; int i2 = i1+s1;//两个字节加上四个字节结果就是四个字节 float = f1 =12.3f; float f2 = f1+i1;//f1+i1是float类型 double d1 = f1+12.3;//这是double类型,如果写为 float d1 = f1+12.3;这样就会出错,因 //为12.3是double类型 long l =12;//12是int类型 int比long范围小,可以进行赋值 float f3 = l;//这样也可以赋值,虽然long是八个字节,float是四个字节,但是存储方式不一样 char c1='a'; int i3 = c1+1;//char本质哈市int类型,char与int运算还是int类型 //需要注意的是 char与byte short之间默认运算结果为int类型,包括两个byte运算也是int类型 例子: short ss1 = 10; byte bb1 = 1; int ss2=ss1+bb1;//short是两个字节,byte是一个字节,但结果是int类型 char cc1='a'; int cc2 = cc2+bb1;//char是两个字节,与byte运算结果是四个字节的int类型 //两个short运算也是int类型 short ss1 = 10; short ss4 = 19; int ss10 = ss3_+ss4;//short与shor也是int类型 //当容量大的转换为为容量小的,就需要强制类型转换,要使用强制类型转换符(),在()写入需要强制类型转换类型 //强制类型转换的问题 精读损失 long l1 =1234567L; int int1 = l1;//这样就会报错 int int1 =(int)l1; byte byte1 = (int)int1; 平时用的字符串也是一种数据类型String类型 String nation="我是中国人"; System.out.println(nation); //字符串和基本数据类型之间做运算只能是连接运算 int int2 = 100; String st3=nation+int2;//这个+是连接符号,不是加号,得到结果仍然是一个字符串 这里st3得到结果是我是一个中国人100 题目: //题目: String st1="hello"; int myint10=12; char ch1='a'; System.out.printf(str1+myint10+ch1);//答案是hello12a System.out.printf(myint10+ch1+str1);//答案是109hello ch1先与myint10进行加法运算 System.out.printf(ch1+str1+myint1);//答案是ahello12 +前后有字符串则直接进行合并,否则就进行加法运算! //注意问题 //String str12=10;//基本数据类型直接对字符串进行赋值是不可以的!!! String str12345=123+"adg";//这样是可以的 //进制 /* byte b =13; 最高位是符号位 如果为0是正数 如果是1则是负数 对于正数 原码 补码 二码合一 -13的原码 符号位 1 0 0 0 1 1 0 1 反码 除去符号位 取反 1变为0 0变为1 1 1 1 1 0 0 1 0 补码 反码加上1 1 1 1 1 0 0 1 1 在计算机内部 整数都是用补码进行存储的 如下的byte值是多少? 1 1 1 1 0 0 0 1 首先确定是一个负数的补码 然后减一 变为反码 1 1 1 1 0 0 0 0 取反 再取反变为原码 1 0 0 0 1 1 1 1 /* 运算符是一种符号 ,用来表示数据的运算,赋值和比较 + - * / ++ -- % */ //接下来是测试算术的 int i=12; int j=i/5;//结果是2 double d=i/5;//这个也是2.0 i是int 5是int 结果是2 2是int 赋值给double 就是2.0 double d2=(double)i/5;//这样的结果才是2.4 double d3=i/5.0;//这样的结果也是2.4 5.0是double类型 //% 结果的符号取决于被模数 也就是前面的 只能整数进行取模 int i1=12; int j1=i%5;//结果是2 int j2=-12%5;//-2 int j3=12%-5;//2 int j4=-12%-5;//-2 //前++先自增 后做运算 后++ int myint11=10; int myint12=myint11++;//myint12是10 myint11是11 int myint3=10; int myint4=++myint3;//myint4是 |
|