Java基础
注释
Java中的注释有三种:
- 单行注释
- 多行注释
- 文档注释
public class HelloWorld {
public static void main(String[] args) {
//单行注释
//输出一个Hello World!
System.out.println("Hello World!");
/*
多行注释: 可以注释一段文字
*/
// JavaDoc:文档注释 /** */
/**
* 有趣的代码注释
* _ooOoo_
* o8888888o
* 88" . "88
* (| -_- |)
* O\ = /O
* ____/`---'\____
* . ' \\| |// `.
* / \\||| : |||// \
* / _||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' | |
* \ .-\__ `-` ___/-. /
* ___`. .' /--.--\ `. . __
* ."" '< `.___\_<|>_/___.' >'"".
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `-. \_ __\ /__ _/ .-` / /
* ======`-.____`-.___\_____/___.-`____.-'======
* `=---='
*
* .............................................
* 佛祖保佑 永无BUG
*/
}
}
标识符和关键字
-
Java 语言目前定义了 51 个关键字,这些关键字不能作为变量名、类名和方法名来使用。以下对这些关键字进行了分类。
- 数据类型:boolean、int、long、short、byte、float、double、char、class、interface。
- 流程控制:if、else、do、while、for、switch、case、default、break、continue、return、try、catch、finally。
- 修饰符:public、protected、private、final、void、static、strict、abstract、transient、synchronized、volatile、native。
- 动作:package、import、throw、throws、extends、implements、this、supper、instanceof、new。
- 保留字:true、false、null、goto、const。
-
Java所有组成部分都需要名字。类名、变量名以及方法名都被称为标识符
标识符注意点
-
所有的标识符都应该以字母(A-Z或者a-z) ,美元符($)、或者下划线()开始
-
首字符之后可以是字母(A-Z或者a-z) ,美元符($)、下划线()或数字的任何字符组合
-
不能使用关键字作为变量名或方法名。
-
标识符是大小写敏感的
-
合法标识符举例: age、$salary、_value、_1_value
-
非法标识符举例: 123abc,-salary,#abc
-
public static void main(string[] args) { String 王者荣耀 = "百星王者" System.out.printin(王者荣耀); }
可以使用中文命名,但是一般不建议这样去使用,也不建议使用拼音,很Low
数据类型
-
强类型语言
要求变量的使用要严格符合规定,所有变量必须先定义后才能使用
-
弱类型语言
要求变量的使用要严格符合规定,所有变量必须先定义后才能使用
-
Java的类型分为两大类
- 基本类型(primitive type)
-
引用类型(reference type)
public class Demo02 {
public static void main(String[] args) {
//八大基本类型数据
//整数
int num1 = 10; // 最常用
byte num2 = 20;
short num3 = 30;
long num4 = 40L;//Long类型要在数字后面加个L
//小数;浮点数
float num5 = 10.1F;//float类型要在数字后面加F
double num6 = 3.14151926535798709;
//字符
char name1 = '王';//只能写一个字
//字符串,String不算关键词,是类
//String name2 = "王。。";
//布尔值:是 非
boolean flag = true;
// boolean flag = false;
}
}
扩展
整数扩展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 010; //八进制
int i3 = 0x10; //十六进制0x 0-9 A-F
System.out.println(i);//10
System.out.println(i2);//8
System.out.println(i3);//16
浮点数扩展 银行业务怎么表示?钱
//BigDecimal 数学工具类(这个比较精确)
//float 有限 离散 舍入误差 大约 接近旦不等于
//double
//最好完全使用浮点数进行比较
//最好完全使用浮点数进行比较
//最好完全使用浮点数进行比较
float f = 0.1f; //0.1
double d = 1.0 / 10; //0.1
System.out.println(f == d); //false
float d1 = 23131312312312313f;
float d2 = d1 + 1;
System.out.println(d1 == d2);//true
字符扩展
char c1 = 'a';
char c2 = '王';
System.out.println(c1);
System.out.println((int) c1);//强制转换 97
System.out.println(c2);
System.out.println((int) c2);//强制转换 29579
//所有的字符本质还是数字
//编码 Unicode: (97 =a 65 =A) 2字节 0-65536 Excel 2 16 =65536
// u0000 UFFFF
char c3 = '\u0061';
System.out.println(c3); //Unicode转换中文 输出的是 a
转义字符
System.out.println("Hello\nworld");
对象从内存分析
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! 代码要精简易读
类型转换
-
由于Java是强类型语言,所以要进行有些运算的时候的,需要用到类型转换。
低----------------------------------------高
byte, short, char-> int-> long-> float -> double
-
运算中,不同类型的数据先转化为同一类型,然后进行运算。
-
强制类型转换
-
自动类型转换
代码案例:
//操作比较大的数的时候,注意溢出问题
// JDK7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
System.out.println(money);//下划线并不会影响输出
int years = 12;
int total = money * years;//-884901888,计算的时候溢出了
System.out.println(total);
long total2 = money * years;//-884901888,计算的时候溢出了
System.out.println(total2);//默认是int,转换之前已经存在问题了?
long total3 = money*((long)years);//先把一个数转换为long
System.out.println(total3);//12000000000
//long类型的L由于会经常把小写的l看成是1所以要写大写的L
变量
-
变量是什么:就是可以变化的量!
-
Java是一种强类型语言,每个变量都必须声明其类型。
-
Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域。
type varName [-value] [{,varName [=value]}] ;
//数据类型 变量名=值;可以使用逗号隔开来声明多个同类型变量。
-
注意事项:
- 每个变量都有类型,类型可以是基本类型,也可以是引用类型。
- 变量名必须是合法的标识符。
- 变量声明是一条完整的语句,因此每一个声明都必须以分号结束
变量作用域
- 类变量
- 实列变量
- 局部变量
示例代码:
public class Demo08 {
//属性:变量
//类变量 static
static double salary = 2500;
//实列变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0 u0000
//布尔值:默认是false
//除了基本类型,其余的默认值都是null
String name;
int age;
//main方法
public static void main(String[] args) {
//局部变量:必须声明和初始化值
int i = 10;
System.out.println(i);
//变量类型 变量名字 = new Demo08();
Demo08 demo08 = new Demo08();
System.out.println(demo08.age); //0 int默认值为0
System.out.println(demo08.name); //null String默认值为null
//类变量 static
System.out.println(salary);
}
//其他方法
public void add() {
}
}
常量
-
常量(Constant):初始化(initialize)后不能再改变值!不会变动的值。
-
所谓常量可以理解成一种特殊的变量,它的值被设定后,在程序运行过程中不允许被改变。
final 常量名 = 值;
final double =PI =3.14
-
常量名一般使用大写字符
变量的命名规范
- 所有变量、方法、类名:见名知意
- 类成员变量:首字母小写和驼峰原则: monthSalary ,除了第一个单词以外,后面的单词首字母大写
- 局部变量:首字母小写和驼峰原则
- 常量:大写字母和下划线: MAX VALUE
- 类名:首字母大写和驼峰原则: Man, GoodMan
- 方法名:首字母小写和驼峰原则: run(), runRun()
运算符
Java语言支持如下运算符:
-
算术运算符:+, -, , /, %, ++,–
package operator; public class Demo01 { public static void main(String[] args) { //二元运算符 //Ctrl +D:复制当前行到下一行 int a =10; int b =20; int c =30; int d =40; System.out.println(a+b);//30 System.out.println(a-b);//-10 System.out.println(a*b);//200 System.out.println(a/b);//0 System.out.println(a/(double)b);//0.5 要注意类型 } }
package operator; public class Demo02 { public static void main(String[] args) { long a =1231231231231231L; int b = 123; short c = 10; byte d = 8; System.out.println(a+b+c+d);//1231231231231372 Long类型 System.out.println(b+c+d);//141 Int类型 System.out.println(c+d);//18 Int类型 System.out.println((double) c+d);//18.0 Double类型 //cast 转换 } }
package operator; public class Demo04 { public static void main(String[] args) { //++ -- 自增,自减 一元运算符 int a = 3; int b = a++; //执行完这行代码后,先给b赋值,在自增 //a++ a = a+1; System.out.println(a);//4 //++a a= a+1; 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(3,2); System.out.println(pow);//9.0 } }
-
赋值运算符=关系运算符:>, <, >=, <=, ==, != instanceof
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);//1 c /a 21 / 10 = 2 ... 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 } }
-
逻辑运算符:&&, ||, !
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));//false 逻辑与运算:两个变量都为真,结果才为true System.out.println("a||b:"+(a||b));//true 逻辑或运算:两个变量有一个为真,则结果才为true System.out.println("!(a&&b):"+!(a&&b));//true 如果是真,则变为假,如果是假则变为真 //短路运算 int c=5; boolean d =(c<4)&&(c++<4);//第一个是false,所以c++没有运行,所以c返回的值是5 System.out.println(d);//false System.out.println(c);//5 } }
-
位运算符:&,|,^,~,>>, <, >>> (了解!!!)
package operator; public class Demo06 { public static void main(String[] args) { /* 位运算 A = 0011 1100 B = 0000 1101 A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 _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);//16 } }
-
条件运算符?:
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 ? "不及格" : "及格";//必须掌握 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 //字符串连接符 + , String System.out.println(a+b);//30 System.out.println(""+a+b);//1020 ""+在前面把类型换成看String System.out.println(a+b+"");//30 +""在后面看前面的类型 } }
Java运算符优先级
别 | 操作符 | 关联性 |
---|---|---|
后缀 | () [] . (点操作符) | 左到右 |
一元 | expr++ expr– | 从左到右 |
一元 | ++expr --expr + - ~ ! | 从右到左 |
乘性 | * /% | 左到右 |
加性 | + - | 左到右 |
移位 | >> >>> << | 左到右 |
关系 | > >= < <= | 左到右 |
相等 | == != | 左到右 |
按位与 | & | 左到右 |
按位异或 | ^ | 左到右 |
按位或 | | | 左到右 |
逻辑与 | && | 左到右 |
逻辑或 | | | | 左到右 |
条件 | ?: | 从右到左 |
赋值 | = + = - = * = / =%= >> = << =&= ^ = | = | 从右到左 |
逗号 | , | 左到右 |