+ - * /java
public class HelloWorld { public static void main(String[] args) { int i = 10; int j = 5; int a = i+j; int b = i - j; int c = i*j; int d = i /j; } }
若是有任何运算单元的长度超过int,那么运算结果就按照最长的长度计算spa
int a = 5;
long b = 6;
a+b
结果类型是long
public class HelloWorld { public static void main(String[] args) { int a = 5; long b = 6; int c = (int) (a+b); //a+b的运算结果是long型,因此要进行强制转换 long d = a+b; } }
若是任何运算单元的长度都不超过int,那么运算结果就按照int来计算code
byte a = 1;
byte b= 2;
a+b
结果类型是int
public class HelloWorld { public static void main(String[] args) { byte a = 1; byte b= 2; byte c = (byte) (a+b); //虽然a b都是byte类型,可是运算结果是int类型,须要进行强制转换 int d = a+b; } }
public class HelloWorld { public static void main(String[] args) { int i = 5; int j = 2; System.out.println(i%j); //余数为1 } }
++ -- 在原来的基础上增长1或者减小1blog
public class HelloWorld { public static void main(String[] args) { int i = 5; i++; System.out.println(i);//输出为6 } }
i++;
先取值,再运算++i;
先运算,再取值
public class HelloWorld { public static void main(String[] args) { int i = 5; System.out.println(i++); //输出5 System.out.println(i); //输出6 int j = 5; System.out.println(++j); //输出6 System.out.println(j); //输出6 } }
- > 大于
- >= 大于或等于
- < 小于
- <= 小于或等于
- == 是否相等
- != 是否不等
public class HelloWorld { public static void main(String[] args) { int a = 5; int b = 6; int c = 5; System.out.println(a>b); //返回 false System.out.println(a>=c); //返回 true System.out.println(a==b); //返回false System.out.println(a!=b);//返回true } }
不管长路与
&
仍是短路与&&
,两边的运算单元都是布尔值ip
- 都为真时,才为真
- 任意为假,就为假
区别字符串
- 长路与
&
两侧,都会被运算- 短路与
&&
只要第一个是false,第二个就不进行运算了
public class HelloWorld { public static void main(String[] args) { //长路与 不管第一个表达式的值是true或者false,第二个的值,都会被运算 int i = 2; System.out.println( i== 1 & i++ ==2 ); //不管如何i++都会被执行,因此i的值变成了3 System.out.println(i); //短路与 只要第一个表达式的值是false的,第二个表达式的值,就不须要进行运算了 int j = 2; System.out.println( j== 1 && j++ ==2 ); //由于j==1返回false,因此右边的j++就没有执行了,因此j的值,仍是2 System.out.println(j); } }
不管长路或
|
仍是短路或||
,两边的运算单元都是布尔值it
- 都为假时,才为假
- 任意为真,就为真
区别class
- 长路或
|
两侧都会被运算- 短路或
||
只要第一个是true的,第二个就不进行运算了
public class HelloWorld { public static void main(String[] args) { //长路或 不管第一个表达式的值是true或者false,第二个的值,都会被运算 int i = 2; System.out.println( i== 1 | i++ ==2 ); //不管如何i++都会被执行,因此i的值变成了3 System.out.println(i); //短路或 只要第一个表达式的值是true的,第二个表达式的值,就不须要进行运算了 int j = 2; System.out.println( j== 2 || j++ ==2 ); //由于j==2返回true,因此右边的j++就没有执行了,因此j的值,仍是2 System.out.println(j); } }
取反
!
import
- 真变为假
- 假变为真
public class HelloWorld { public static void main(String[] args) { boolean b = true; System.out.println(b); //输出true System.out.println(!b);//输出false } }
异或
^
基础
- 不一样,返回真
- 相同,返回假
public class HelloWorld { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println(a^b); //不一样返回真 System.out.println(a^!b); //相同返回假 } }
略
赋值操做
- =
对自己进行运算,并赋值
- +=
- -=
- *=
- /=
- %=
- &=
- |=
- ^=
- <<=
- >>=
- >>>=
赋值操做的操做顺序是从右到左
int i = 5+5;
- 首先进行
5+5
的运算,获得结果10
,而后把10
这个值,赋给i
public class HelloWorld { public static void main(String[] args) { int i = 5+5; } }
+=即自加
i+=2;
i=i+2;
其余的 -= , *= , /= , %= , &= , |= , ^= , >= , >>>= 都是相似,不作赘述
public class HelloWorld { public static void main(String[] args) { int i =3; i+=2; System.out.println(i); int j=3; j=j+2; System.out.println(j); } }
表达式?值1:值2
- 若是表达式为真 返回值1
- 若是表达式为假 返回值2
public class HelloWorld { public static void main(String[] args) { int i = 5; int j = 6; int k = i < j ? 99 : 88; // 至关于 if (i < j) { k = 99; } else { k = 88; } System.out.println(k); } }
使用Scanner类,须要在最前面加上
import java.util.Scanner;
表示导入这个类,才可以正常使用
import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner s = new Scanner(System.in); int a = s.nextInt(); System.out.println("第一个整数:"+a); int b = s.nextInt(); System.out.println("第二个整数:"+b); } }
import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner s = new Scanner(System.in); float a = s.nextFloat(); System.out.println("读取的浮点数的值是:"+a); } }
import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner s = new Scanner(System.in); String a = s.nextLine(); System.out.println("读取的字符串是:"+a); } }
- 若是在经过
nextInt()
读取了整数后,再接着读取字符串,读出来的是回车换行:"\r \n",由于nextInt
仅仅读取数字信息,而不会读走回车换行"\r \n".- 若是在业务上须要读取了整数后,接着读取字符串,那么就应该连续执行两次
nextLine()
,第一次是取走整数,第二次才是读取真正的字符串
import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner s = new Scanner(System.in); int i = s.nextInt(); System.out.println("读取的整数是"+ i); String rn = s.nextLine(); String a = s.nextLine(); System.out.println("读取的字符串是:"+a); } }