注释
单行注释 //
多行注释 /* */
文档注释(JavaDoc) /**
*这个通常使用在类上
*还有方法上
*/
标识符
关键字
abstrct assert boolean break byte
case catch char class const
continue default do double else
等等…
注意点
- 所有的标识符都应该以字母(A-Z或者a-z),美元符($),或者下划线(_)开始
- 首字母之后可以是字母,美元符,下划线或数字的任何字符组合
- 不能使用关键字作为变量名或者方法名
- 标识符是大小写敏感的
数据类型
java的数据类型分为两大类:
-
基本类型(primitive type)
-
引用类型(reference type)
基本数据类型(primitive type)
(面试、笔试必考)
数值类型
数值型 8位: 0000 0000
- 整数类型
byte:1个字节 -128-127(2^7-1) 占8位
short:2个字节 -32768-32767 占16位
int:4个字节 -21亿-21亿 占32位
long:8个字节 占64位
int num1 = 10;
byte num2 = 20;//若 byte num2=200;会报错,因为byte占一个字节范围,数值在-128~127之间
short num3 = 30;
long num4 = 30L;//Long类型要在数字后面加个L
拓展:进制在java中如何表示
二进制(0b)
八进制(0)
十六进制(0x)
public class Demo03{
public static void main(String[] args){
//二进制(0b) 十进制 八进制(0) 十六进制(0x)
int i = 10; //十进制
int i2 = 0b10; //二进制 0b
int i3 = 010; //八进制 0
int i4 = 0x10; //十六进制 0x
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
}
}
运行结果截图:
- 浮点类型(小数、浮点数)
float:4个字节
double:8个字节
float num5 = 50.1F;//float类型也要在数字后面加个F
double num 6 = 3.1415926535;
拓展:银行业务怎么表示?钱
BigDecimal :数学工具类
float特点:有限 离散 舍去误差 大约 接近但不等于
最好完全避免使用浮点数进行比较
public class Demo03{
public static void main(String[] args){
//float特性:有限 离散 舍入误差 大约
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d); //flase
float d1 = 2333569822211f;
float d2 = d1 + 1;
System.out.println(d1==d2); //true
}
}
运行结果截图:
字符类型
char:2个字节 16位 unicode
//单个字符
char name = 'A';
//字符串,String不是关键字,是类
String name = "abc";
拓展:
- 所有字符的本质还是数字
- 编码:Unicode 2字节 0-65536个字符(现在可以更多)
- U0000-UFFFF
- 转义字符:\t 制表符 \n 换行 \u
public class Demo03{
public static void main(String[] args){
//所有字符的本质还是数字
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1); //强制转换
System.out.println(c2);
System.out.println((int)c2); //强制转换
char c3 = '\u0061'; // \u 为转义字符
System.out.println(c3); //a
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); //ture
}
}
运行结果截图:
boolean类型
占一位,1/8个字节,值只有true和false两个。
//布尔值:是非
boolean flag = true;
//boolean flag = false;
拓展:
boolean flag = true;
if(flag==true){} //新手
if(flag){} // 老手
// Less is More! 代码要精简易读
引用数据类型(reference type)
类
接口
数组(string)
类型转换
由于java是强类型语言,要进行有些运算时,需要进行类型转换。转换分为两种
- 强制转换 优先级: 高------->低
int i = 97;
byte b = (byte)i; // (类型) 变量名
- 自动转换 优先级: 低------->高
int i = 97;
double = i;
优先级由低到高比较:
低----------------------------------------------------------------------->高
byte,short,char,–> int --> long --> float --> double (小数的优先级一定大于整数)
运算中,不同类型的数据要先转换成同一类型数据,然后再进行运算。
注意点:
- 不能对布尔值进行转换
- 不能把对象类型转换为不相干类型
- 在把高容量转换为低容量时,强制转换
- 转换的时候可能会出现内存溢出或精度问题
public class Demo04 {
//类型转换
public static void main(String[] args) {
int i = 128;
byte b = (byte)i;//内存溢出(byte,所以输出结果为-128
double d = i; //自动转换
System.out.println(i);
System.out.println(b);//结果为-128,内存溢出
System.out.println(d);//自动转换
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(c);
System.out.println(e);
System.out.println("=============================");
//操作比较大的数时,注意溢出问题
//JDK7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
int years = 20;
int total = money*years;
System.out.println(total);//-1474836280,溢出
long total2 = money*years;//默认是int,转换之前已经存在问题了
System.out.println(total2);//-1474836480,溢出
System.out.println("=============================");
//如何解决溢出
long total3 = money*((long)years);//先把一个数转换为long型
System.out.println(total3);
}
}
运行结果截图:
变量、常量、作用域
-
变量,即可以变化的量
-
java是一种强类型语言,每个变量都必须声明其类型
-
java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域
type varname [=value] [{,varname [=value]}];
//数据类型 变量名 = 值;可以使用逗号隔开声明多个同类型变量。
注意事项:
- 每个变量都有类型,类型可以是基本类型,也可以是引用类型
- 变量名必须是合法的标识符
- 变量声明是一条完整的语句,因此每一个声明都必须以分号结束
变量作用域
- 类变量 :static 从属于类,随着类一起出现,一起消失
- 实例变量:从属于对象(在类里面,方法的外面)
这个类型的变量默认值为0,0.0 布尔型默认值为false 除了基本类型,其余默认值都是null
- 局部变量:必须声明和初始化值
public class Variable{
static int allClicks = 0; //类变量
String str = "hello world"; //实例变量
public void method(){
int i = 0; //局部变量
}
}
public class Demo06 {
//类变量 static 从属于类,随着类一起出来,一起消失
static double salary = 2500;
//实例变量:从属于对象(在类里面,方法的外面)
String name;
int age;
//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(){
}
}
运行结果截图:
常量(constant)
- 初始化后不能再改变值,不会变动的值
- 可以理解为一种特殊的变量,它的值被设定后,在程序运行过程中不允许被改变。
- 常量名一般用大写字符。
// final 常量名 = 值;
final double PI = 3.14;
变量的命名规范
package zengstudy.day03;
/**
* 变量名命名规则
*/
public class Test002 {
public static void main(String[] args) {
/**
* 变量名命名规则
* 首字母只能是:字母 _ $
*/
int _abc = 10;
int $bbb = 20;
int aaa = 30;
/**
* 首字母之后:字母 _ $ 数字
*/
int as6t = 1;
int a_his = 2;
/**
* 实际开发
* 取名要有意义 驼峰命名法 helloWorld
* 变量名:首字母小写 第二单词首字母大写
* 类名:首字母大写 第二单词首字母大写
*/
}
}
- 所有变量、方法、类名:见名知意
变量名:
首字母:字母、下划线’_’、和’$'符号
其余部分:数字、字母、下划线’_‘和’$'符号
- 类成员变量:首字母小写和驼峰原则(除了第一个单词以外,后面的单词首字母大写)
monthSalary
- 局部变量:首字母小写和驼峰原则
- 常量:大写字母和下划线
MAX_VALUE
- 类名:首字母大写和驼峰原则
Man,GoodMan
- 方法名:首字母小写和驼峰原则
run(),runSpeed()
基本运算符
- 算术运算符:+,-,*,/,%(取余),++(自增),–(自减)
public class Demo04 {
public static void main(String[] args) {
//++ :自增 --:自减 一元运算符
int a = 3;
int b = a++; //先给b赋值,再自增:int b = a; a=a+1;
//a++ a=a+1
System.out.println(a);
//++a a=a+1
int c = ++a; //先自增,再给c赋值:a=a+1; int c = a;
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
}
}
运行结果截图:
-
赋值运算符:=
-
关系运算符:>,<,>=,<=,==,!=instanceof
package operator;
//关系运算符
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果:正确,错误 布尔值
//一般在if语句中使用
int a = 10;
int b = 20;
int c = 11;
System.out.println(c%a); //取余,模运算
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
System.out.println(a>=b);
System.out.println(a<=b);
}
}
运行结果:
- 逻辑运算符:&&(与),||(或),!(非)
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));
System.out.println("a || b:"+(a || b));
System.out.println("! (a && b):"+! (a && b));
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<10);//前半部分为假,则不再执行后半部分,因此c++(c=c+1)没有执行
System.out.println(c);
System.out.println(d);
}
}
运行结果:
- 位运算符:&,|,^,-,>>,<<,>>>
package operator;
//位运算:与二进制数有关 &,|,^,~,>>,<<
public class Demo06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
-----------------------------------------------
A&B = 0000 1100 对应位都是1,则结果为1,否则为0
A|B = 0011 1101 对应位都是0,则结果为0,否则为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);
}
}
运行结果:
- 条件运算符:?:
package operator;
//三元运算符
public class Demo08 {
public static void main(String[] args) {
//条件运算符 ?:
// x?y:z
//如果x==true, 则结果为y,否则结果为z
int score = 80;
String type = score < 60 ? "不及格" : "及格";
//if
System.out.println(type);
}
}
运算结果:
- 扩展赋值运算符:+=,-=,*=,/=
package operator;
//扩展赋值运算符
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b; //a=a+b
System.out.println(a); //30
a-=b; //a=a-b
System.out.println(a);//10
a*=b; //a=a*b
System.out.println(a);//200
a/=b; //a=a/b;
System.out.println(a);//10
//字符串连接符 + 在“+”运算符两侧,只要有一侧出现string类型,它会把另外一个操作输出的数都转换为string类型再进行连接
System.out.println("========================");
System.out.println(a);
System.out.println(b);
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
运行结果: