狂神说学Java-04基础语法学习(下)
时间:2021-05-24 13:24:18
收藏:0
阅读:0
基础语法学习
5.运算符
Java语言支持一下运算符
- 算术运算符:+,-,*,/,%,++,--
- 赋值运算符 =
- 关系运算符: >,<,>=,<=,==,!=,instanceof
- 逻辑运算符:&&,||,!
- 位运算符:&,|,^,~,>>,<<,>>>(了解)
- 条件运算符 ? :
- 扩展赋值运算符:+=,-=,*=,/=
新建一个base包
然后把这些类移动到base包中
创建一个operator包,在这个包中新建类
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 123123123123123L;
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
}
}
package operator;
public class Demo04 {
public static void main(String[] args) {
int a = 3;
int b = a++;//执行完这行代码后,先给b赋值,再自增
int c = ++a;//执行完这行代码后,先自增,再给c赋值
System.out.println(a);//5
System.out.println(b);//3
System.out.println(c);//5
}
}
注意短路特性
package operator;
public class Demo05 {
public static void main(String[] args) {
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);//false
System.out.println(c);//5
}
}
位运算
package operator;
public class Demo06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
----------------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~B = 1111 0010
2*8 16 2*2*2*2
位运算效率极高!!!
<<相当于*2
>>相当于/2
*/
System.out.println(2<<3);
/*
0000 0010 是2
0000 0100 是4
0000 1000 是8
0001 0000 是16
*/
}
}
拓展:字符串连接符 +
package operator;
public class Demo07 {
public static void main(String[] args) {
//字符串连接符
int a = 10;
int b = 20;
System.out.println(""+a+b);//"1020"
System.out.println(a+b+"");//"30"
}
}
6.包机制
一般使用公司域名倒置作为包名
javadoc
- javadoc命令是用来生成自己API文档的
- 参数信息:
- @author 作者名
- @version 版本号
- @since 指明需要最早使用的jdk版本
- @param 参数名
- @return 返回值情况
- @throws 异常抛出情况
package com.brucezhang.base;
/**
* @author brucezhang
* @version 1.0
* @since 1.8
*/
public class Doc {
String name;
/**
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;
}
}
在文件管理器中打开这个Doc的类
在当前目录中打开cmd,运行命令
打开生成的index.html
评论(0)