Day07
Java语言支持如下运算符:
优先级
使用优先级为 1 的小括号可以改变其他运算符的优先级,即如果需要将具有较低优先级的运算符先运算,则可以使用小括号将该运算符和操作符括起来
算术运算符:+,,*,l,%,++,--。
package operator;
public class Demo01 {
public static void main(String[] args) {
//二元运算符
int a = 10;
int b = 10;
int c = 10;
int d = 55;
System.out.println(d%b);
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 12312313123123L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);//long
System.out.println(b+c+d);//int
System.out.println(c+d);//int
//如果这两个操作或多个操作中有一个long类型那么结果类型位long如果没有long类型结果位int类型(double同理)
}
}
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增,自减 一元运算符
int a = 3;
int b = a++;//执行完这段代码后,先给b赋值,在自增
//相当于隐藏了一段代码
// a = a + 1;
System.out.println(a);
//a++ a = a + 1 ;
int c = ++a;//执行完这段代码前,先自增,在给c赋值
//++ --是相对应的
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算 2^3 2*2*2 = 16 很多运算,我们会使用一些工具来操作
double pow = Math.pow(2, 9);
System.out.println(pow);
}
}
赋值运算符=
关系运算符:>,<,>=,<=,==,!= instanceof
package operator;
//关系运算符
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果;只会有正确和错误(布尔值表示)
//常用于if
int a = 10;
int b = 20;
int c = 21;
//取余,也叫做模运算
System.out.println(c%a);
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
逻辑运算符: &&,||,!。
package operator;
//逻辑运算符
public class Demo05 {
public static void main(String[] args) {
//与(and) 或(or) 非(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b;"+(a&&b));//逻辑与运算;两个变量都为真,则结果为true。
System.out.println("a || b;"+(a||b));//逻辑或运算;有一个变量都为真,则结果为true。
System.out.println("!(a && b);"+!(a&&b));//如果是真,则变为假。如果是假,则变为真。
//短路运算;逻辑与第一个变量为假,则会直接输出false,后面就不会进行计算了。
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
}
}
package operator;
//位运算符
public class Demo06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
-------------------------
A&B = 0000 1100 对应位都为1则为1,否则为0。
A|B = 0011 1101 对应位都为0则为0,否则为1。
A^B = 0011 0001 对应位相同则为0,不同则为1。
~B = 1111 0010 对应位全部取反。
2*8 = 16 2*2*2*2
效率极高
<< 把数字*2
>> 把数字/2
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(3<<3);
}
}
条件运算符 ? ∶ 。
package operator;
//三元运算符
public class Demo08 {
public static void main(String[] args) {
// x ? y ; z
//如果x==true,则结果为y,否则结果位z
int score = 100;
String type = score < 60 ?"不及格":"及格";//必须掌握
System.out.println(type);
}
}
扩展赋值运算符:+=,-=,*=,/=
package operator;
//赋值运算符
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;//a = a + b
a-=b;//a = a - b
System.out.println(a);
//字符串连接符; 在+两侧只要出现了string类型,那么输出时会将其他操作数全部变为字符串类型然后进行拼接
//字符串在前面后面的就会进行拼接,字符串在后面前面的就会进行运算。
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
推荐这些文章:
public class B {
public static void main(String[] args) {
int bb = 5;
int pp = bb++;
System.out.println(pp);
}
}
...
==============================================
public class Helllo
{
static boolean bool;
static byte by;
static char ch;
static double d;
static float f;
static int i;
static long l;
static short sh;
static String str;
final ...
enum Singleton {
INSTANCE;
public void m() {
System.out.println("test");
}
}
...
public class B {
public static final void main(String[] args) {
System.out.println(System.getProperty("bb"));
System.out.println(System.getProperty("file.encoding"));
}
}
...
public static void main(String[] args) {
int a = 123456;
int b = 6546;
a = a^b;
b= a^b;
a = b^a;
System.out.println("a:"+a);
System.out.println("b:"+b);
}
知识点:a^b^a = b 自己位或自己=空气
...
继承的类的静态成员变量是自身的时候为啥不爆StackOverflowError
问题
有两个方面的问题:
1,当一个类B继承一个类C,而类B的一个实例又是类C的一个静态成员变量的时候,为什么不会报StackOverflowError,我知道类B的一个实例是类C的一个非静态成员变量的时候会报这个错误。虽然静态的只有一个实例,但引用还是是无限循环的啊?
2.当一个类B继承一个类C、一个接口A,类C中和接口A中都有一个相同的成员变量b的时候,分别因该怎么引用?(其中类C中的b是static的)
可能没有说的太清楚,我把代码贴出来了,求解答,困惑我很久了。。
public class InterfaceVariable {
...
1.整型数据之间的类型转换
查看代码
package operator;
public class Demo01 {
public static void main(String[] args) {
long a = 12345678911L;
int b = 124578;
short c = 100;
byte d = 15;
//ctrl + D 复制当前行到下一行
System.out.println(a + b + c + d); //long型
...
1 public class Hello{
2 public static void main(string[] args){
3 System.out.print("hello world!");}
4 }
...
文章链接:https://www.dianjilingqu.com/4416.html
本文章来源于网络,版权归原作者所有,如果本站文章侵犯了您的权益,请联系我们删除,联系邮箱:saisai#email.cn,感谢支持理解。