发布于2021-03-10 18:41 阅读(906) 评论(0) 点赞(5) 收藏(1)
1 public class JavaDemo { 2 public static void main(String[] args) { 3 System.out.println('a' + 1); // 98,因为有ASCII,a字符对应的是int类型97 4 System.out.println((char) ('a' + 1)); // b 5 6 // 任何数据类型用 + 与字符串相连接都会产生新的字符串 7 System.out.println("hello" + 'a' + 1); // helloa1 8 System.out.println('a' + 1 + "hello"); // 98hello 9 10 System.out.println("5 + 5 = " + 5 + 5); // 5 + 5 = 55 11 System.out.println("5 + 5 = " + (5 + 5)); // 5 + 5 = 10 12 } 13 }
1 public class JavaDemo { 2 public static void main(String[] args) { 3 char c = 'a'; 4 System.out.println(c); 5 6 byte b = 100; 7 char c1 = 97; // 0 ~ 65535 8 System.out.println(c1); 9 10 // char类型是否可以存储中文 11 char x = '中'; 12 System.out.println(x); 13 } 14 }
1 public class JavaDemo { 2 public static void main(String[] args) { 3 /* 4 * a:+号在java中有三种作用,代表正号,做加法运算,字符串的连接符 5 * b:整数相除只能得到整数。如果想得到小数,必须把数据变化为浮点数类型 6 * c:/获取的是除法操作的商,%获取的是除法操作的余数 7 * %运算符 8 * 当左边的绝对值小于右边绝对值时,结果是左边 9 * 当左边的绝对值等于右边或是右边的倍数时,结果是0 10 * 当左边的绝对值大于右边绝对值时,结果是余数 11 * %运算符结果的符号只和左边有关系,与右边无关 12 * 任何一个正整数%2结果不是0就是1可以用来当作切换条件 13 */ 14 // 除/ 15 System.out.println(10 / 3); // 3 整数相除结果只能是整数 16 System.out.println(10 / 3.0); // 3.3333333333333335 如果想得到小数,把其中一个 数变成小数,另一个在运算的时候会自动类型提升 17 // 模 % 18 System.out.println(3 % 5); // 3 如果想得到小数,把其中一个 数变成小数,另一个在运算的时候会自动类型提升 19 System.out.println(5 % 5); 20 System.out.println(15 % 5); 21 } 22 } 23 // 简化运算符 24 public class JavaDemo { 25 public static void main(String[] args) { 26 int num = 20; 27 num = num + 10; 28 System.out.println(num); 29 } 30 } 31 32 对于以上“ num = num + 10;”在内存小的时代就不适合了。所以才提供有一系列简化的运算符(+=、*=、-=、/=、%=)支持 33 34 public class JavaDemo { 35 public static void main(String[] args) { 36 int num = 20; 37 num += 10; 38 System.out.println(num); 39 } 40 }
1 // 实现自增与自减 2 public class JavaDemo { 3 public static void main(String[] args) { 4 int x = 10 ; 5 int y = 20 ; 6 // 1、++ x:首先x的内容要先自增1,为11; 7 // 2、y --:先进行计算,使用的内容是20,计算完成后自减 8 int result = ++ x - y -- ; 9 System.out.println("计算结果:" + result) ; // -9 10 System.out.println("x = " + x) ; // 11 11 System.out.println("y = " + y) ; // 19 12 } 13 }
1 // 判断相等 2 public class JavaDemo { 3 public static void main(String[] args) { 4 int x = 10 ; 5 int y = 10 ; 6 boolean flag = x == y ; // true 7 System.out.println(flag) ; 8 } 9 } 10 进行关系运算的时候可以针对所有的基本数据类型,例如:也可以直接使用字符进行处理 11 // 在字符处理上使用关系运算 12 public class JavaDemo { 13 public static void main(String[] args) { 14 char c = '剑'; 15 boolean flag = 21073 == c ; // true 16 System.out.println(flag) ; 17 } 18 }
数据类型之间提供有转型支持,所以在进行判断的时候字符会自动转为int后进行数字的比较
1 public class JavaDemo { 2 public static void main(String[] args) { 3 // &(逻辑与)、|(逻辑或)、^(逻辑异或)、!(逻辑非) 4 // 逻辑与 & 并且and 遇到false则false 5 int a = 10; 6 int b = 20; 7 int c = 30; 8 System.out.println(a < b & b < c); // true & true = true 9 System.out.println(a < b & b > c); // true & false = false 10 System.out.println(a > b & b < c); // false & true = false 11 System.out.println(a > b & b > c); // false & false = false 12 13 // 逻辑 | 或or 遇true则true 14 System.out.println(a < b | b < c); // true | true = true 15 System.out.println(a < b | b > c); // true | false = true 16 System.out.println(a > b | b < c); // false | true = true 17 System.out.println(a > b | b > c); // false | false = false 18 19 // 逻辑异或^ 两边相同为false,两边不同为true 20 System.out.println(a < b ^ b < c); // true ^ true = false 21 System.out.println(a < b ^ b > c); // true ^ false = true 22 System.out.println(a > b ^ b < c); // false ^ true = true 23 System.out.println(a > b ^ b > c); // false ^ false = false 24 25 // 逻辑非 ! 26 System.out.println(!true); 27 System.out.println(!!true); 28 } 29 }
1 public class JavaDemo { 2 public static void main(String[] args) { 3 /* 4 int a = 10; 5 int b = 20; 6 int c = 30; 7 System.out.println(a < b && b < c); // true && true = true 8 System.out.println(a < b && b > c); // true && false = false 9 System.out.println(a > b && b < c); // false && true = false 10 System.out.println(a > b && b > c); // false && false = false 11 */ 12 int x = 3; 13 int y = 4; 14 // System.out.println((++x == 3) & (++y == 4)); // false & false = false 15 // System.out.println("x = " + x); // 4 16 // System.out.println("y = " + y); // 5 17 System.out.println("---------------------------------"); 18 System.out.println((++x == 3) && (++y == 4)); // false & false = false 19 System.out.println("x = " + x); // x = 4 20 System.out.println("y = " + y); // y = 4 21 } 22 }
1 public class JavaDemo { 2 public static void main(String[] args) { 3 /* 4 * &,|,^,~ 的用法 5 * &:有0则0 6 * |:有1则1 7 * ^:相同则0,不同则1 8 * ~:按位取反 9 */ 10 System.out.println(6 & 3); // 010 - 2 11 System.out.println(6 | 3); // 111 - 7 12 System.out.println(6 ^ 3); // 101 - 5 13 System.out.println( ~ 6); // -5 14 } 15 } 16 /* 17 110 18 & 011 19 ---------- 20 010 21 22 23 110 24 | 011 25 ---------- 26 111 27 28 29 110 30 ^ 011 31 ---------- 32 101 33 34 35 00000000 00000000 00000000 00000110 6的原码反码补码都是本身 36 11111111 11111111 11111111 11111001 对6取反 37 - 00000000 00000000 00000000 00000001 38 --------------------------------------- 39 11111111 11111111 11111111 11111000 反码 40 10000000 00000000 00000000 00000111 原码(-7) 41 */ 42 位异或运算符的特点 43 public class JavaDemo { 44 public static void main(String[] args) { 45 /* 46 * 位异或运算符的特点 47 * ^的特点:一个数据对另一个数据位异或两次,该数本身不变。 48 */ 49 // System.out.println(5 ^ 10 ^ 10); // 5 50 // System.out.println(5 ^ 10 ^ 5); // 5 51 52 /* 53 * 请自己实现两个整数变量的交换(不需要定义第三方变量) 54 */ 55 int x = 5; 56 int y = 10; 57 58 // 需要第三方变量 59 /* 60 int temp; 61 temp = x; 62 x = y; 63 y = temp; 64 */ 65 66 // 不需要定义第三方变量,有可能超出int取值范围 67 /* 68 x = x + y; // 5 + 10 = 15 69 y = x - y; // 15 - 10 = 5 70 x = x - y; // 15 - 5 = 5 71 */ 72 // 不需要第三方变量 73 x = x ^ y; // 5 ^ 10 74 y = x ^ y; // 5 ^ 10 ^ 10 y = 5 75 x = x ^ y; // 5 ^ 10 ^ 5 76 System.out.println("x = " + x + "---" + "y = " + y); 77 } 78 } 79 80 public class JavaDemo { 81 public static void main(String[] args) { 82 /* 83 * >>,>>>,<<的用法: 84 * <<:左移 左边最高位丢弃,右边补齐0 85 * >>:右移 最高位是0,左边补齐0;最高为是1,左边补齐1 86 * >>>:无符号右移 无论最高位是0还是1,左边补齐0 87 * 最有效率的算出2 * 8的结果 88 */ 89 90 // 左移,向左移动几位就是 乘以2的几次幂 91 System.out.println(12 << 1); 92 System.out.println(24 << 2); 93 94 // 右移,向右移动几位就是除以2的几次幂 95 System.out.println(12 >> 1); 96 System.out.println(12 >> 2); 97 98 System.out.println(12 >>> 2); 99 100 // 最有效率的算出2 * 8的结果 101 System.out.println(2 << 3); 102 } 103 }
1 // 判断两个数字的大小,将最大值保存 2 public class JavaDemo { 3 public static void main(String[] args) { 4 int x = 20; 5 int y = 10; 6 // 判断两个数的最大是,如果x大于y就将x赋值给max,反之y赋值给max 7 int max = x > y ? x : y; 8 System.out.println(max) ; 9 } 10 }
1 // 三目运算的嵌套,将三个数字中的最大值保存下来 2 public class JavaDemo { 3 public static void main(String[] args) { 4 int x = 20; 5 int y = 10; 6 int z = 30; 7 // 判断两个数的最大是,如果x大于y就将x赋值给max,反之y赋值给max 8 int max = (x > y) ? (x > z ? x : z ) : (y > z ? y : z); 9 System.out.println(max) ; 10 } 11 } 12 虽然允许进行嵌套处理,但是程序的可读性变得很差,根据实际的情况确定是否使用
原文链接:https://www.cnblogs.com/sftp/p/14504061.html
作者:欧美丽
链接:http://www.javaheidong.com/blog/article/112175/48769ec56ec461942efa/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!