类型转换
由于java是强类型语言,要进行有些运算时要用到类型转换
从低————————————到高
byte short char ----> int ---->long------>float---->double
运算中,不同类型的数据先转换成同一类型,然后进行运算
public class Demo05 {
public static void main(String[] args) {
//高到低 (类型)变量名
int i = 128;
byte b = (byte) i; //内存溢出,-127~128
System.out.println(i);
System.out.println(b);
/*
不能对Boolean进行转换
不能把对象类型转换为不相干的类型
把高容量转换为低容量的时候,强制转换
转换的时候可能存在内存溢出,或者精度问题
*/
System.out.println("==================");
System.out.println((int)23.7); //23
System.out.println((int)45.89f); //45
char c ='a';
int d = c+1;
System.out.println(d); //98
System.out.println((char) d); //b
}
}
···
···JAVA
public class Demo06 {
public static void main(String[] args) {
//操作比较大的数,注意溢出问题
int money = 10_0000_0000; //100000000
int years = 20;
int total =money*years; //-1474836480
long total2= ((long)years)*money; //20000000000
System.out.println(money);
System.out.println(total);
System.out.println(total2);
//L l 小写l容易看成1,所有建议用L
}
}
···
变量
变量就是可以变化的量
每个变量都必须有声明类型
变量名 变量类型 作用域
//数据类型 变量名 = 值
···
public class Demo07 {
//main 方法
//实例变量,从属于类 对象
String name;//可以不初始化值,默认值为0
//布尔值默认为false
//除了基本类型,其他的默认值都是null
int age;
//类变量 static
static double salary = 2500;
//常量 final 常量名,全大写
static final double PI = 3.14; //修饰符不存在先后顺序,,静态常量
public static void main(String[] args) {
//int a = 1,b=2,c=3;
//实例变量
//局部变量 必须声明和初始化值
int i = 10;
Demo07 demo07 = new Demo07();
System.out.println(demo07.age); //0
System.out.println(demo07.name); //null
System.out.println(salary); //2500.0
System.out.println(PI); //3.14
}
//其他方法
public void add(){
}
}
···
变量的命名规范
所有的变量、方法名 类名 见名知意
类成员变量:首字母小写和驼峰原则 lastName monthSalary
局部变量:首字母小写和驼峰原则
常量:大写字母和下划线 MAX_VALUE
类名:首字母大写和驼峰原则 Man GoodMan
方法名:首字母小写和驼峰原则 run() runRun()
运算符
package operator;
public class Demo01 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println((double) a/b);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 123122141556454L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d); //123122141556595 long
System.out.println(b+c+d); //141 int
System.out.println(c+d); //18 int
System.out.println((double) c+d); // 18.0
}
}
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系运算返回的结果
int a = 10;
int b = 20;
int c = 21;
System.out.println(a<b);
System.out.println(a>b);
System.out.println(a==b);
System.out.println(a!=b);
System.out.println(c%a); //取余,模运算
}
}
package operator;
public class Demo04 {
public static void main(String[] args) {
int a = 3;
int b = a++;
//隐藏了 a=a+1 运算行代码之后a自己加1
System.out.println(a);
int c =++a; // 执行行代码前先给a自增1
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算 2^3 很多算法使用工具类来操作
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
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)); //逻辑与运算,
System.out.println("a||b:"+(a||b)); //逻辑或运算
System.out.println("!(a&&b):"+!(a&&b)); //取反
//短路运算
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则为0
A|B 0011 1101 或 都是0则为0,有1则为1
A^B 0011 0001 异或 相同则为0,不相同则为1
~B 1111 0010 取反,完全相反
2*8=16 2*2*2*2
<< 左移 *2 >>右移 /2
位运算和底层打交道,效率极高
*/
System.out.println(2<<3);
}
}
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 两侧转换位String 进行连接
System.out.println("" + a + b);
System.out.println(a + b + "");
}
}
package operator;
public class Demo08 {
public static void main(String[] args) {
//x ? y :z
//若干x为真,则y ,否则为z
int score= 80;
String type =score<60?"不及格":"及格";
System.out.println(type);
}
}
视频链接地址:
包的本质是文件夹 class 是文件
包的规范,一般利用公司域名道指作为包名 www.baidu.com 包 com.baidu.www
import 导入包路径。包必须放在最上面,import 在它下面
import package1.clasename *通配符,导入包下所有的类
阿里巴巴开发手册
javadoc 命令 使用cmd生产 javadoc -encoding UTF-8 -charset UTF-8 Doc.java
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-deOhfZV4-1657354630112)(D:\01 my zone\002 study area\01MarkDown\捕获.PNG)]
作业 学会使用idea 生成javadoc文档