Java基础语法
java基础语法 第一章
注释
- 平时我们编写代码,在代码量比较少的时候,我们自己能看懂,但项目复杂时候,我们就需要注释了。
- 注释不会被执行,是给我们写代码的人看的
- 书写注释是一个非常好的习惯
- 平时写代码需要注意规范
-
java中的注释有三种
-
单行注释
// 两个双斜杠为单行注释
//1232123123123
- 多行注释
/*多行注释
duoha
多行注释*/
- 文档注释
/** 以此为开头
*@Description HelloWorld
* @Author lllljava
* 每行开头有此星星
*/ //以此为结束
标识符
关键字
| 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 | thorw | throws |
| transient | try | void | volatile | while |
java所有的组成部分都需要名字。类名,变量名以及方法名都被称为标识符。
标识符是大小写敏感的
数据类型
强类型语言
- 要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用
弱类型语言
- 变量的使用不需要符合规定
java的数据类型分为两大类
- 基本类型(primitive type)

整数类型
- byte占一个字节范围 -128-127
- short占2个字节范围 -32768-32767
- int占4个字节范围 -2147483648-2147483647
- long占8个字节范围 -9223372036854775808-92233720136854775807
浮点类型
- float占4个字节
- double占8个字节
字符
- char
boolean类型:占1位其值只有true和alse两个
字符串
string 不是数据类型是一个类,但表示为字符串
//八大基本数据类型
//整数
int num1 = 10; //最常用
byte num2 = 20;
short num3 = 30;
log num4 = 30L; //Long类型要在署资后面加L
//小数:浮点数
float num5 = 50.1F; //Lfloat类型要在署资后面加F
double num6 = 3.141592653;
//字符
char name ='g'
===========================
//字符串,String不是关键字,类
//String namea = "lmk";
//布尔值:只有是与非
boolean flag = true;
//boolean flag = false;
- 引用类型(reference type)
见上图
扩展一
整数的进制
- 二进制 以 ob 开头
- 十进制
- 八进制 以 0 开头
- 十六进制 以 0x 开头
int i = 0b10;
int i2 = 10;
int i3 - 010;
int i4 = 0x10;
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
//输出结果为

//float 有限 离散 舍入误差 大约 接近但不等于
//double
char 字符
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int) c1);//强制转换为数字
System.out.println(c2);
System.out.println((int) c2);//强制转换为数字
输出结果
//float 有限 离散 舍入误差 大约 接近但不等于
//double
char 字符
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int) c1);//强制转换为数字
System.out.println(c2);
System.out.println((int) c2);//强制转换为数字
输出结果

//所有的字符本质还是数字
a的数字字符就是97
中的数字字符就是20013
//依据编码表 Unicode:0-65536
//转义字符
char c3 = '\u0061';
System.out.println(c3);
//====================
//转义字符
System.out.println("Hello\tWorld");
//\t 制表符
System.out.println("Hello\nworld");
//\n 换行
//==============
输出结果

boolean flag = true;
if (flag == true) { //flag =true
}
if (flag) { //flag = true 虽然没写明,但和上面的结果是一样的
}
//less is More! 代码要精简易读
类型转换
由于java是强类型语言,所以要进系有些运算的时候,需要用到类型转换。
//低------------------------高
byte,short,char ->int -> long -> float -> double
PS:float大于long的原因是,小数的优先级大于整数。
//运算中,不同类型的数据先转化为同一类型,然后再进系运算。
public class dom4 {
public static void main(String[] args) {
int i = 128;
byte b = i;
}
}
结果在byte处报错;原因是,int定义了i为128,byte只占一个字节(-128-127),所以但定义byte为128时提示错误;需要进行转化;
byte转int
public class dom4 {
public static void main(String[] args) {
int i = 128;
byte b = (byte) i;//加括号是进行强制转换。这里会造成内存溢出!!!
System.out.println(i);
System.out.println(b);
}
}
结果:

输出没有问题,但是结果错误,b的值变为了128;这里的错误原因为内存溢出;根源为byte只占一个字节(-128-127);
低转高
public class dom4 {
public static void main(String[] args) {
int i2 = 128;
double b2 = i2;
System.out.println(i2);
System.out.println(b2);
}
}
输出结果:

低转高则为自动转换;
注意点###
- 不能对布尔值进行转换
- 不能把对象类型转换为不相干的类型
- 在把高容量转换为低容量的时候,强制转换。反之自动
- 转换的时候可能存在内存溢出,或者精度的问题
精度案例
public class dom4 {
public static void main(String[] args) {
System.out.println((int)23.7);
System.out.println((int)-45.85f);
}
}
输出结果:

可以看出浮点类型转为整数类型时,精度会发生变化。
字符转换
没转之前案例
public class dom4 {
public static void main(String[] args) {
char c = 'a';
int d = c+1;
System.out.println(d);
}
}
结果:

没有问题
整数转字符案例
public class dom4 {
public static void main(String[] args) {
char c = 'a';
int d = c+1;
System.out.println((char)d);//!!!高容量转低容量需要强制转换
}
}
输出结果

分析:char类型定义了c为a,int定义了b为c+1,当输出结果强制把整数类转换为字符集时,过程为字符a转换为int类型为97,+1为98。强制转为为char时98对应的编码表字母就为b。
jdk7的新特性,数字之间可以用下划线分割来让数字更好的区分位。
下划线分割案例
public class dom4 {
public static void main(String[] args) {
int $ = 10_0000_0000;
System.out.println($);
}
}
输出结果:

操作较大时,注意溢出问题
溢出案例:
public class dom4 {
public static void main(String[] args) {
int $ = 10_0000_0000;
int years = 20;
int total = $*years;
System.out.println(total);
}
}
输出结果:

分析:输出结果正确的应该为20000000000,结果却为负数。原因:20000000000大于了int所占的位;
思考:int占位不够,则使用long试试
案例
public class dom4 {
public static void main(String[] args) {
int $ = 10_0000_0000;
int years = 20;
int total = $*years;
System.out.println(total);
long total2 = $*years;
System.out.println(total2);
}
}
输出结果

分析:为什么换位long后还是溢出。原因:$和years 都为int,定义long时自动转为int了。在转换之前就已经存在问题了。
正确案例
public class dom4 {
public static void main(String[] args) {
int $ = 10_0000_0000;
int years = 20;
int total = $*years;
System.out.println(total);
long total2 = $*years;
System.out.println(total2);
long total3 = $*((long)years);
System.out.println(total3);
long total4 = ((long)$)*years;
System.out.println(total4);
long total5 = ((long)$)*((long)years);
System.out.println(total5);
}
}
输出结果

转换为long后正确。
思考:为什么转换时,只用转换一个变量名称就可以达到不报错,而不是像total5一样两个都进行转换。
答案:当一个较“小”的数据和较“大”的数据一起运算的时候,系统将自动将较“小”的数据转换为较“大”的数据,再进行运算。
total3中只转换了变量years,但当把years和$进行运算时,触发自动转换,自动 “($)” 把转换为了long

浙公网安备 33010602011771号