Java基础复习
基础语法
标识符
public class Demo01 {
public static void main(String[] args) {
//字母、美元符、下划线开头、大小写敏感、可写中文
String Ahello="宏伟";
String hello="宏伟";
String ¥hello="宏伟";
String _hello="宏伟";
//String #hello="宏伟";
//String *hello="宏伟";
}
}
注释
public class HelloWorld {
public static void main(String[] args) {
//输出一个Hello World
System.out.println("Hello World");
String student ="张宏伟";
/*我是多行注释
*/
//JavaDoc:文档注释
/**
* @Description HelloWorld
* @Author 软工张宏伟
*/
}
}
数据类型
基本数据类型
public class Demo01 {
public static void main(String[] args) {
//整数类型byte short int long 一个,两个,四个,八个字节
//浮点类型float double 四个、八个字节
//字符类型char 两个字节
int num1=10;
byte num2=20;
short num3=30;
long num4=30L;//long类型需要在数字后加L
//小数:浮点数
float num5= 50.1F;//原理同long
double num6=3.1415926;
//字符
char name='伟';
//字符串,String不是关键字,而是一个类
String name2="宏伟";
//布尔值
boolean flag=true;
}
}
// public static void main(String[] args) {
// //字母、美元符、下划线开头、大小写敏感、可写中文
// String Ahello="宏伟";
// String hello="宏伟";
// String ¥hello="宏伟";
// String _hello="宏伟";
// //String #hello="宏伟";
// //String *hello="宏伟";
// }
类型拓展
public static void main(String[] args) {
//整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i=10;
int i2=010;
int i3=0x10;//0`9 A`F 16
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
//浮点数拓展
System.out.println("--------------");
float num1=0.1F;
double num2=0.1;
System.out.println(num1==num2);//flase
float num3=1231231321321F;
float num4=num3+1;
System.out.println(num3==num4);//true
//float 有限位数,切离散 接近但不等于存在舍入误差
//对于银行业务 BigDecimal数学工具类
System.out.println("--------------");
//字符拓展
char c1='a';
System.out.println((int)c1);//对字符进行强制转换,字符的本质还是数字 Unicode编码 2字节 0~65536 U0000 UFFFF
char c2='u0000';
System.out.println(c2);
//转义字符
// 制表符
换行等等
System.out.println("Hello
World");
}
引用数据类型
类
接口
数组
类型转换
public static void main(String[] args) {
//byte short chat int long float double
//强制类型转换
int i=128;
byte b=(byte) i;//内存溢出
System.out.println(i);
System.out.println(b);
//自动类型转换
double c=i;
/*
布尔值不能转换
高容量转低容量时,强制转换
转换时存在内存溢出,或者精度问题
*/
//对于较大数字,数字之间可用下划线分割
int money=10_0000_0000;
System.out.println(money);
//较大数字计算前将类型转换好,避免内存溢出
}
变量
static int a=10;//实例变量 从属于对象 默认值为0 0.0 false,除基本类型外,其余默认值都为null
public static void main(String[] args) {
int a=1;//局部变量
int b=2;
String name="宏伟";
char x='X';
double p1=3.14;
Demo01 demo01=new Demo01();
System.out.println(demo01.a);
}
常量
//命名:变量、方法 见名知意,驼峰原则 lastName runRun() 常量要求大写字母下划线
static final double PI=3.1415926;
public static void main(String[] args) {
System.out.println(PI);
}
运算符
public static void main(String[] args) {
//二元运算符 cast 转换
int a=10;
int b=10;
int c=10;
int d=10;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
//短路运算
int c=5;
boolean d=(c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
public static void main(String[] args) {
/*
A= 0011 1100
B= 0000 1101
A&B 都为1变1
A|B
A^B 相同变0
~B 取反
<< *2
>> /2
*/
//三元运算符
//x?y:z
}
幂运算用工具里 Math