HelloWorld例子
// 第一行的第三个单词必须和所在的文件名称完全一样,大小写也要一样
// public class后面代表定义一个类的名称
public class HelloWorld {
// 第二行内容是固定不变的,代表main方法
// 这一行代表程序执行的起点
public static void main(String[] args) {
// 第三行代表打印输出语句
System.out.println("hello world");
}
}
JDK安装
解释型语言:要经过编译和解释才可运行
编译型语言:一次编译即可运行
java是解释型语言
系统变量(java5.0以上这样配置)
JAVA_HOME D:\Java\jdk-9.0.4(bin的上一级目录)
path %JAVA_HOME%\bin
标识符与关键字
标识符命名规则:大小写字母,数字,$(美元符), _(下划线),数字不能开头,不能是关键字 “见名知意”
关键字:特殊意义字符
关键字特点:
1、完全小写的字母
2、在特殊IED中有特殊颜色
类名,接口:大驼峰
变量名:小驼峰
方法名:小驼峰
常量 单个单词全部大写,多个单词,每个单词全部大写,并用_连接
JAVA常量
在程序执行的过程中,其值不发生改变的量
字面值常量
字符串常量: "hello" //双引号括起来的
字符常量:'a' // 两个单引号中间必须有且只有一个字符,不能没有否则报错,有多个也会报错
浮点数常量 3.14
整数常量 10
布尔常量 true/false
空常量: null 代表没有任何数据 // 不能直接用来打印输出
自定义常量
final int x = 10;
# 被final修饰的变量只能赋值一次,一旦赋值不能更改。
final作用:
修改类:该类不能继承
修改方法:该方法不能重写
修改变量:变量只能赋值一次,一旦赋值不能更改
数据类型分类
基本数据类型:(4类8种)
整数型 byte(1字节) short(2字节) int(4字节 默认) long(8字节 后加大写L)
浮点型 float(4字节 后加大写F) double(8字节 默认) 3.14 3.14E2
字符型 char(2字节) (单引号括起来的单个字符,采用Unicode编码,每个字符占两个字节)
布尔型 boolean 只能取true/false 逻辑运算,程序流程控制
引用数据类型:
字符串
数组
类
接口
注意事项:
1.字符串不是基本类型,而是引用类型
2.浮点型可能是个近似值,并非精确的值
3.数据范围与字节数不一定相关,float数据范围比long更加广泛,但是float是4字节,long是8字节
4.浮点数默认是double,使用float,后缀加F 100F。整数默认是int,使用long,后缀加L 100L
5.基本数据类型一块内存空间,引用数据类型两块内存空间
整数类型 | 占用存储空间 | 表数范围 |
---|---|---|
byte | 1字节 | -128 ~ 127 |
short | 2字节 | -2 ^15 ~ 2 ^15 - 1 |
int | 4字节 | -2 ^31 ~ 2 ^31- 1 |
long | 8字节 | -2 ^63 ~ 2 ^63- 1 |
基本数据类型转换
1.boolean类型不可以转换为其他数据类型
2.整形,字符型,浮点型转换原则
(1)容量小的类型自动转换为容量大的数据类型,数据类型容量大小排序:byte,short,char>int>long>float>double
(2)byte,short,char之间不会相互转换,三者计算时首先转换为int类型,char对应的是ASCII码
(3)容量大的类型转换为容量小的数据类型,要加上强制转换符,但可能造成精度降低或溢出
(4)多种类型的数据混合运算时,系统首先自动将所有的数据转换为容量最大的类型再进行计算
(5)浮点数默认是double,使用float,后缀加F 100F。整数默认是int,使用long,后缀加L 100L
public class DataTypeConversion {
public static void main(String[] args) {
int i1 = 123;
int i2 = 456;
double d1 = (i1+i2) * 3.14; // int自动转换为double进行运算
System.out.println(d1);
byte b1 = 88;
byte b2 = 99;
byte b3 = (byte) (b1+b2); // byte会先转换为int再计算,强制转换,注意byte的表示范围-128-127
System.out.println(b3); // -69 溢出,int转byte会切除溢出部分
double d2 = 1e200;
float f2 = (float) d2;
System.out.println(f2); // Infinity 产生溢出
float f3 = 3.14F;
long l1 = 1234;
long l2 = 666666666L; // 必须加L
float f = l1+l2+f3; // 系统会自动转为容量大的数据类型float再计算
System.out.println(f); // 6.666679E8
long l = (long) f; // 强制转换会舍去小数部分(不是四舍五入)
System.out.println(l); // 666667904
}
}
运算符
短路与&& 左边是false,右边不执行 短路逻辑
短路或|| 左边为true,右边不执行 短路逻辑
自增++,自减-- 参与运算时 i++ 先取值再运算 ++i 先运算再取值
“+” 字符串拼接
int c = 10;
System.out.println("c=" + c); // c = 10
System.out.println('a'); //输出字符a
System.out.println("hello"+'a'+1); //helloa1 字符串拼接
System.out.println('a'+1+"hello"); //98hello 字符和整数先运算再拼接
System.out.println("5+5="+5+5); //5+5=55
System.out.println(5+5+"=5+5"); //10=5+5
# 变量相加,会首先看类型问题,最终把结果赋值的也会考虑类型问题。
# 常量相加,首先做加法,然后看结果是否在赋值的数据类型范围内,如果不在,才报错
byte b1 = 3,b2 = 4,b; //b1的值是3,b2的值是4,b没有值
b = b1 + b2; //这个是类型提升,有问题
b = 3 + 4; //常量,先把结果计算出来,然后看是否在byte的范围内,如果在就不报错
键盘录入
import java.util.Scanner;
public class DemoScanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数据:");
int x = sc.nextInt(); // 录入整数
System.out.println("请输入第二个数据:");
int y = sc.nextInt();
int sum = (x + y);
System.out.println("sum:"+sum);
}
}
三目条件运算符
三目运算符:
格式:比较表达式?表达式1:表达式2;
比较表达式:结果是一个boolean类型。
执行流程:
根据比较表达式的计算返回一个true或者false。
如果是true,就把表达式1作为结果。
如果是false,就把表达式2作为结果。
int x = -100;
int flag = x > 0 ? 1 : (x == 0 ? 0 : -1);
if语句
public class DemoIf {
public static void main(String[] args) {
int x = 90;
if (x >= 90 && x <= 100) {
System.out.println("优");
}else if (x >=80 && x <= 89) {
System.out.println("良");
}else if (x >= 60 && x < 80) {
System.out.println("中");
}else if (x >= 0 && x <= 59) {
System.out.println("差");
}else {
System.out.println("数据错误");
}
}
}
for语句
// 水仙花数
public class DemoFor {
public static void main(String[] args) {
for(int i = 1; i <= 999; i++ ) {
int a = i % 10; // 各位
int b = i / 10 % 10; // 十位
int c = i / 10 / 10 % 10; // 百位
if(a * a * a + b * b * b + c * c *c == i) {
System.out.println(i);
}
}
}
}
//for语句的无限循环
for (; ; ) {
System.out.println("hello world");
}
while & do while 语句
public class DemoWhileDoWhile {
public static void main(String[] args) {
int i= 0;
while(i < 10) { // while先判断再执行
i++;
System.out.println(i); // 1-10
}
i = 0;
do { // while先执行再判断
i++;
System.out.println(i);
}
while(i < 10); // 注意分号 1-10
}
}
// for语句执行后变量会被释放,不能再使用
//while语句执行后,初始化变量还可以继续使用
switch语句
public class DemoSwitch {
public static void main(String[] args) {
int i = 5;
switch(i) {
case 0:
System.out.println("星期天");
break;
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;
case 6:
System.out.println("星期六");
break;
default :
System.out.println("数据错误");
}
}
}
# 不写break可能发生case穿透
# 多个case可以合并一起,表示或 case 1 case 2 case 3:
# switch(表达式)可以是基本数据类型可以接收byte,short,char,int(表达式),引用数据类型可以接收枚举(JDK1.5)String字符串(JDK1.7) 【先计算表达式的值,再和case值匹配,无匹配执行default】
// break 只能在switch和循环中
// continue 只能在循环中
Shylin