运算符
Java语言支持如下运算符:
- 算术运算符:+,-,*,/,%(取余),++,–
- 复制运算符: =
- 关系运算符:>,<,>=,<=,==,!=,instanceof
- 逻辑运算符:&&,||,!
- 位运算符:&, |, ^, ~, >>, <<, >>>
- 条件运算符: ?:
- 扩展赋值运算符:+=, -= , *= ,/=
算术运算符:
public static void main(String[] args) {
//二元运算符
int a = 2;
int b = 6;
int c = 9;
int d = 10;
System.out.println(c%b);//取余 模运算
System.out.println(a+b);
System.out.println(d/b);
System.out.println(d/(float)b);
System.out.println((double)d/b);
run:
3
8
1
1.6666666
1.6666666666666667
运算时的类型变化:
long a1 = 1500000000000L;
int b1 = 15000000;
short c1 = 15000;
byte d1 = 112;
System.out.println(a1+b1+c1+d1);//多个数操作中有一个类型是long,其结果也是long
System.out.println(b1+c1+d1);//操作中没有long类型,无论是否有int类型,结果都为int类型
System.out.println(c1+d1);//int 类型
//英文: cast 转换
run:
1500015015112
15015112
15112
关系运算符:
//关系运算符返回的结果:正确 或 错误 (布尔值)
int a2 = 10;
int b2 = 20;
int c2 = 20;
System.out.println(a2>b2);
System.out.println(a2<b2);
System.out.println(b2==c2);
System.out.println(a2!=b2);//!= 不等于
run:
false
true
true
true
自增和自减运算符:
// ++ 自增运算符 -- 自减运算符 一元运算符
int a = 5;
int b = a++;// a++ : a = a+1
int c = b++;// 变量在前,运算符在后,先给b赋值,然后再加自增
System.out.println(a);
System.out.println(b);
System.out.println(c);
int d = ++c;// 变量在后,运算符在前,先自增,再给d赋值
System.out.println(c);
System.out.println(d);
//幂运算 (次方) 2*2*2=8 (很多运用会使用工具类来操作)
double pow = Math.pow(2, 3); //Math.pow
System.out.println(pow);
run:
6
6
5
6
6
8.0
逻辑运算符:
// && 与 || 或 !非(取反)
boolean a = true;
boolean b = false;
boolean c = true;
boolean d = false;
System.out.println("a&&c:"+(a&&c)); //逻辑与运算:两个变量都为真,结果才为真
System.out.println("a&&b:" +(a&&b));
System.out.println("a||b:" +(a||b)); //逻辑或运算:两个变量有一个结果为真,则结果为真
System.out.println("a||c:" +(a||c));
System.out.println("b||d:" +(b||d));
System.out.println("!(a&&b):" +(!(a&b)));// 如果是真则为假,如果是假则变为真
System.out.println("!(a&&c):" +(!(a&c)));
System.out.println("!(b&&d):" +(!(b&d)));
run:
a&&c:true
a&&b:false
a||b:true
a||c:true
b||d:false
!(a&&b):true
!(a&&c):false
!(b&&d):true
短路运算:
int a1 = 5;
boolean b1 =(a1<5)&&(++a1==5);//短路运算,如果前者判断为错,则不执行后面的代码
System.out.println(b1);
System.out.println(a1);
run:
false
5 //a1<5判断为错 则短路运算 ++a1==5就没有去执行了
位运算符:
/*
A = 0011 1100
B = 0000 1101
A&B = 0000 1100 (两个都为则为1,否则为0)
A|B = 0011 1101 (有一个是1则为1)
A^B = 0011 0001 (两个相同为0,不相同为1)
~B = 1111 0010 (和B相反)
<< 左移
>> 右移
*/
System.out.println(2<<1); //左移相当于是 *2
System.out.println(2<<2);
System.out.println(2<<3);
System.out.println("~~~~~~~~~~~~~~");
System.out.println(3<<1);
System.out.println(3<<2);
System.out.println(3<<3);
System.out.println("~~~~~~~~~~~~~~~");
System.out.println(2>>1);
System.out.println(2>>2);
System.out.println(2>>3);
System.out.println("~~~~~~~~~~~~~~~");
System.out.println(24>>3); //右移相当于是 /2
System.out.println(98>>3);
System.out.println(99>>3);
run:
4
8
16
~~~~~~~~~~~~~~
6
12
24
~~~~~~~~~~~~~~~
1
0
0
~~~~~~~~~~~~~~~
3
12
12
扩展赋值运算符:
public static void main(String[] args) {
int a = 2;
int b = 5;
int c = 6;
int d = 9;
a+=b; //等于 a=a+b
c-=d; //等于 c=c-d
System.out.println(a);
System.out.println(c);
System.out.println("===========");
//字符串连接符 + (+连接中如果出现String类型,则会把操作数都转化为String然后连接)
System.out.println(b+d);
System.out.println(""+b+d);
System.out.println(b+d+"");//String类型在后面,则会先进行前面的运算
run:
7
-3
===========
14
59
14
三元运算符:
//三元运算符
/*
x ? y : z
如果x==true则为y,否则为z
例如: */
int scora1 = 50;
int scora2 = 60;
int scora3 = scora1>=60 ? 1:0;
int scora4 = scora2>=60 ? 1:0;
System.out.println(scora3);
System.out.println(scora4);
String socorb1 = scora1>=60 ? "及格":"不及格";
String socorb2 = scora2>=60 ? "及格":"不及格";
System.out.println(socorb1);
System.out.println(socorb2);
run:
0
1
不及格
及格
包机制
- 为了更好的组织类,Java提供了包机制,用于区别类名的命名空间。
- 包语句的语法格式为:
package pkg1[. pkg2[. pkg3..]];
- 一般利用公司域名倒置作为包名;
- 为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包,使用“import”语句可完成此功能
import package1[.package2...].(classname|*);
package com.lingxiang.data.January;
//导入其它包中的类的格式,(*是导入包中所有的类)
import com.lingxiang.data.*;
import java.util.Date;//导入包 (代码不能在package上面)
public class A0122f {
public static void main(String[] args) {
Date //需要导入包才能使用
JavaDoc
- javadoc命令是用来生成自己API文档的
参数信息
- @author 作者名
- @version 版本号
- @since 指明需要最早使用的jdk版本
- @param 参数名
- @return 返回值情况
- @throws 异常抛出情况
package com.lingxiang.data.January;
/**
* @author Watermelon
* @version 1.0
* @since 1.0
*/
public class A0122g {
String name;
/**
* @author Watermelon
* @param name
* @return
* @throws Exception
*/
public String test(String name)throws Exception{
return name;}
}
生成JavaDoc文档:
在dos中输入:
javadoc -encoding UTF-8 -charset UTF-8 A0122g.java
遇见狂神说,https://space.bilibili.com/95256449?spm_id_from=333.788.b_765f7570696e666f.1