注释
- 注释并不会被执行
- 写注释是一个非常好的习惯
- 平时写代码一定要注意规范
单行注释: // 注释内容
多行注释: /*
注释内容
*/
JavaDoc 文档注释:/**
* @Description HelloWorld
*/
标识符
- 关键字
abstract | assert | boolean | break | byte |
---|---|---|---|---|
case | catch | char | class | const |
continue | default | do | double | else |
enum | extends | final | finally | float |
for | goto | if | implements | import |
instanceof | int | interface | long | native |
new | package | private | protected | public |
return | strictfp | short | static | super |
switch | synchronized | this | throw | throws |
transient | try | void | volatile | while |
-
Java所有的组成部分都需要名字。类名,变量名以及方法名都被称为标识符。
-
所有标识符只能以 A-Z,a-z,,_,数字的任何字符组合。
String Ahello = "YWB"; String hello = "YWB"; String $Ahello = "YWB"; String _Ahello = "YWB";
-
注意:大小写敏感;最好不要中文命名。
数据类型
-
Java属于强语类型语言
要求变量的使用要求严格符合规定,所有变量必须先定义才能使用
-
Java的数据类型分为两大类
- 基本类型:
- 数值类型:byte,short,int,long,float等
- boolean类型:占一位,其值只有true和false
//整数 int num1 = 10; byte num2 = 20; short num3 = 30; long num4 = 30L;//long类型要在数字后面加L //小数:浮点数 float num5 = 50.1F;//float类型要在数字后面加F double num6 = 3.141592653589793238462643; //字符 char name = 'A'; //字符串,String不是关键字,是类 String name01 = "YWB"; //布尔值:是与非 boolean fLag = true; //==================================================== int i = 10; int i2 = 010; //八进制 0 int i3 = 0x10; //十六进制 0x 0~9 A~F 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 float d1 = 23131312322313f; float d2 = d1 + 1; System.out.println(d1==d2); //true System.out.println("=============================================="); //==================================================== //字符拓展 //==================================================== char c1 = 'a'; char c2 = '中'; System.out.println(c1); // a System.out.println((int)c1); //强制转换 97 System.out.println(c2); // 中 System.out.println((int)c2); //强制转换 20013 //所有字符本质还是数字 //编码 Unicode 2字节 早年范围 0-65535 ;Excel表格最长只有 2的16次方 = 65536 char c3 = '\u0061'; System.out.println(c3); // a //转义字符 // \t 制表符 // \n 换行 System.out.println("Hello\tWorld"); 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! 代码要精简易读
-
引用类型
- 类
- 接口
- 数组
- 基本类型:
-
什么是字节
- 位(bit):最小存储单位。
- 字节(byte):数据处理的基本单位,常用B表示。
- 1B = 8bit
- 字符:计算机中使用的字母、数字、字和符号。
类型转换
低----------------------------------------------------->高
byte,short,char --> int --> long --> double
- 运算中,不同类型要先转化为同一类型,然后再进行运算。
int i = 128;
byte b = (byte)i; //内存溢出
double d = i;
//强制转换 (类型)变量名 高类型 --> 低类型
//自动转换 低类型 --> 高类型
System.out.println(i); //128
System.out.println(b); //-128 超出byte的表示范围,内存溢出
System.out.println(d); //128.0
/*
1、不能对boolean类型进行转换
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 e = c + 1;
System.out.println(e); // 98
System.out.println((char)e); // b
System.out.println("=====================================");
//操作比较大的数的时候,注意溢出问题
//JDK新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
int years = 20;
int total = money * years; //计算的时候已经溢出了
long total2 = money * years; //默认是int,转换之前已经存在问题了
long total3 = money * ((long)years); //先把一个数转化为long
System.out.println(total); //-1474836480
System.out.println(total2); //-1474836480
System.out.println(total3); //20000000000
变量
- 可以变化的量
- Java的每个变量都必须声明其类型
- Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域
type varName [=value] [{,varName[=value]}];
//数据类型 变量名 = 值; 可使用逗号隔开来声明多个同类型变量(不推荐)。
变量作用域
- 类变量
- 实例变量
- 局部变量
public class Demo04 {
//属性:变量
//类变量 static
static double salary = 2500;
//实例变量:从属于对象;如果不进行初始化,则变量为该类型的默认值
//整型默认值为0,浮点型默认为0.0,布尔值默认为false
//除了基本数据类型,其余的默认值都为null
String name;
int age;
//main方法
public static void main(String[] args) {
//局部变量:必须声明和初始化值,只作用于该方法内
int i = 10;
System.out.println(i);
//实例变量:变量类型 变量名字 = new Demo04();
Demo04 demo04 = new Demo04();
System.out.println(demo04.age); // 0 age未初始化,为默认值
//类变量 static
System.out.println(salary);
}
//其他方法
public void add(){
}
}
常量
- 初始化后不能再改变,它的值被设定后,在程序运行过程中不允许被改变
- 常量一般使用大写字符
//修饰符不存在前后顺序 static和final可以调换顺序
static final double PI = 3.14;
public static void main(String[] args) {
System.out.println(PI);
}
变量的命名规范
- 所有变量、方法、类名:见名知意
- 类成员变量、局部变量、方法名:首字母小写驼峰原则
- 常量:大写字母和下划线
- 类名:首字母大写驼峰原则
运算符
- 算数运算符:+,-,*,%,/,++,–
- 赋值运算符:=
- 关系运算符:>,<,>=,<=,==,!=
//二元运算符
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/(double)b);
//==================================================================
//关系运算符返回的结果: 正确,错误 布尔值
int a = 10;
int b = 20;
int c = 21;
System.out.println(c%a); // 1 取余,模运算
System.out.println(a>b); //false
System.out.println(a<b); //true
System.out.println(a==b); //false
System.out.println(a!=b); //true
//==================================================================
//++ -- 自增,自减 一元运算符
int a = 3;
int b = a++; //先给b赋值再自增
System.out.println(a); //4
int c = ++a; //先自增再给c赋值
System.out.println(a); //5
System.out.println(b); //3
System.out.println(c); //5
//幂运算 2^3 2*2*2 = 8 使用数学工具类
double pow = Math.pow(2,3);
System.out.println(pow); //8.0
- 逻辑运算符:&&,||,!
//与(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);
System.out.println(d); //false
System.out.println(c); //结果为5,c++未执行
- 位运算符:&,|,^,~,>>,<<,>>>
/*
A = 0011 1100
B = 0000 1101
=================================
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0010
~B = 1111 0010
=================================
2*8 = 16 2*2*2*2
<<
>>
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0000 0000 16
*/
System.out.println(2<<3); // 0010 --> 1000
- 条件运算符:?:
// x ? y : z
//如果 x==true,则结果为y,否则结果为z
int score = 80;
String type = score<60?"不及格":"及格";
System.out.println(type); //及格
- 扩展赋值运算符:+=,-=,*=,/=
int a = 10;
int b = 20;
a+=b; //a=a+b
a-=b; //a=a-b
//字符串连接符 + ,String
System.out.println(""+a+b); //1020 连接起来
System.out.println(a+b+""); //30 两数相加
包机制
- 为了更好的组织类,Java提供了包机制,用于区别类名的命名空间
- 一般用公司域名倒置作为包名:例如 com.YWB.www
package com.YWB.www
- 为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包,使用import可完成
import com.YWB.com.Demo01
JavaDoc
- JavaDoc命令用于生成自己的API文档
- 参数信息
- @author 作者名
- @version 版本号
- @since 指定需要最早使用的jdk版本
- @param 参数名
- @return 返回值情况
- @throws 异常抛出情况
/**
* @author YWB
* @version 1.0
* @since 1.8
*/
public class Doc {
String name;
/**
* @author YWB
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;
}
- 可以通过命令行 javadoc 参数 Java文件 生成JavaDoc文档,也可使用IDEA生成