Day01-Java基础
# Java基础:
## 1. 注释、标识符、关键字
### 注释:
**(书写注释是一个非常好的习惯 BAT)**
**(平时写代码一定要注意规范)**
- 单行注释
- 多行注释
- 文档注释
```java
public class HelloWorld {
public static void main(String[] args) {
//单行注释 :只能注释一行文字 //
//输出一个Hello,World!
System.out.println("Hello,World!");
//有趣的代码注释
/***
* ii. ;9ABH,
* SA391, .r9GG35&G
* i3X31i;:,rB1
* iMs,:,i5895, .5G91:,:;:s1:8A
* 33::::,,;5G5, ,58Si,,:::,sHX;iH1
* Sr.,:;rs13BBX35hh11511h5Shhh5S3GAXS:.,,::,,1AG3i,GG
* .G51S511sr;;iiiishS8G89Shsrrsh59S;.,,,,,..5A85Si,h8
* :SB9s:,............................,,,.,,,SASh53h,1G.
* .r18S;..,,,,,,,,,,,,,,,,,,,,,,,,,,,,,....,,.1H315199,rX,
* ;S89s,..,,,,,,,,,,,,,,,,,,,,,,,....,,.......,,,;r1ShS8,;Xi
* i55s:.........,,,,,,,,,,,,,,,,.,,,......,.....,,....r9&5.:X1
* 59;.....,. .,,,,,,,,,,,... .............,..:1;.:&s
* s8,..;53S5S3s. .,,,,,,,.,.. i15S5h1:.........,,,..,,:99
* 93.:39s:rSGB@A; ..,,,,..... .SG3hhh9G&BGi..,,,,,,,,,,,,.,83
* G5.G8 9#@@@@@X. .,,,,,,..... iA9,.S&B###@@Mr...,,,,,,,,..,.;Xh
* Gs.X8 S@@@@@@@B:..,,,,,,,,,,. rA1 ,A@@@@@@@@@H:........,,,,,,.iX:
* ;9. ,8A#@@@@@@#5,.,,,,,,,,,... 9A. 8@@@@@@@@@@M; ....,,,,,,,,S8
* X3 iS8XAHH8s.,,,,,,,,,,...,..58hH@@@@@@@@@Hs ...,,,,,,,:Gs
* r8, ,,,...,,,,,,,,,,..... ,h8XABMMHX3r. .,,,,,,,.rX:
* :9, . .:,..,:;;;::,.,,,,,.. .,,. ..,,,,,,.59
* .Si ,:.i8HBMMMMMB&5,.... . .,,,,,.sMr
* SS :: h@@@@@@@@@@#; . ... . ..,,,,iM5
* 91 . ;:.,1&@@@@@@MXs. . .,,:,:&S
* hS .... .:;,,,i3MMS1;..,..... . . ... ..,:,.99
* ,8; ..... .,:,..,8Ms:;,,,... .,::.83
* s&: .... .sS553B@@HX3s;,. .,;13h. .:::&1
* SXr . ...;s3G99XA&X88Shss11155hi. ,;:h&,
* iH8: . .. ,;iiii;,::,,,,,. .;irHA
* ,8X5; . ....... ,;iihS8Gi
* 1831, .,;irrrrrs&@
* ;5A8r. .:;iiiiirrss1H
* :X@H3s....... .,:;iii;iiiiirsrh
* r#h:;,...,,.. .,,:;;;;;:::,... .:;;;;;;iiiirrss1
* ,M8 ..,....,.....,,::::::,,... . .,;;;iiiiiirss11h
* 8B;.,,,,,,,.,..... . .. .:;;;;iirrsss111h
* i@5,:::,,,,,,,,.... . . .:::;;;;;irrrss111111
* 9Bi,:,,,,...... ..r91;;;;;iirrsss1ss1111
*/
//多行注释:可以注释一段文字 /* 注释 */
/*
我是多行注释
我是多行注释
我是多行注释
我是多行注释
*/
//JavaDoc:文档注释
/**
* @Description HelloWorld
* @Author 邵杰
*/
}
}
```
### 标识符:
Java所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符。

### 关键字:

