JAVA基础语法
- 编译型:全局翻译
- 解释型:实时翻译
-
单行注释://
-
多行注释:/* */
-
文档注释:/** */
-
标识符:只能以大写字母、小写字母、$, _开头,不能使用关键字作为变量名或方法名。
数据类型
-
Java是一种强类型语言:所有变量都必须先定义后使用
-
数据类型:
- 引用类型:类,接口,数组
- 基本类型:
- byte: 1字节 8位
- int: 4字节 32位
- short: 2字节 16位
- long(数值后L) : 8字节 64位
- float: 4字节 32位
- double: 8字节 64位
- String: 2字节 16位
- boolean
拓展
-
完全避免使用浮点数进行比较
-
转义字符:
- \n :换行
- \t : 制表符
-
如果比较的对象是基本数据类型,则比较的是数值是否一致;如果比较的是引用类型,则比较的是对象的地址值是否一致
-
boolean flag = true; if(flag=true){}//新手 if(flag){}//老手 //Less is More! 代码要精简易读
-
进制
- 二进制: 0b
- 十进制
- 八进制:0
- 十六进制: 0x
类型转换
- byte,short,char–>int–>long–>float–>double
public class Demo04 {
public static void main(String[] args) {
int i=128;
byte b = (byte)i;//内存溢出
double c = i;
//强制转换 (类型)变量名 高-->低
//自动转换 低-->高
System.out.println(i);
System.out.println(b);
System.out.println(c);
/**
* 1.不能对布尔类型转换
* 2.不能把对象类型转换位不相干的类型
* 3.在把高容量转换到低容量的时候,强制转换
* 4.转换的时候可能存在内存溢出,或者精度问题!
*/
System.out.println("===============================");
System.out.println((int)23.7);//23
System.out.println((int)-45.89f);//-45 精度问题
System.out.println("===============================");
char d ='a';
int e = d+1;
System.out.println(e);
System.out.println((char)e);
}
}
public class Demo06 {
public static void main(String[] args) {
//操作比较大的数的时候,注意溢出问题
//jdk7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
System.out.println(money);
int years = 20;
int total = money*years;//-1474836480,计算时溢出
long total2 = money*years;//默认是int,转换前就已经存在问题了
long total3 = money*((long)years);//先把一个数转换为long
System.out.println(total3);
//L l l容易看成1;规范写L
}
}
变量
- Java是一种强类型语言,每个变量都要声明类型
- Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域。
- 每个变量都有类型,可以是基本类型,也可以是引用类型。
- 变量名必须是合法的标识符。
- 变量声明是一条完整的语句。以分号结束。
public class Demo08 {
//类变量 static 范围:在类中
static double salary = 2500;
//实例变量:从属于对象(类);如果不进行初始化,会有一个默认值 0 0.0 u0000
//布尔值:默认是false
//除了基本类型,其余默认位null
String name;
int age;
//main方法
pblic static void main(String[] args) {
//局部变量:必须声明和初始化值 范围:{}内
int i = 10;
System.out.println(i);
//变量类型 变量名字 = new Demo08();
Demo08 demo08 = new Demo08();
System.out.println(demo08.age);
System.out.println(demo08.name);
//类变量 static
System.out.println(salary);
}
//其他方法
public void add(){
}
}
常量
常量名一般使用大写
- final表示常量
- 驼峰原则
运算符
package operator;
public class Demo01 {
public static void main(String[] args) {
// 二元运算符
//ctrl + D :复制当前行到下一行
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);//0.5
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a= 12312312312312312L;
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
}
}
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增,自减 一元运算符
int a = 3;
int b = a++; //a++ a=a+1 执行完这行代码后,先给b赋值,再自增
System.out.println(a);//4
int c = ++a;//++a a=a+1 先自增,再执行这行代码
System.out.println(a);//5
System.out.println(b);//3
System.out.println(c);//5
System.out.println("==============================");
//幂运算2^3 2*2*2 =8 很多运算我们会使用工具类来操作
double pow = Math.pow(2, 3);
System.out.println(pow);
}
}
逻辑运算符
package operator;
//逻辑运算符
public class Demo5 {
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)));//逻辑非运算:如果是真,则变为假,如果为假,则变为真
System.out.println("====================");
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
}
}
位运算
package operator;
import com.sun.scenario.effect.impl.sw.java.JSWBlend_SRC_OUTPeer;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.w3c.dom.ls.LSOutput;
public class Demo06 {
/*
A = 0011 1100
B = 0000 1101
----------------------------
与 A&B =0000 1100
或 A|B =0011 1101
非 A^B =0011 0001
取反 ~B = 1111 0011
2*8 = 16 2*2*2*2
效率极高!!!!!
左移:<< *2
右移: >> /2
0000 0000 0
0000 0000 1
0000 0010 2
0000 0100 4
0000 1000 8
0001 0000 16
*/
public static void main(String[] args) {
System.out.println(2<<3);
}
}
扩展赋值运算符
package operator;
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;//a=a+b
a-=b;//a=a-b
System.out.println(a);
System.out.println("======================");
//字符串连接符 + , String 首位为String 则结果为String,中间位为String,则结果为String,末位为String,则结果为之前的运算
System.out.println(a+b);//30
System.out.println(""+a+b);//1020
System.out.println(a+""+b);//1020
System.out.println(a+b+"");//30
}
}
条件运算符
package operator;
//三元运算符
public class Demo08 {
public static void main(String[] args) {
//x ? y : z
//如果x==true,则结果为y,否则为z
int age =18;
String type = age<18?"早恋":"单身狗";//必须掌握
System.out.println(type);
}
}