Java基础语法
基础语法学习
注释
// 单行注释
/* xxxxxx */ 多行注释
/** xxxxx */ 文档注释

tip:IDEA可以改变字体 颜色等
标识符
-
Java标识符以字母(大写或小写),美元符,或者下划线开始。之后可以是字母、美元符、下划线或任何字符组合。
-
大小写敏感
-
不能使用关键字做标识符
数据类型
tip:IDEA快捷键:ctrl+d 复制单行至下一行
基本类型
-
整数类型
- byte: 1B -128~127 (Byte)
- short: 2B -32768~32767
- int: 4B (Integer)
- long: 8B 如:long num = 30L; 要加L
-
浮点类型
-
float: 4B 如:float num = 50.1F; 要加F
float有限、离散、有舍入误差,最好完全避免使用浮点数进行比较判断
可以使用BigDecimal 数学工具类
-
double: 8B
-
-
字符类型
-
char: 2B ‘ x’
字符串String 不是关键字,是类 如:"zxzz"
字符本质还是数字 如:(int)num强制转换
转义字符 : '\t' 制表符 '\n' 换行符
-
-
boolean类型
占一位 true or false
引用数字类型
- 类
- 接口
- 数组
进制
二进制 十进制 八进制0 十六进制0x
类型转换
低--------------------------------------------->高
byte,short,char->int ->long ->float ->double
public class Demo3 {
public static void main(String[] args) {
int i =128;
byte b =(byte)i;//内存溢出
//强制转换 (类型)变量名 高->低
//自动转换 低->高
System.out. println(i);
System.out. println(b);
System.out.println((int)23.7);//23
System.out.println((int)-45.89f);//-45
}
}
注意:
- 不能对布尔值转换
- 不能把对象转换为不相干类型
- 高容量转换为低容量,强制转换
- 转换时存在内存溢出或精度问题
public class Demo6 {
public static void main(String[] args) {
int money = 10_0000_0000;//数字可以用下划线分割
int years=20;
int total=money*years;//计算溢出
long total2 = money*years;//输出错误。默认int,转换前已经存在问题
long total3 = money*((long)years);//正确。先把一个数转换为long
}
}
变量
- 每个变量都有类型,可以是基本类型也可以是引用类型
- 变量名必须是合法的标识符
- 变量声明是一条完整的语句,因此以分号结束
type varName = value; 可以用逗号隔开声明多个同类型变量
public class Demo7 {
public static void main(String[] args) {
int a=1;//换行保证可读性
int b=2;
int c=3;
String name = "xxxx";
char x = 'x';
double pi = 3.14;
}
}
public class Demo8 {
//类变量 static
static double salary = 2500;
//属性:变量
//实例变量(成员变量): 从属于对象
String name;
int age;
//不进行初始化,会赋默认值
//数值型、字符型默认值:0 0.0 布尔值:false 除了基本类型,其余默认null
//main 方法
public static void main(String[] args) {
//局部变量:必须声明和初始化值
int i = 10;
System.out.println(i);
//变量类型 变量名字 = new 构造方法名(参数)
Demo8 deom8 = new Demo8();
System.out.println(deom8.age);//输出0
System.out.println(deom8.name);//输出null
//类变量 static
System.out.println(salary);
}
}
常量
初始化后不能再改变值
final 常量名 = 值
常量名通常使用大写字母
public class Demo9 {
//修饰符不存在先后顺序 static final.... 可换顺序
static final double PI = 3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}
命名规范
- 所有变量、方法、类名 见明知意
- 类成员变量:首字母小写和驼峰原则:monthSalary 除第一个以外,后面的单词首字母大写
- 局部变量:首字母小写和驼峰原则
- 常量:大写字母和下划线
- 类名:首字母大写和驼峰原则 Man GoodMan
- 方法名:首字母小写和驼峰原则
运算符
- 算数运算符:+ - * / % ++ --
- 赋值运算符: =
- 关系运算符:>, <, >=, <=, ==, !=, instanceof
- 逻辑运算符: & | ^ ~ >> << >>>
- 条件运算符 : ? :
- 扩展运算符: +=, -= , *= , /=
public class Demo01 {
public static void main(String[] args) {
int a = 10;
int b = 26;
int c = 30;
int d = 20;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);//强转
}
}
public class Demo02 {
public static void main(String[] args) {
long a = 12313123123L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);//long
System.out.println(a+d+c);//int
System.out.println(a+b);//int
//多个操作中,有long为long,没有long为int
}
}
public class Demo03 {
public static void main(String[] args) {
//++ -- 自增 自减 一元运算符
int a = 3;
int b = a++; //先赋值再自增
int c = ++a; //先自增再赋值
}
}
//幂运算 2^3 2*2*2 = 8 可以使用工具类操作
double pow = Math.pow(2,3);
System.out.println(pow);
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);//false
System.out.println(c);//5
public class Demo05 {
public static void main(String[] args) {
/*
A = 0011 1100
B + 0000 1101
A&B 0000 1100 与
A|B 0011 1101 或
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);// 16
}
}
public class Dimo6 {
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(""+a+b);//1020
System.out.println(a+b+"");//30 在前侧会相加
}
}
public class Dimo07 {
public static void main(String[] args) {
//x ? y : z
//如果为true,则结果为x,否则为z
int score = 50;
String type = score < 60 ? "不及格":"及格";
System.out.println(type);
}
}
包机制
定义包:
package pkg1[.pkg2[.pkg3...]];
一般利用公司域名倒置作为包名 com.Huang.xxx
导入包:
import package1[.package2...].(classname|*);
import com.Huang.base.*(通配符) 导入包下的所有类
javaDoc
命令行:javadoc -encoding UTF-8 -charset UTF-8 doc.java
/**
* @author hqw
* @version 1.0
* @since 1.8
*/
//加在类之上就是类的注释,加在方法上就是方法的注释
public class Doc {
String name;
/**
* @auther 作者名
* @version 版本号
* @since 指明版本需要最早使用的jdk版本
* @param name 参数名
* @return 返回值情况
* @throws Exception 异常抛出情况
*/
public String test(String name) throws Exception{
return name;
}
}

浙公网安备 33010602011771号