Java 基础语法
注释
- 单行注释
//单行注释
- 多行注释
/*多行注释
*
*
*
*
*
* */
- 文档注释
/**
* @JavaDos: 文档注释
*
*
*/
- 有趣的注释
/***
*
* █████▒█ ██ ▄████▄ ██ ▄█▀ ██████╗ ██╗ ██╗ ██████╗
* ▓██ ▒ ██ ▓██▒▒██▀ ▀█ ██▄█▒ ██╔══██╗██║ ██║██╔════╝
* ▒████ ░▓██ ▒██░▒▓█ ▄ ▓███▄░ ██████╔╝██║ ██║██║ ███╗
* ░▓█▒ ░▓▓█ ░██░▒▓▓▄ ▄██▒▓██ █▄ ██╔══██╗██║ ██║██║ ██║
* ░▒█░ ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄ ██████╔╝╚██████╔╝╚██████╔╝
* ▒ ░ ░▒▓▒ ▒ ▒ ░ ░▒ ▒ ░▒ ▒▒ ▓▒ ╚═════╝ ╚═════╝ ╚═════╝
* ░ ░░▒░ ░ ░ ░ ▒ ░ ░▒ ▒░
* ░ ░ ░░░ ░ ░ ░ ░ ░░ ░
* ░ ░ ░ ░ ░
*/
/***
* .::::.
* .::::::::.
* ::::::::::: FUCK YOU
* ..:::::::::::'
* '::::::::::::'
* .::::::::::
* '::::::::::::::..
* ..::::::::::::.
* ``::::::::::::::::
* ::::``:::::::::' .:::.
* ::::' ':::::' .::::::::.
* .::::' :::: .:::::::'::::.
* .:::' ::::: .:::::::::' ':::::.
* .::' :::::.:::::::::' ':::::.
* .::' ::::::::::::::' ``::::.
* ...::: ::::::::::::' ``::.
* ````':. ':::::::::' ::::..
* '.:::::' ':'````..
*/
关键字
数据类型
- 整型
byte num01 = 12;
short num02 = 12222;
int num03 = 1222222222;
long num04 = 1222222222222222222L; //后面加L
- 浮点型
float num05 = 2.5F; //后面加F
double num06 = 22.5;
- 字符
char name = '1'; //只能一个字
4.布尔值(是非)
boolean flag = true;
boolean flag1 = false;
数据类型扩展
-
进制
二进制:0b
八进制:0
十进制
十六进制:0x
int a1 = 0b10;
int a2 = 89;
int a3 = 010;
int a4 = 0xf;
- float、double 不可用于比较 而 BigDecimal 可以
float b1 = 0.1f ;
double b2 = 1.0/10;
System.out.println(b1);
System.out.println(b2);
System.out.println(b1==b2);
float c1 = 13515461111111111111f;
float c2 = 13515461111111111111f+1;
System.out.println(c1==c2);
- 强制转换
char d1 = 'a' ;
char d2 = '中';
char d3 = '\u0061';
System.out.println(d1);
System.out.println( (int)d1 );
System.out.println(d2);
System.out.println( (int)d2 );
System.out.println(d3);
类型转换
低级–>高级:
byte,short,char --> int --> long --> float --> double
int a = 128;
byte b=(byte)a; //低级-->高级:强制转换(但需要考虑取值范围)
System.out.println(a);
System.out.println(b);
double c=a;
System.out.println(c);
System.out.println("============================");
System.out.println((int)23.7);
System.out.println((int)-45.89f);
- 不能对布尔值进行转换
- 不能把对象类型转换为不相干的类型
- 在把高容量转换到低容量的时候,强制转换
- 转换的时候可能存在溢出,或者精度问题!
- 操作比较大的数时,注意溢出问题
int d=10_0000_0000;
int e=20;
int f=d*e;
long g=d*e;
long h=d*((long)e);
System.out.println(f); //溢出
System.out.println(g); //溢出
System.out.println(h); //转换成功
变量、作用域
public class day03 {
/*变量、作用域*/
//类变量:static
static double money = 3000;
//属性:变量
//实例变量:从属于对象:如果不自行初始化,这个类型的默认值: 0 0.0
//布尔值:false
//除了基本类型,其余默认值都是空的:null
String name;
int age;
//main方法
public static void main(String[] args) {
//局部变量:必须声明和初始值
int i = 10 ;
System.out.println(i);
day03 day = new day03();
System.out.println(day.age);
System.out.println(day.name);
System.out.println(money);
}
//其他方法
public void add(){
}
}
常量final
public class day04 {
/* 常量:final */
//修饰符,不存在先后顺序
static final double PI = 3.14;
final static double PI1 = 3.14;
public static void main(String[] args) {
System.out.println(PI);
System.out.println(PI1);
}
}