## 2. 数据类型
- 强类型语言(安全性高,速度慢。)
要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用
- 弱类型语言 (安全性低,速度快。)
要求变量的使用不需要严格符合规定,所有变量都无须先定义后才能使用
- 基本类型(primitive type)
- 引用类型(reference type)

- 字节:

``` java
public class Demo02 {
public static void main(String[] args) {
//String a = 10;
String a = "hello"; //String是字符串
int num = 10;
System.out.println(a);
System.out.println(num);
}
}
```
### 八大基础类型:(int、byte、short、long、float、double、char、boolean)
```java
public class Demo03 {
public static void main(String[] args) {
//八大基本数据类型
//整数
//Byte //command + 鼠标左键 进入关键词.java
int num1 = 10; //int最常用
byte num2 = 20; //byte:-128-127
short num3 = 30;
long num4 = 30L;//Long类型要在数字后面加个L
//小数:浮点数
float num5 = 50.1F;
double num6 = 3.141592653589793238462643;
//字符
char name = 'A'; //字符只有一个
//字符串,String不是关键词, 是一个类
//String namea = "邵杰";
//布尔值:是非
boolean flag = true;
//boolean flag = false;
}
}
```
数据类型拓展:(面试题讲解)
```java
public class Demo04 {
public static void main(String[] args) {
//整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 010; //八进制0
int i3 = 0x10; //十六进制0x 0-9 A-F 16
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);//false
System.out.println(f);
System.out.println(d);
float d1 = 23123123131231213123f;
float d2 = d1 + 1;
System.out.println(d1 == d2);//true
//======================================================================================
//字符拓展?
//======================================================================================
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);//强制转换
//所有的字符本质还是数字
//编码 Unicode 表: (97 = 0 65 =A) 2字节 0 - 65536 Excel 2^16=65536
//U0000 UFFFF
char c3 = '\u0061';
System.out.println(c3);//a
//转义字符
// \t 制表符
// \n 换行
//......
System.out.println("Hello\nWorld");
System.out.println("======================================================================================");
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa == sb);//false
String sc = "hello world";
String sd = "hello world";
System.out.println(sc == sd);//true
//对象 从内存分析
//======================================================================================
//布尔值拓展?
//======================================================================================
boolean flag = true;
if (flag == true) {
} //新手
if (flag) {
} //老手
//Less is More! 代码要精简易读
}
}
```
## 3. 类型转换

```java
public class Demo05 {
public static void main(String[] args) {
int i = 128;
byte b = (byte) i;//内存溢出
double b1 = i;
//强制转换 (类型)变量名 高-->低
//自动转换 低-->高
System.out.println(i);
System.out.println(b);
System.out.println(b1);
/*
注意点:
1.不能对布尔值进行转换
2.不能把对象类型转换为不相关的类型
3.在把高容量的转换到低容量的时候,强制转换
4.转换的时候可能存在内存溢出,或者精度问题!
*/
System.out.println("====================================");
System.out.println((int)23.7); //23
System.out.println((int)-45.89f); //-45
System.out.println("====================================");
char c ='a';
int d = c + 1;
System.out.println(d);
System.out.println((char) d);
}
}
```
```java
public class Demo06 {
public static void main(String[] args) {
//操作比较大的数,注意溢出问题
//JDk7的新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
System.out.println(money);
int years = 20;
int total =money * years;//-1474836480 , 计算的时候溢出了
System.out.println(total);
long total2 =money * years;//-1474836480 , 默认是int ,转换之前已经存在问题了?
System.out.println(total2);
long total3 = money * ((long)years);//现将years转换成long
System.out.println(total3);
}
}
```
## 4. 变量、常量
### 变量:


#### 变量的命名规范:

- 类变量
- 实例变量
- 局部变量
- ```java
public class Demo08 {
//3.类变量 static
static double salary = 2500;
//属性:变量
//2.实例变量:从属于对象; 如果不自行初始化,这个类型的默认值: 0 0.0 u0000
//布尔值: 默认是false
//除了基本类型,其余的默认值都是null;
String name;
int age;
//main方法
public static void main(String[] args) {
//1.局部变量;必须声明和初始化值
int i=10;
System.out.println(i);
//变量类型 变量名字 = new Demo08();
Demo08 demo08 = new Demo08();
System.out.println(demo08.age);
System.out.println(demo08.name);
//3.类变量 static
System.out.println(salary);
}
//其他方法
public void add(){
System.out.println();
}
}
```
### 常量:
(final static )

