前言
本系列记录Java从入门开始的知识点,Java基础阶段中主要包括注释、标识符、关键字;数据类型;类型转换;常量、变量;运算符;包机制、JavaDoc。
文章目录
一、注释、标识符、关键字
1、注释
注释一般分为单行注释、多行注释以及文档注释。
- 单行注释://开头;
- 多行注释:/*开头 */结尾
- 文档注释:/**开头 */结尾
//输出Helloworld 单行注释
System.out.println("Hello,world");
/*多行注释
多行注释
多行注释*/
/**
* @Description HelloWorld
* @Author 饭啊饭
*/
2、标识符
标识符只需要记住一些注意点即可:
- 所有的标识符都应该以
字母、$、或者下划线(_)
开头; - 首字母之后可以时任何字符组合;
不能使用关键字作为变量名或方法名
;- 标识符是
大小写敏感
的; - 可以使用中文命名,但是一般不建议这样使用,也不建议使用拼音。
3、关键字
Java中指定了一些关键字,慢慢后面都会遇到的,这里先直接给出。
二、数据类型
1、八大基本数据类型
- Java是一种
强类型语言
:要求变量的使用要严格符合规定,所有变量都必须先定义才能使用。 - 基本数据类型
// 八大基本数据类型
// 整数
int num1 = 10; //最常用
byte num2 = 20;
short num3 = 500;
long num4 = 40000000L;
// 小数:浮点数
float num5 = 40.5F;
double num6 = 1.25647895;
// 字符类型 String不是关键字 是一个类
char name = '饭';
// String names = "饭啊饭";
// 布尔值
boolean flag1 = true;
boolean flag2 = false;
2、整数拓展
二进制:010101
十进制
八进制:0开头
十六进制:0x开头
int i = 10;
int i2 = 010;
int i3 = 0x10;
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
输出:
3、浮点数拓展
最好完全避免用浮点数进行比较!!!!!!
最好完全避免用浮点数进行比较!!!!!!
最好完全避免用浮点数进行比较!!!!!!
银行业务要使用BigDecimal
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d);
float d1 = 1516548496161f;
float d2 = d1+1;
System.out.println(d1 == d2);
输出:
4、字符拓展
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1);
System.out.println(c2);
System.out.println((int)c2);
//所有字符的本质还是数字
//编码 Unico 97=a 65=A
char c3 = '\u0061';
System.out.println(c3);
输出:
5、转义字符
System.out.println("Hello\tWorld"); //制表符
// \n 换行符
6、对象拓展
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa==sb);
String sc = "hello world";
String sd = "hello world";
System.out.println(sc==sd);
输出:
7、布尔值拓展
boolean flag = true;
if(flag){
System.out.println("对啦");
}
输出:
三、类型转换
Java是强类型语言,所以要进行运算的时候,需要用到类型转换。运算中,不同类型要先转化位同一类型,然后进行运算。
1、强制类型转换:高------>低
int i = 128;
byte b = (byte)i;
System.out.println(b); //byte最大值是127 内存溢出
2、注意点
(1) 不能对布尔值转换;
(2)不能把对象类型转换位不相干的类型;
(3)高容量转为低容量才需要强制类型转换;
(4)高转低的时候要注意内存溢出。
3、自动类型转换:低------>高
4、操作大数字的时候,注意溢出问题
int money = 10_0000_0000;
System.out.println(money);
int years = 20;
int total = money*years;
System.out.println(total);
long total2 = money*years;
System.out.println(total2);
long total3 = money*(long)years;
System.out.println(total3);
输出:
四、变量
Java变量是程序中最基本的存储单元,要素包括变量名、变量类型和作用域。
1、注意点
- 每个变量都有类型,类型可以是基本类型,也可以是引用类型;
- 变量名必须是合法的标识符;
- 变量声明是一条完整的语句,因此,每一个声明都必须以分号结束。
2、变量作用域
- 类变量
public class Demo08 {
//属性:变量
//类变量: static
static double salary = 2500;
//main方法
public static void main(String[] args) {
//类变量
System.out.println(salary);
}
}
- 实例变量
public class Demo08 {
//实例变量:从属于对象的;
//除了基本类型,其余的默认值都是null
String name;
int age;
//main方法
public static void main(String[] args) {
//实例变量
Demo08 demo08 = new Demo08();
System.out.println(demo08.name);
System.out.println(demo08.age);
}
}
- 局部变量
public class Demo08 {
//main方法
public static void main(String[] args) {
//局部变量:必须声明和初始化
int i = 0;
System.out.println(i);
}
}
3、常量
值被设定以后,不能改变。
public class Demo09 {
// 修饰符不存在先后顺序
static final double PI = 3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}
4、变量名命名规则
- 所有变量、方法、类名:
见名知意
; - 类成员变量、局部变量、方法名:首字母小写和驼峰原则;
- 常量:大写字母和下划线;
- 类名:首字母大写和驼峰原则;
五、运算符
1、Java支持的运算符
2、自增、自减
public class Demo03 {
public static void main(String[] args) {
int a = 3;
int b = a++; //先赋值,再+1
System.out.println(a);
System.out.println(b);
int c = ++a; //先+1,再赋值
System.out.println(a);
System.out.println(c);
}
}
3、幂运算
//幂运算
double pow = Math.pow(2,3);
System.out.println(pow);
4、位运算(面试题)
问:2*8 怎么算最快?
System.out.println(2<<3); //位运算中左移为乘2。
5、字符串连接符 +(面试题)
public class Demo05 {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(""+a+b); //1020
System.out.println(a+b+""); //30
}
}
六、包机制
- 为了更好的组织类,Java提供了包机制,用于区别类名的命名空间。
- 包的语法格式为:
一般利用公司域名倒置作为包名
;- 为了能够使用某一个包的成员,需要在Java程序中明确导入该包。
七、Java Doc
package operator;
/**
* @author fananfan
* @version 1.6
* @since 1.8
*/
public class Demo06 {
String name;
/**
* @author fanafan
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;
}
}
生成文档: