Java基础
Java基础语法
注释、标识符、关键字
1、注释
单行注释
// 用于单行注释
多行注释
/*
用于多行注释
用于多行注释
用于多行注释
*/
文档注释(JavaDoc)
/**
*@Description Helloworld
*@Author 啊啵
*/
- 书写注释是一个非常好的习惯
- 平时写代码一定要规范
2.标识符
- Java所有组成部分都需要名字。类名、变量名和方法名都被称为标识符
- 不能用关键字作为变量名或方法名
- 标识符大小敏感
- 不建议用拼音或汉字,尽可能使用单词
- 标识符都应以字母、美元符号$、下划线_开头,不能使用数字开头
数据类型
Java要求变量的使用严格符合规定,要求变量都必须先定义再使用
基本类型
数值类型
整数类型
1字节 = 8位
byte:1字节
short:2字节
int:4字节
long:8字节 使用long时会在所赋的值后加L
long number = 10000L;
浮点数类型
float:4字节**使用float时会在所赋的值后加F
float number = 1.4F;
double:8字节
字符类型
char:2字节
字符串String不是关键字,是类
布尔类型(boolean)
boolean:1位
拓展
public class Demo01 {
public static void main(String[] args) {
/*整数拓展: 进制
二进制 0b
十进制
八进制 0
十六进制 0x
*/
int i = 10; //十进制
int i2 = 010;//八进制0
int i3 = 0x10;//十六进制0x
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("========================================");
//浮点数拓展 银行业务表示 钱
//最好不要完全使用浮点数进行比较 使用BigDecimal 数学工具类
//float 有限 舍入误差
//double
float f = 0.1f; //0.1
double d = 1.0/10; //0.1
System.out.println(f==d);
float f1 = 111111111111111111111111111111f;
float f2 = f1 + 1;
System.out.println(f1==f2);
//字符拓展
System.out.println("===============================================");
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1);// 强制类型转换
System.out.println(c2);
System.out.println((int)c2);// 强制类型转换
// 所有的字符本质还是数字 ASCII Unicode
// 转义字符
//\t制表符 \n换行
System.out.println("Hello\tworld");
// 布尔值拓展
boolean flag = true;
//if (flag){} 老手
// if(flag == true){} 新手
//代码要精简易读
}
}
运行结果

引用类型
类 接口 数组
基本类型之外的
类型转换
强制转换
public class TypeReverse {
public static void main(String[] args) {
int i = 128;
byte b = (byte) i;//内存溢出 byte -128~127
// 强制转换 高到低 (类型)变量名
// 自动转换 低到高
System.out.println(i);
System.out.println(b);
/*
注意点:
1.不能把布尔值进行转换
2.不能把对象类型转换为不相干的类型
3. 在把高容量的类型转化为低容量的类型要强制转换
4. 转换时可能会出现内存溢出或者精度问题
*/
System.out.println("========================");
System.out.println((int) 23.7);
System.out.println((int) -45.12f);
System.out.println("=========================");
char c = 'a';
int d = c + 1;
System.out.println(d);
System.out.println((char)d);
}
}
运行结果

溢出问题
public class Demo02 {
public static void main(String[] args) {
//操作比较大的数,注意溢出问题
//JDK7 新特性 数字之间可以用下划线分割
int money = 1_000_000_000;// 1,000,000,000
int year = 20;
int total = money * year;// 计算的时候溢出了
System.out.println(total);
long total2 = money * year;// 默认是int,转换之前已经存在问题了
System.out.println(total2);
long total3 = (long)money * year;
System.out.println(total3);
}
}
运行结果

