Java中变量和8种基本数据类型

变量

变量的命名规则

  1. 变量名只包含数字、字母、下划线
  2. 数字不能开头
  3. 变量命名可以使用 小驼峰命名法  当一个变量名由多个单词构成时,除第一个单词外,其他单词首字母都大写

 变量的作用域

该变量能生效的范围,一般是变量定义所在的代码块(大括号)

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);

	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值