变量
变量的命名规则
- 变量名只包含数字、字母、下划线
- 数字不能开头
- 变量命名可以使用 小驼峰命名法 当一个变量名由多个单词构成时,除第一个单词外,其他单词首字母都大写
变量的作用域
该变量能生效的范围,一般是变量定义所在的代码块(大括号)
class Test {
public static void main(String[] args) {
{
int x = 10;
System.out.println(x); // 编译通过;
}
System.out.println(x); // 编译失败, 找不到变量 x.
}
}
数据类型
简单介绍下
byte
public static void main7(String[] args) {
byte b = 12;
byte c = 21;
System.out.println(b+" " +c);
System.out.println(Byte.MAX_VALUE);
System.out.println(Byte.MIN_VALUE);
System.out.println(Byte.MAX_VALUE+1);
//打印 默认是以整型进行打印的
}
char
public static void main8(String[] args) {
char ch = 'a';
System.out.println(ch);
char ch2 = '高';
System.out.println(ch2);
char ch3 = 97;
System.out.println(ch3);
}
short
public static void main6(String[] args) {
short sh = 12;
System.out.println(Short.MAX_VALUE);
System.out.println(Short.MIN_VALUE);
}
long 和 Int
public static void main(String[] args) {
long a = 10L;
System.out.println("a的值是: " + a);
System.out.println("最大值: " + Long.MAX_VALUE);//拼接
System.out.println("最小值: " + Long.MIN_VALUE);
int b = 10;
System.out.println("b的值是: " + b);
System.out.println("最大值: " + Integer.MAX_VALUE);//拼接
System.out.println("最小值: " + Integer.MIN_VALUE);
}
double 和 float
public static void main9(String[] args) {
float f = 12.3f;
System.out.println(f);
double d = 12.5;//12.5默认double类型数据
System.out.println(d);
System.out.println(Double.MAX_VALUE);
System.out.println(Double.MIN_VALUE);
}
boolean 布尔
public static void main5(String[] args) {
boolean flg = true;
System.out.println(flg);
}