注释
- 单行 //
- 多行 /* */
- 文本 /** */
public class hello {
public static void main(String[] args) {
//单行注释
//输出一个Hello World!
System.out.println("Hello world!");
/*多行注释*/
/*
我是多行注释
我是多行注释
我是多行注释(Ctrl+D是复制上一行)
*/
//JavaDoc:文档注释 /** */
/**
* @Description Hello World
* @Author 狂神说Java_刘婷
**/
}
}
关键字
![image-20210122173209173](https://gitee.com/guyebai/cloudimage/raw/master/img/image-20210122173209173.png)
合法标识符:字母、美元符$、下划线_
public class Demo01 {
public static void main(String[] args) {
// 大小写敏感
String hello = "liuting";
String Hello = "liuting";
//合法
String Ahello = "liuting";
String hhello = "liuting";
String $hello = "liuting";
String _hello = "liuting";
//不合法
// String 1hello = "liuting";
// String #hello = "liuting";
// String *hello = "liuting";
}
}
数据类型
基本类型:数值(整数、浮点数、字符)、布尔。
引用类型:类、接口、数组。
public class Demo02 {
public static void main(String[] args) {
//八大基本数据类型
//整数
int num1 = 10000000; //不能超过21亿、最常用
byte num2 = 20; //不能超过127
short num3 = 30; //30亿
long num4 = 30L; //加L
//小数
float num5 = 50.1F;
double num6 = 3.1216864565757;
//字符
char name = 'A';
String namea = "liuting"; //String不是一个关键字。是一个类
//布尔
boolean flag1 = true;
boolean flag2 = false;
}
}
public class Demo03 {
public static void main(String[] args) {
//整数拓展
// 进制 二进制 0b 十进制 八进制 0 十六进制 0x
int i1 = 10;
int i2 = 010; //八进制 0 0~7(0-7) 010(8)
int i3 = 0x10; //十六进制 0x 0~9(0-9) A~F(10-15) 0x10(16)
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println("===============================");
//=========================================//
//浮点数怎么扩展? 银行业务怎么表示?钱
//=========================================//
//float 有限、离散 舍入误差 大约 接近但是不等于
//double
//最好不要完全避免使用浮点数进行比较
//最好不要完全避免使用浮点数进行比较
//最好不要完全避免使用浮点数进行比较
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d); //false
float d1 = 23243253545f;
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); //97
System.out.println(c2);
System.out.println((int)c2); //20013
//所有的字符本质还是数字,(编码 Unicode 2字节 0-65535) 表:(a = 97 A = 65) Excel 2^16 = 65536
//U0000 UFFFF
char c3 = '\u0061';
System.out.println(c3); //a
//转义字符
// \t制表符
// \n换行
// 。。。
System.out.println("Hello\tWorld");
//对象 从内存分析
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){} //老手
// 代码要精简易读!
}
}
int i1 = 10;
int i2 = 010; //八进制 0
int i3 = 0x10; //十六进制 0x 0~9 A~F 16
//八进制
/**
01 1
02 2
.
.
07 7
没有 08、09
010 8
011 9
012 10
*/
//十六进制
/**
0x1 1
0x2 2
0x3 3
.
.
0x9 9
0xA 10
.
.
0xF 15
0x10 16
0x11 17
.
.
*/
![image-20210123103803820](https://gitee.com/guyebai/cloudimage/raw/master/img/image-20210123103803820.png)
字节
![image-20210123103938540](https://gitee.com/guyebai/cloudimage/raw/master/img/image-20210123103938540.png)
类型转换
![image-20210123120007821](https://gitee.com/guyebai/cloudimage/raw/master/img/image-20210123120007821.png)
强制转换、自动转换
public class Demo04 {
public static void main(String[] args) {
//强制转换(类型)变量名 高~低
//自动转换 低~高
int i = 128;
byte b = (byte) i; //最高127 内存溢出
double d = i;
System.out.println(i); //128
System.out.println(b); //-128
System.out.println(d); //128.0
/*
注意点:
1、不能对布尔值进行转换
2、不能把对象类型准换为不相干的类型、
3、在高容量转换为低容量的时候,强制转换
4、转换的时候存在内存溢出,或者精度问题!
*/
System.out.println("========================");
System.out.println((int)23.6); //23
System.out.println((int)-23.6f); //-23
System.out.println("========================");
char c1 = 'a';
int c2 = c1 + 1;
System.out.println(c2); //98
System.out.println((char)c2); //b
}
}
public class Demo05 {
public static void main(String[] args) {
//操作数比较大的数的时候
//JDK7新特性,数字之间可以用下划线
int money = 10_0000_0000;
//尽量用L
long money11 = 10_0000_0000L;
System.out.println(money);
int year = 20;
int total = money * year; //计算的时候溢出了
System.out.println(total); //-1474836480
long total2 = money * year; //默认是int,转换之前已经存在问题了
long total3 = money * ((long)year); //2000000000 20亿
}
}
变量
作用域:
类变量:static
实例变量、局部变量
![image-20210123201813360](https://gitee.com/guyebai/cloudimage/raw/master/img/image-20210123201813360.png)
public class Demo06 {
//属性:变量
//实例变量:从属于对象Demo06;如果不进行初始化,则返回这个类型的默认值
//布尔值:默认是false
//除了基本类型,其余都是null
String name;
int age;
//类变量 static
static double salary = 2500;
//main方法
public static void main(String[] args) {
//局部变量:必须声明和初始化值
int i=10;
System.out.println(i);
//变量类型 变量名字 = new Demo06
Demo06 demo06 = new Demo06();
System.out.println(demo06.age);
System.out.println(demo06.name);
//类变量 static
System.out.println(salary);
}
//其他方法
public void add(){
//System.out.println(i); 错误
}
}
public class Demo07 {
//修饰符,不存在先后顺序
static final double pi = 3.14;
final static double PI = 3.14; //常量一般用大写+下划线
public static void main(String[] args) {
System.out.println(pi);
System.out.println(PI);
}
}
运算符
![image-20210123204254997](https://gitee.com/guyebai/cloudimage/raw/master/img/image-20210123204254997.png)
package operator;
public class Demo01 {
public static void main(String[] args) {
long a = 123123123123L;
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(d);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
//关系运算符返回的结果:正确、错误 布尔值
int a = 1;
int b = 2;
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a == b);
System.out.println(a != b);
//取余,模运算
int c = 11;
System.out.println(c % b);
}
}
package operator;
public class Demo03 {
public static void main(String[] args) {
// ++ -- 自增 自减 一元运算符
int a = 3;
int b = a++; //a = a + 1 执行完这行代码后,先给b赋值,再自增
int c = ++a; //a = a + 1 执行完这行代码前,先自增,再给b赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算--工具类实现
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
package operator;
//逻辑运算符
public class Demo04 {
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); //只运行 c < 4
System.out.println(d);
System.out.println(c);
}
}
package operator;
import javax.crypto.spec.PSource;
public class Demo06 {
/*
A = 0011 1100
B = 0000 1101
-------------------------------
A & B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001 //不同的为1
~B = 1111 0010 //取反
2 * 8 = 16 2*2*2*2
效率高!!!
效率高!!!
效率高!!!
<< //乘以2
>> //除以2
*/
public static void main(String[] args) {
System.out.println(2<<3); //效率高
//字符串连接符 + ,String
System.out.println("============================");
int a=10;
int b=20;
System.out.println(""+ a + b); //1020 准换为了字符串
System.out.println(a + b + ""); //3
//三元运算符
System.out.println("============================");
int score = 50;
String type1 = score < 60 ? "不及格" : "及格";
String type2 = score < 50 ? "不及格" : "及格";
System.out.println(type1);
System.out.println(type2);
}
}
包机制
1、Ctrl + 鼠标左键 (进入包)
2、一般用公司域名倒置作为包名
3、Alt + 回车,(导入包) import
4、导入包下所有的类 import com.liu.base.*;
JavaDoc
1、
![image-20210125103142527](https://gitee.com/guyebai/cloudimage/raw/master/img/image-20210125103142527.png)
2、命令行生成javadoc
![image-20210125103929161](https://gitee.com/guyebai/cloudimage/raw/master/img/image-20210125103929161.png)
package com.liu.base;
/**
* @author ting
* @version 1.0
* @since 1.8
*/
public class Doc {
String name;
//自动生成
/**
*
* @param name
* @return
*/
/**
*
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;
}
//学会用IDEA生成javadoc
}
3、学会用IDEA生成javadoc
首先选择需要生成文档的代码
![image-20210125105023552](https://gitee.com/guyebai/cloudimage/raw/master/img/image-20210125105023552.png)
然后
![image-20210125105039123](https://gitee.com/guyebai/cloudimage/raw/master/img/image-20210125105039123.png)
![image-20210125105103819](https://gitee.com/guyebai/cloudimage/raw/master/img/image-20210125105103819.png)
-encoding utf-8 -charset utf-8