IDE:用于提供程序开发环境的应用程序
一般包括代码编辑器,编译器,调试器和图形用户界面等工具
常见的java开发工具有: IntelliJ IDEA, Eclipse
注释-
单行注释: // 对某行内容进行解释说明
-
多行注释: 对多行内容进行解释说明
-
文本注释: 对文档进行说明(适用于包,类,变量,方法 调用时可以看到注释信息)
-
关键字:被java程序赋予了特殊含义的单词 主要用来修饰包,类,变量,方法
-
保留字:java现版本尚未使用的关键字 后期可能会用(例如goto,const)
-
对包,类,变量,方法进行命名的字符序列 即自己命名的名称
-
命名规范
2.1 语法强制
① 可以用大小写字母,数字,下划线,$
②不可以用数字开头 不能使用关键字或保留字 不能有空格
③区分大小写 长度无限制
2.2 约定俗成
①写名知意
②驼峰表示(timeName time_name)
③类名首字母大写 变量,方法名首字母小写(目的:区别两者)
-
常见
包:字母全部小写
类,接口名:每个单词的首字母大写
变量,方法:除第一个单词外其他单词首字母大写
常量:字母全部大写
-
变量:变量时程序中的基本存储单位 运行时值可变
本质是内存中的一块区域
使用:先声明 后赋值
-
声明语法(java还强类型)
格式: [修饰符] 数据类型 变量名 = 值 ;
Sting:是java核心类库中定义好的一个类,表示字符串
-
可以指导程序中的数据如何存储 如何运算
-
数据类型分类
2.1 基本类型
数值型(整数类型:byte short int long; 浮点类型:float double)
字符型:char
布尔型:boolen
2.2 引用类型
类:class
接口
数组
-
整数类型
byte:1字节
short:2字节
int:4字节
long:8字节 (值后面要加上L或者l)
byte a = 5; short b = 7; int c = 55; long d = 77L; System.out.println(Byte.MAX_VALUE); System.out.println(Short.MAX_VALUE); System.out.println(Integer.MAX_VALUE); System.out.println(Long.MIN_VALUE);
-
整数其他进制表示方法
二进制:0b11
八进制:011
十六进制:0x11
byte a = 0b11; byte b = 011; byte c = 0x11;
-
float: 4字节 单精度 声明时在字面量后面加上F或f
-
double: 8字节 双精度 浮点数字面量默认为double类型
-
两种表现形式
十进制
科学计数法
boolean 适用于逻辑运算
java中只能用true,false
不能用1,0表示
boolean = true; boolean = false;字符型
char: 2字节
可以表示一个字符,java中用单引号
char值可以参与算术运算 使用编码表中的十进制对应值进行运算
char = 'a'; char = 10;
java中的字符使用Unicode编码表
可以表示世界上所有国家的语言
Unicode主要存储对照字符
utf-8时Unicode中一种具体的表示方式
英文字母少 一个字符可以存储 所有英文字母一个字母在utf-8中占一个字节
中文多 一个字符无法存储 在utf-8中一个中文占3个字节
基本数据类型转换除了布尔型之外 其他7种基本数据类型之间可以相互转换
默认转换(隐式转换)由容量小的类型转换为容量大的类型
byte a = 10; int b = a; long c = b;
byte short char ---> int ---> long ---> float --->double
强制类型转换由容量大的类型转换为容量小的类型
带来的问题:
-
数据溢出
int a = 258; byte b = (byte)a; System.out.println(b); //输出2
-
精度降低
float a = 10.35f; long b = (float)a; System.out.println(b); //输出10
在混合运算中,小类型会默认转换为大类型
int a =5; int n = (int)(10*3.5f+a); //10*3.5f+a为double类型运算符 1. 算术运算符
① +
可进行加法运算(数值+数值;数值+字符)
可进行连接(字符串+字符串;字符串+数值)
int a = 5; int b = 7; String c ="ab"; String d ="cd"; System.out.println(a+b); //12 数值+数值 System.out.println(a+'c'); //104 数值+字符(c的编码表对于99) System.out.println(c+d); //abcd 字符串+字符串 System.out.println(a+c); //5ab 数值+字符串
② - * /
只进行算法运算
int a = 10; int b = 5; System.out.println(a-b); //输出5 System.out.println(a*b); //输出50 System.out.println(a/b); //输出2
③++ -- ++x --x
单独的++ -- ++x --x无区别
混合运算中++ --先运算后自增 ++x --x先自增后运算
y = x++ +1; //即 y=x+1,x=x+1 先运算再自增 y = ++x +1; //即 x=x+1,y=x+1 先自增再运算2. 关系运算符
①> < >= <=
只能进行数值之间的比较
②== !=
可以进行数值之间的比较 还可以进行逻辑值与逻辑值,引用类型与引用类型比较
boolean a = true; boolean b = false; System.out.println(a==b); //输出false System.out.println(a!=b); //输出true String a = "abc"; String b = "efg"; System.out.println(a==b); System.out.println(a!=b);3. 逻辑运算符
| & ! && || ^
是逻辑值(false和true)之间的运算
①& 逻辑与 && 短路与
int a = 5; int b = 7; int c = 10; System.out.println(ac); //全为真时输出真 System.out.println(ac); //全为真时输出真 System.out.println(a>b && b>c); //出现第一次false时后面不运算
②| 逻辑或 || 短路或
int a = 5; int b = 7; int c = 10; System.out.println(ac); //有真时输出真 System.out.println(ac); //有真时输出真 System.out.println(a>b || b>c); //出现第一次true时后面不运算4. 赋值运算符
①=
把右边的值赋给左边
数据类型 变量 = 值;
byte a = 10; int b = a; //默认转换 short c = (short)b; //强制转换
②+= -= *= /=
赋值运算 会进行隐形的类型转换
例题:
short s = 3; s = s+3; //报错 s+3上升为int类型 s += 3; //输出6 隐式的类型转换 s=(short)(s+3)5. 条件运算符
(条件表达式)?表达式1:表达式2
true时返回表达式1
false时返回表达式2
int a = 10; int b = 5; int max = (a>b)?a:b; //输出10 String s = (a>b)?"成立":"不成立"; //输出成立6. 位运算
对bit位进行运算
①>> << >>>
int a = 10; System.out.println(a<<1); //二进制bit位左移 System.out.println(a>>1); //二进制bit位右移 System.out.println(a>>>1); //二进制bit位右移 无符号右移(负数移位后会默认补零为正数)
②& | ~ ^
int x = 4; int y = 3; System.out.println(x & y); //输出0 System.out.println(x | y); //输出7 System.out.println(~x); //输出-5 System.out.println(x ^ y); //输出7流程控制台
仅在JAVASE当中测试程序时使用
向程序中实现输入数据
java提供了一个类Scanner
import java.util.Scanner;
public class ScannerDome {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); //
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入年龄:");
int age = scanner.nextInt();
System.out.println("请输入星座:");
String constellation = scanner.next();
System.out.println("姓名:"+name+"年龄:"+age+"星座:"+constellation);
}
}
流程控制语句
1. 条件语句
if选择语句
① if(条件表达式){ 条件表达式为真执行代码块 }
② if(条件表达式){ 条件表达式为真执行代码块 }else{ 条件表达式为假执行代码块 }
③ if(条件表达式){ }else if(条件表达式){ }else if(条件表达式){ }else{ }
public class IfDome {
public static void main(String[] args) {
int w = 5;
if (w == 1){
System.out.println("星期一");
}else if (w == 2){
System.out.println("星期二");
}else if (w == 3){
System.out.println("星期三");
}else if (w == 4){
System.out.println("星期四");
}else if (w == 5){
System.out.println("星期五"); //输出星期五
}else if (w == 5){
System.out.println("周五"); //虽符合条件,但前一条语句执行后流程自动结束
}else if (w == 6){
System.out.println("星期六");
}else if (w == 7){
System.out.println("星期天");
}
}
}
注:当在同一个if else语句中,满足一个条件后,流程自动结束
Switch语句
switch (表达式){
case 1:代码块;[break];
case 2:代码块;[break]
case 3:代码块;[break];
case n:代码块;[break];
default:默认代码块;
}
多支选择语句 进行多路选择
表达式值类型:byte short int char 枚举 String(字符串)
值1,2,3...n必须是字面量(常量)且不能重复
public class SwitchDome {
public static void main(String[] args) {
int w = 5;
switch (w){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五"); //没有break结束,故向下继续执行至遇到break
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期天");
break;
default:
System.out.println("日期失效");
}
}
}
若匹配的case语句块执行完成后 如果没有break结束 则继续向下
执行下一个case 直到遇到break
若后面没有break 则持续输出到default
若没有default 则直接跳出Switch语句
if Switch异同点
同:多路选择(Switch可以实现的 if都可以实现)
异:if条件更灵活; Switch适用于一些固定选择 在固定选项执行中效率更高
2. 循环语句满足某种条件 一直重复做某件事情
循环语句格式:循环变量初始化 进入循环的条件 循环体(循环中要执行的代码) 循环变量的更新
while循坏
while(进入循环的条件){
循环体
}
int a = 1; //变量初始化
while(a<=5){ //进入循坏的条件
System.out.println(a); //循坏体
a++; //若没有a++ 则进入死循坏
}
先条件判断 不成立则无法进入循坏中
do while循坏
do{ } while( );
先执行后判断 即使条件不成立 也至少执行一次
int a = 0;
do{
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数值");
a = scanner.nextInt();
}while(a<60);
for循环
for(变量初始化;条件;更新变量){
循坏体
}
break和continuebreak:终止循坏 强制退出循坏
continue:跳过某一次循坏
循坏嵌套for,while,do while都可以作为循坏嵌套中的内部循坏和外部循坏
break只能终止当前所在层的循坏
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("*");
}
System.out.println( );
}
在内层循坏中要终止外层循坏方法:为循坏定义一个标签,例如定义outer:for...(break outer)
outer:for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if(i == 2){
break outer;
}
System.out.print("*");
}
System.out.println( );
}
方法
类似于C语言中的函数,解决某一个问题而编写的代码组合
main方法:java程序的入口 启动java程序的主线程 (属于类或对象)
格式:[访问权限修饰符] [修饰符] [返回值类型]
例如:public static void(void表示没有返回值)
public class Demo2 {
public static void multi() { //static修饰的方法属于类 可以直接通过类名调用
for (int i = 1; i <=9 ; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+(i*j)+" ");
}
System.out.println();
}
}
public void print(int m){ //不加static修饰的方法属于对象 需要通过对象调用
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
System.out.print("*");
}
System.out.println( );
}
}
}
public class Demo02 {
public static void main(String[] args) {
Demo2.multi();
Demo2 m = new Demo2();
m.print(5);
}
}
形参:数据结构 参数名(参数名可以自定义)
return
①return后面有值 表示方法有返回值 必须声明返回值类型
public class Demo2 {
public int compute(int a,int b){ //return有返回值
int c = a+b;
return c;
}
}
Demo2 m = new Demo2(); int c = m.compute(10,5); //return有返回值 必须声明返回值 System.out.println(c);
②return后面没有值时 只表示方法终止 不表示方法有返回值



