Java 变量介绍

变量的定义 

变量是指内存里的一段区域

变量使用时的注意

Java里变量必须先声明,后使用

使用变量名来访问内存中的数据

变量的作用域,其定义所在的一对{}里

变量只有在作用域才生效

同一个作用域不能定义重名的变量

class VariableTest1
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");   单行注释    		
                //声明变量类型并赋值
                int  myAge = 12; 
                //单行打印变量值
		System.out.println(myAge);
	}
}
或者
class VariableTest1
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		int myAge = 12;
		System.out.println(myAge);
		//声明变量类型
		int chenXi;
		//赋值变量
		chenxi = 13;
		//单行打印变量
		System.out.println(chenXi);
	}
}

变量的类型:基本数据类型、引用数据类型

基本数据类型:数值型(整数型(byte、short、int、long)、浮点数型(flote、double))、字符型(char)、布尔型(boolean)

引用数据类型: 类(class)字符串也属于这个类型、接口(interface)、数组([])

基本数据类型的使用

整数类型:Java的整数有固定表述范围和长度,不收具体os的影响,保证Java的可移植性;整数类型通常默认是int型,声明long型变量值后面通常加‘l'或者‘L’,Java声明整数数据类型时通常默认int类型,除非明显不足以保证较大的数时使用long

类型 占用存储空间 表示范围
byte 1字节=8byte -128~127
short 2字节 -2的15次方~2的15方-1
int 4字节 -2的31次方~2的31次方
long 8字节 -2的63次方~2的63次方
class VariableTest1
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		int myAge = 12;
		System.out.println(myAge);
		//声明变量类型
		int chenXi;
		//赋值变量
		chenXi = 13;
		//单行打印变量
		System.out.println(chenXi);
		byte b1 = 12;
		byte b2 = -128;
//     编译出错
		b2 = 128;
		System.out.println(b1);
		System.out.println(b2);
	}
}
---------- javac ----------
VariableTest1.java:24: 错误: 不兼容的类型: 从int转换到byte可能会有损失
		b2 = 128;
		     ^
1 个错误

输出完成 (耗时 0 秒) - 正常终止

  测试其它类型的

class VariableTest1
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		int myAge = 12;
		System.out.println(myAge);
		//声明变量类型
		int chenXi;
		//赋值变量
		chenXi = 13;
		//单行打印变量
		System.out.println(chenXi);
		byte b1 = 12;
		byte b2 = -128;
		//b2 = 128;
		System.out.println(b1);
		System.out.println(b2);
		short s1 = 128;
		int i1 = 12345;
		long l1 = 54321987698765421L;
		System.out.println(l1);
		System.out.println(i1);
		System.out.println(s1);
	}
}


---------- java ----------
12
13
12
-128
54321987698765421
12345
128

输出完成 (耗时 0 秒) - 正常终止

  浮点型常量:与整数类型类似Java浮点型数也有固定的数表示范围和字段长度,不受系统影响;

浮点型常量有两种表示是方式:1进制数型如3.14;2科学计数法形式如5.21E

float:单精度尾数可以精确到7位有效数字,很多情况精度不够,难以满足需求

double: 双精度,是单精度的2倍通常采用双精度

java:浮点型常量默认double型,声明float型常量,和面必须加f或F;注意E10倍

类型 占用空间 表示范围
单精度 4字节 -3.403E38~3.403E38
双精度 8字节 -1.798E308~1.798E308

 测试

class VariableTest1
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		int myAge = 12;
		System.out.println(myAge);
		//声明变量类型
		int chenXi;
		//赋值变量
		chenXi = 13;
		//单行打印变量
		System.out.println(chenXi);
		byte b1 = 12;
		byte b2 = -128;
		//b2 = 128;
		System.out.println(b1);
		System.out.println(b2);
		short s1 = 128;
		int i1 = 12345;
		long l1 = 54321987698765421L;
		System.out.println(l1);
		System.out.println(i1);
		System.out.println(s1);、
//单精度加f或F
		float f1 = 12.3f;
		double d1 = 123.3;
		System.out.println(f1+2);
		System.out.println(d1+15);
	}
}
---------- java ----------
12
13
12
-128
54321987698765421
12345
128
14.3
138.3

输出完成 (耗时 0 秒) - 正常终止

  字符类型char两个字节声明格式通常‘’单引号字符内部只能一个字符

代码

class VariableTest1
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		int myAge = 12;
		System.out.println(myAge);
		//声明变量类型
		int chenXi;
		//赋值变量
		chenXi = 13;
		//单行打印变量
		System.out.println(chenXi);
		byte b1 = 12;
		byte b2 = -128;
		//b2 = 128;
		System.out.println(b1);
		System.out.println(b2);
                // 变量声明及定义
		char c1 = 'x';
		System.out.println(c1);
	}
}
---------- java ----------
12
13
12
-128
x

输出完成 (耗时 0 秒) - 正常终止

  布尔型变量类型

class VariableTest1
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		int myAge = 12;
		System.out.println(myAge);
		//声明变量类型
		int chenXi;
		//赋值变量
		chenXi = 13;
		//单行打印变量
		System.out.println(chenXi);
		byte b1 = 12;
		byte b2 = -128;
		//b2 = 128;
		System.out.println(b1);
		System.out.println(b2);
		char c1 = 'x';
		System.out.println(c1);
		//布尔型只有两个值true;false
		boolean B = true;
		boolean b = false;
		System.out.println(B);
		System.out.println(b);
	}
}
---------- java ----------
12
13
12
-128
x
true
false

输出完成 (耗时 0 秒) - 正常终止

  

原文地址:https://www.cnblogs.com/rdchenxi/p/13181691.html