编译+解释
增加运行速度 Java解释器一边解释一边执行
JVM———Java虚拟机
开发环境
工具
- Eclipse
- IDEA
JAVA的文件名字必须和类的名字一样 Java主要用类编程
数值类型
public class Main {
public static void main(String[] args) {
// 基本的数据类型
byte b = 100;
int i = 300;
short s = 200;
long l = 12445;
// 只能存放一个字符 并且用单引号表示
char c = '中';
// 默认的小数值是double类型的 如果使用float类型需要加f
double d = 12.321;
float f = 12.321f;
boolean t = true;
boolean F = false;
// 字符串类型 区别于c语言
String str = "hello world";
}
}
基本类型转换
需要满足一定的规矩
- 精度高的数据类型像容量大的杯子,可以放更大的数据
- 精度低的数据类型就像容量小的杯子,只能放更小的数据
- 小杯子往大杯子里倒东西,大杯子怎么都放得下
- 大杯子往小杯子里倒东西,有的时候放的下,有的时候就会有溢出
- short 和 char都是16位的 但是仍然需要强制类型转换
命名规则
变量命名只可以用字母 数字 $_
JAVA作用域
声明在类下面的变量叫做字段后者属性,成员变量,Field 在方法内的变量 叫做局部变量 在子块里面的变量外部无法访问
public void method1() {
int i = 5; //其作用范围是从声明的第4行,到其所处于的块结束12行位置
System.out.println(i);
{ //子块
System.out.println(i); //可以访问i
int j = 6;
System.out.println(j); //可以访问j
}
System.out.println(j); //不能访问j,因为其作用域到第10行就结束了
}
}
Final修饰变量
只有一次赋值的机会
三元操作符
表达式?数值1:数值2; 表达式成立 返回数值1 不成立 返回数值2
Scanner 输入
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//// 读取第一个整数 nextInt()是根据顺序读取的
// int a = sc.nextInt();
//// 读取第二个整数
// int b = sc.nextInt();
// System.out.println("the first number is "+a);
// System.out.println("the second number is " + b);
// 读取float类型
// float f = sc.nextFloat();
// System.out.println("the float number is "+f);
// String a = sc.nextLine();
// System.out.println("the inputed String:" + a);
// 如果读完第一个整数之后 再想读取字符串 先读取的是"\r\n"
int i = sc.nextInt();
System.out.println("the inputed number "+i);
String rn = sc.nextLine();
String a = sc.nextLine();
// System.out.println(rn);
System.out.println(a);
}
}