4 JAVA基本语法
JAVA基础语法
注释、标识符、关键字
首先可以通过写Hello World熟悉一下
public class Demo01{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
关键字 public表示访问说明符,表明该类是一个公共类,可以控制其他对象对类成员的访问。
关键字 class 用于声明一个类,其后所跟的字符串是类的名称。
关键字 static 表示该方法是一个静态方法,允许调用 main() 方法,无须创建类的实例。
关键字 void 表示 main() 方法没有返回值。
main() 方法是所有程序的入口,最先开始执行
编程:psvm--sout
注释
在程序中,注释不会被执行,它用于对程序进行解释
种类:
- 单行注释 //
- 多行注释 /* */
- 文档注释 /** */
标识符
类名、变量名、方法名都为标识符

所有的标识符都以字母(A-Z或者a-z),美元$,下划线_ 开始
首字符之后可以是字母(A-Z或者a-z),美元$,下划线_ ,或者数字的任何字符组合
不能使用关键字作为变量名或方法名
标识符区分大小写
一般来说可以使用中文命名,但是不建议使用

数据类型
强类型语言
要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用
安全性高、速度较慢
弱类型语言
VB JS
Java的数据类型
字节
位(bit):计算机内部数据存储的最小单位,11001100是一个八位二进制数
字节(byte):计算机中数据处理的基本单位,习惯用B表示
1B=8bit
字符:计算机中使用的字母、数字、字和符号
1bit表示1位
1Byte表示一个字节 1B=8b
1024B=1KB
1024KB=1M
1024M=1G
1024G=1T
基本数据类型(primitive type):
- 数值类型
- 整数类型
- byte占1个字节范围:-128-127
- short占2个字节范围:-32768-32767
- int占4个字节范围:-2147483648-2147483647
- long占8个字节范围:-9223372036854775808-9223372036854775807
- 浮点类型
- float占4个字节
- double占8个字节
- 字符类型 char占2个字节 写程序需要增加单引号
- 整数类型
- boolean类型:占1位,其值只有true和false 1 0
public class demo03 {
public static void main(String[] args) {
//整数拓展 进制:二进制0b、十进制、八进制0、十六进制0x
int i = 10; //十进制
int i2 = 010; //八进制0
int i3 = 0x10;
int i4 = 0x1F; //十六进制0x 0~9 A~F(10~15)
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
System.out.println("=============================");
//浮点数拓展 银行业务如何表示,钱
//用一个类来进行表示 BigDecimal 数学工具类
//float 能表现的字符有限 离散 有舍入误差 结果是大约数,接近但不等于
//double
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
float f = 0.1f; //0.1
double d = 1.0/10; //0.1
System.out.println(f);
System.out.println(d);
System.out.println(f==d); //判断是否一样 false
float d1 = 23569585f;
float d2 = d1+1;
System.out.println(d1==d2); //true
System.out.println("=============================");
//字符类拓展
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1); //强制转换
System.out.println(c2);
System.out.println((int)c2); //强制转换
// 所有的字符本质还是数字
// 编码 Unicode 表格:(97=a,65=A) 2字节,65536个字符 0-65536 Excel,最早的有2的16次方行,即65536行
// U0000-UFFFF
char c3 = '\u0061';
System.out.println(c3);
System.out.println("=============================");
//转义字符 后期查找
// \t 制表符
// \n 换行
System.out.println("hello\tworld");
System.out.println("hello\nworld");
System.out.println("=============================");
//思考有什么区别?
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa==sb); //比较地址
String sc = "hello world";
String sd = "hello world";
System.out.println(sc==sd); //比较内容
//对象 从内存级别进行分析
//布尔值拓展
boolean flag = true;
// less is more
if (flag==true);{ } //if(flag){}
}
}
引用数据类型(reference type):
- 类
- 接口
- 数组
类型转换
低-----------------------------------------高
byte--short,char--int--long--float--double
运算中,不同类型的数据要先转化成同一类型,然后进行运算
强制类型转换 高到低
自动类型转换 低到高
public class demo04 {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i; //byte最大127,赋值128,会内存溢出,所以转换时要尽量避免
//强制转换 高到低
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 demo05 {
public static void main(String[] args) {
//操作比较大的时候,注意溢出
//数字之间可以用下划线分割
int money = 10_0000_0000;
System.out.println(money);
int year = 20;
System.out.println(year);
int total = money*year; //-1474836480 计算时溢出了
System.out.println(total);
long total2 =money*year;
System.out.println(total2); //默认是int,转换前已经存在问题
//
long total3 = money*((long)year); //先把数转换为long,统一之后进行计算
System.out.println(total3);
System.out.println("=======================");
long a = 1000;
long b = 20;
long c = a*b;
System.out.println(c);
//
}
}
变量、常量
变量:可以变化的量 ---可以用来存储数据。
java 是一种强类型语言,每一个变量都需要声明其类型
java 变量是程序中最基本的存储单元,其要素包括变量名,变量类型,作用域
public class demo06 {
public static void main(String[] args) {
//int a,b,c ; 不推荐写在同一行,确保程序的可读性
int a=1,b=1,c=1;
String name ="梦想";
char x = 'X';
double pi = 3.14;
}
}
注意事项:
- 每个变量都有类型,类型可以是基本类型,也可以是是引用类型--String等
- 变量名必须是合法的标识符
- 变量声明是一条完整的语句,因此每个声明都需要以分号结束
作用域:
- 类变量 static
- 实例变量
- 局部变量 写在方法中
public class demo07 {
//类变量 增加static
static double salary = 2500;
//main方法
//属性:变量
//实例变量:从属于对象;在类的下面,不在方法的下面;
// 如果不自行初始化,这个类型的默认值 数值型的默认值:0 0.0 U0000
//布尔值 boolean 默认是false
//除了基本类型(八大基本类型),其余的默认值都是null 如引用型
String name;
int age;
//==========
public static void main(String[] args) {
//局部变量:必须声明和初始化值
int i = 10;
System.out.println(i);
//实例 变量类型 变量名字 = new demo07(); new的意思是:拿到了自己的变量
demo07 demo07 = new demo07(); //ALT 回车+回车
System.out.println(demo07.age);
System.out.println(demo07.name);
//类变量 增加static
System.out.println(salary);
}
//其他方法
public void add(){ }
}
常量:初始化后不能再改变值 // final 常量名=值
常量名一般使用大写字符
public class demo08 {
//修饰符不存在先后顺序 变量类型前的都是修饰符
final static double PI = 3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}
变量的命名规范
- 所有变量、方法、类名:见名知意
- 类成员变量:首字母小写和驼峰原则:monthSalary 除了第一个单词以外,后面的单词首字母大写
- 局部变量:首字母小写和驼峰原则
- 常量:大写字母和下划线:MAX_VALUE
- 类名:首字母大写和驼峰原则:Man,GoodMan
- 方法名:首字母小写和驼峰原则:run(),runRun()
运算符

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((float)a/b);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 12121212l;
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 cast转换
}
}
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果:正确or错误 通过布尔值来反映
//一般配合if 使用
int a = 10;
int b = 20;
int c = 21;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
//取余,模运算
System.out.println(c%a); // c/a 21/10=2....1
}
}
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ 自增 -- 自减 一元运算符
int a = 3; //初始值3
int b = a++; // a++ = a+1 加号放到后面:执行这行代码后,先给b赋值,再自增
System.out.println(a); //此时 a = 4
int c = ++a; // ++a = 1+a 加号放前面:执行这行代码前,先自增,再给c赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算 2^3 2*2*2 很多运算使用工具来进行 看源码
double pow = Math.pow(2, 3);
System.out.println(pow);
}
}
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));// 如果是真则为假,如果未假则为真
System.out.println("========================================");
//短路运算 a&&b 判断时,如果a为假,则直接判定false,不再往后继续运行
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
System.out.println("========================================");
//位运算 二进制相关
/*
* A = 0011 1100
* B = 0000 1101
* ----------------------------------------------
* A&B=0000 1100 都为1则为1
* A|B=0011 1101 都为0则为0
* A^B=0011 0001 如果位置相同,则为0,否则为1
* ~B =1111 0010
* ----------------------------------------------
* 2*8=16 2*2*2*2=16 2*2^3=16
* << >> 左移、右移(*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 Demo06 {
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);
//字符串连接符 +: 只要有一方出现字符串。 比如 “” 是String类型,就会将两边都转换为String
System.out.println(a+b);
System.out.println(""+a+b); // 1020 字符串在前面,会进行拼接
System.out.println(a+b+""); // 30 字符串在后面,会进行计算
}
}
package operator;
public class Demo07 {
public static void main(String[] args) {
//三元运算符 ? :
// x ? y :z 如果x==true,结果为y,否则为z 面试成功 ?年薪30万 :回家种地
int score = 80;
String type = score < 60 ? "不及格":"及格"; //需要掌握
System.out.println(type);
}
}
包机制、JavaDoc
包机制
用于区别类名的命名空间
语法格式:
package pkg[.pkg2[.pkg3...]]
一般利用公司域名倒置作为包名:www.baidu.com ----> com.baidu.www com.kuangstudy.blog com.M.xxx
为了能够使用某一个包的成员,我们需要再Java程序中导入该包,使用“import”语句即可
inmport package1[.package2...].(classname|*);
JavaDoc
javadoc用来生成自己API的文档
参数信息:
- @author 作者名
- @version 版本号
- @since 指明需要最早使用的jdk版本
- @param 参数名
- @return 返回值情况
- @throws 异常抛出情况
package com.m.base;
/**
* @author meng
* @version 1.0
* @since 1.8
*
*/
public class Doc {
String name; //属性
/**
* @author meng
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception { //方法
return name;
}
}
学习使用IDEA生成Javadoc文件

- 存放位置
- 语言:中文
- 编码集

浙公网安备 33010602011771号