变量、常量、作用域
变量
不建议一行里面定义多个值
public class Demo03 {
public static void main(String[] args) {
//int a,b,c int a = 1,b=....不建议 可读性差
int a = 1;
String name = "Link";
char x = 'x';
double pi = 3.14;
}
}
变量作用域
- 类变量
- 实例变量
- 局部变量
public class Demo04 {
// 类变量 static
static double salary = 2500;
//实例变量:从属于对象;如果不进行初始化,就会赋予其该变量默认值
//0 false null
String name;
int age;
//main方法
public static void main(String[] args) {
// 局部变量:必须声明和初始化值
int i = 1;
System.out.println(i);
Demo04 d = new Demo04();
System.out.println(d.name);
System.out.println(d.age);
System.out.println(d.salary);
}
}
常量
public class Demo05 {
static final double PI = 3.14;
// final static double PI = 3.14也行
public static void main(String[] args) {
System.out.println(PI);
}
}
命名规范
- 见名知意 使用单词
- 类成员变量 局部变量 驼峰命名法 lastName
- 常量 大写字母加下划线 MAX_VALUE
- 类名 首字母大写加驼峰命名
运算符
基本运算符
- 算数运算符
package operator;
public class Demo01 {
public static void main(String[] args) {
// 二元运算符
// ctrl + d 复制当前行
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); // 强制转换
System.out.println(a%b);
//整型
System.out.println(a+b+c+d);// long 有long则为long
System.out.println(b+c+d); //int 无long都为int
System.out.println(c+d); //int
}
}
- 赋值运算符 =
- 关系运算符
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果:true false 布尔值
int a =10;
int b = 20;
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a == b);
System.out.println(a != b);// 以及 >= <=
}
}
自增自减运算符
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增、自减 一元运算符
int a = 3;
int b = a++;// b = a = 3、a + 1 = 4
//先赋a值 再自增
int c = ++a;// a + 1 = 5、 c = a = 5
//先自增 再赋a值
System.out.println(a);// 5
System.out.println(b);// 3
System.out.println(c);// 5
初识Math类
//幂运算 使用工具类去写
double pow = Math.pow(2,3);//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)); //逻辑与运算:两个变量都为真,结果为真. false
System.out.println("a || b: "+(a||b)); //逻辑或运算:两个变量都为假,结果为假. true
System.out.println("!(a && b): "+!(a&&b)); //如果为真,则变为假;如果为假,则变为真. true
//短路运算
int c = 5;
boolean d = (c < 4) && (c++<4); // 前面为false 直接短路 后面的表达式不执行
System.out.println(d);// false
System.out.println(c);// c++不执行 c仍=5
}
}
位运算符
package operator;
public class Demo06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
A&B 0000 1100 按位与 同为1才为1
A|B 0011 1101 按位或 只要不是00则为1
A^B 0011 0001 按位异或 相异为1
~B 1111 0010 按位取反 与原相反
*/
/* 2 * 8 最快的方法
<< 左移 >> 右移 箭头指哪就是哪移
0000 0010 2
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(2 << 3);
}
}
条件运算符
package operator;
// 三元运算符
public class Demo07 {
public static void main(String[] args) {
// x ? y : z
// 如果x = true, 则结果为y,否则为z
int score = 88;
String type = score < 60 ? "不及格" : "及格";
System.out.println(type);
}
}
扩展赋值运算符
package operator;
public class Demo08 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a += b; //a = a+b = 30
a -= b; //a = a-b = 10
System.out.println(a);
// 字符串连接符 +
System.out.println(a+b);
System.out.println("" + a+b); //字符串在前面 后面的会拼接
System.out.println(a+b+""); // 字符串在后面 前面会运算
System.out.println("价格为" + (a+b) + "元"); //可以用括号 避免字符串的拼接
}
}
包机制(package)
- 一般利用公司域名倒置作为包名 www.wiki.com -> com.wiki.www
- 尽量不要把包中的文件定义相同命名
import com.abc.base.* // 导入这个报下所有的类
JavaDoc
用于写API文档
package base;
/**
* @author Arb 作者名
* @version 1.0 版本号
* @since 1.8 最早使用的jdk版本
*/
public class Doc {
String name;
/**
*
* @param name 参数名
* @return 返回值情况
* @throws Exception 异常抛出情况
*/
public String test(String name) throws Exception{
return name;
}
}