```java
public class Demo09 {
//修饰符,不存在先后顺序
static final double PI = 3.14;
//final static double PI = 3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}
```
## 5. 运算符

**优先级 ()**
### 1.基本运算符:
```java
package operator;
public class Demo01 {
public static void main(String[] args) {
//二元运算度符
//command + 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(a/b);//0 因为a,b是int,0.5取整变成0;
System.out.println(a/(double)b);
}
}
```
```java
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 2131232131313131312L;
int b =123;
short c =10;
byte d =8;
System.out.println(a+b+c+d);//long
System.out.println(b+c+d);//int
System.out.println(c+d);//int
System.out.println((double) c+d);//int //cast == 转换
//所有运算中,当有long时,输出结果类型为long,其他没有long类型时,所有非int类型转为int类型
}
}
```
```java
package operator;public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果: 正确, 错误 布尔值
//if
int a = 10;
int b = 20;
int c = 21;
//取余,模运算
System.out.println(c%a); //c/a 21 / 10 =2 ...1
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
```
}
### 2.自增自减运算符:(a++和++a)
```java
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增,自减 一元运算符
int a = 3;
int b = a++; //执行完这行代码后,先给b赋值,再给a自增
//a++ a=a+1
System.out.println(a);
//a++ a=a+1
int c = ++a; //执行完这行代码前,先给a自增,再给c赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算 2^3 2*2*2 = 8 很多运算,我们会使用一些工具类来操作
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
```
### 3.逻辑运算符:(与(and) 或(or) 非(not))
```java
package operator;
//逻辑运算符
public class Demo05 {
public static void main(String[] args) {
//与(and) 或(or) 非(not)
boolean a = true;
boolean b = false;
//一假则假,全真为真
System.out.println("a && b:" + (a && b));//逻辑与运算: 两个变量都为真, 结果才为true
System.out.println("a || b:" + (a || b));//逻辑与运算: 两个变量有一个为真,则结果才为true
System.out.println("! (a && b):" + ! (a && b));//逻辑与运算: 如果是真, 则变为假, 如果是假, 则变为真
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);//false
System.out.println(c);//5
//因为已知 c<4=false, 则d为false。但c仍然=5,所有c++并没有进行运算;
}
}
```
### 4.位运算符:
```java
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
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(2<<3);
}
}
```
### 扩展赋值运算符:
```java
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类型,就会把其他转换成String类再进行连接)
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+"");//运算从左到右
}
}
```
### 5.三元运算符 条件运算符:
```java
package operator;
//三元运算符 条件运算符
public class Demo08 {
public static void main(String[] args) {
// 格式: x ? y : z
//如果x==true, 则结果为y,否则结果为z
int score = 80;
int score1 = 50;
String type = score < 60?"不及格":"及格";//必须掌握
System.out.println(type);
String type1 = score1 < 60?"不及格":"及格";
//if
System.out.println(type1);
}
}
```
## 6. 包机制

包的本质就是文件夹!
```java
//导入这个包下所有的类!
import com.sj.base.*;//* 通配符
```
## 7.JavaDoc

```java
package com.sj.base;
/**
* @author Shaojie
* @version 1.0
* @since 1.8
*/
public class Doc {
String name; //属性
/**
* @author Shaojie
* @param name
* @return
* @throws Exception //抛出异常
*/
public String test(String name) throws Exception{//方法
return name; //
} //
//狂神是通过命令行 javadoc 参数 Java文件
/**
*先右键文件(doc.java),选择open in => Terminal 到终端 (open in =>finder ,就是查找文件所在文件夹)
* 输入:javadoc -encoding UTF-8 -charset UTF-8 Doc.java
*/
//作业: 学会查找使用IDEA生产JavaDoc文档! 面向百度编程!
//基础部分的一切知识,后面几乎每天都会用
}
```

浙公网安备 33010602011771号