一 java的基本类型
/**
* 一、常量
*
* 整型常量
* 十进制(12) ,十六进制 (0x12),八进制(012)
*
* 长整型常量
* 13L
*
* 单精度浮点数
* 5.1f,.4f ,2e3f ,0f
*
* 双精度浮点数
* 5.1,.4,2e-3,0d
*
* 布尔常量
* true和false
*
* 字符常量
* ‘a’ , ‘8’, '/u0027'
* ‘/r‘表示接受键盘输入,此时光标移动到该行的行首
* ‘/n‘是换行;
* ‘/t‘是制表符,相当于table键;
* ‘/b‘是退格键,相当于Back Space;
* ‘/‘‘是单引号,而‘/“‘是双引号;
* ‘//‘是一个斜杠“/”。
* 说明:在Windows系统中的回车键用'/r'和'/n'表示
* 在Linux系统中,回车键用'/n'表示.
*
* 字符串常量
* “Hello World“,”123”, "Welcome /nXXX"
*
* null常量
* null常量只有一个值,用null表示,表示对象的引用为空
*/
/**
* 二、变量
* 变量的类型
* 数据类型:
* 基本类型:
* 数值型:
* 整数类型:byte short int long
* 浮点数类型:float double
* 字符型:char
* 布尔型:boolean
* 引用数据类型:
* 字符串: String
* 类: class
* 接口: interface
* 数组: array[]
* 基本类型的变量:
* byte占用一个字节,数字大小为-27—27-1
* short占用两个字节,数字大小为-215—215-1
* int占用四个字节,数字大小为-231—231-1
* long占用八个字节,数字大小为-263—263-1
* float占用四个字节,数字大小为1.4E-45~3.4E+38 , -1.4E-45~-3.4E+38 。用二进制的指数形式表示一个浮点数的格式,如:101*22 , 101*2-3
* double占用八个字节,数字大小为4.9E-324~1.7E+308, -4.9E-324~-1.7E+308 。
* char占两个字节,数字大小为0—216-1,是unicode编码。字符的本来面目,我们为什么可以直接将一个数字赋给字符变量。
* Boolean占一个字节,其取值只有两个,true和false。
* 注意: 变量在方法中必须在定义时初始化,但是在类中定义变量会自动初始化
* 说明: 1、整数类型的变量byte、short和char在参加运算时将自动提升为int类型.要声明一个变量是长整型long,必须在后面加上"L"或"l".
* 2、浮点类型的变量float在声明时要在后面加上"F"或"f",对于一个浮点数,如果后面什么都不写,Java会自动将它视为double类型.
* 3、可以将一个字符或者数字直接赋值给一个char类型变量,打印时,Java会根据该数字对应的Unicode编码打印出对应的字符.
* 4、可以对char类型的变量与数字进行基本运算.
*/
/**
* 三、类型转化
* 自动类型转换(也叫隐式类型转换)
* 强制类型转换(也叫显式类型转换
* 表达式的数据类型自动提升
* 所有的byte型、short型和char的值将被提升到int型。
* 如果一个操作数是long型,计算结果就是long型;
* 如果一个操作数是float型,计算结果就是float型;
* 如果一个操作数是double型,计算结果就是double型。
*/
/**
* 四、变量的生命周期与作用域
* 变量要先定义,后使用,但也不是在变量定义后的语句一直都能使用前面定义的变量。
* 我们可以用大括号将多个语句包起来形成一个复合语句,变量只能在定义它的复合语句中使用。
*/
public class Basic
{
char c;
byte b;
short s;
int i;
long l;
float f;
double d;
/**
* 基本类型变量定义在类中,可以不为其赋初值,
* 程序在运行时将自动为这些变量初始化,
* 初始化的数值就是在main中定义的这些变量时所赋的值.
*/
public static void main(String[] args)
{
/*第一段内容(基本类型初始化)*/
char c = '/u0000';
byte b = 0;
short s = 0;
int i = 0;
long l = 0;//long l = 0L; 或者long l = 0l;
float f = 0.0f;
//如果一个浮点数后面没有加f或者F,
//这个浮点数将自动定义成一个double类型的,
//相反,若将这个后面没有f或F的浮点数定义成一个float类型,
//编译器将会报错!
//float f = 0.0 错误!
double d = 0.0;//double d = 0.0D; 或者 double d = 0.0d;
System.out.println("第一段内容(基本类型初始化)");
Basic base = new Basic();
System.out.println(
base.c + " " +
base.b + " " +
base.s + " " +
base.i + " " +
base.l + " " +
base.f + " " +
base.d
);
System.out.println(
c + " " +
b + " " +
s + " " +
i + " " +
l + " " +
f + " " +
d
);
System.out.println();
/*第二段内容(char的特殊性)*/
System.out.println("第二段内容(char的特殊性)");
char ch1;
ch1 = 'c';//为char赋予一个字符型常量
System.out.println(ch1);
ch1 += 1;//可以对char类型的变量进行基本运算
System.out.println(ch1);
char ch2 = 97;//为char变量附一个整数类型的值
System.out.println(ch2);
ch2 += 1;
System.out.println(ch2);
System.out.println();
/*第三段内容(隐式类型转化)*/
System.out.println("第三段内容(隐式类型转化)");
byte bt1 = 1;
byte bt2 = 2;
//byte bt3 = bt1 + bt1;错误两个byte类型的变量进行运算将自动提升为int型.
byte bt3 = (byte)(bt1 + bt1);//强制类型转换成byte
System.out.println(bt3);
short st1 = 3;
short st2 = 4;
//short st3 = st1 + st2;错误两个short类型的变量进行运算将自动提升为int型.
short st3 = (short)(st1 + st2);//强制类型转换成short
System.out.println(st3);
char cr1 = 'a';
char cr2 = 1;
//char cr3 = cr1 + cr2;错误两个char类型的变量进行运算将自动提升为int型.
char cr3 = (char)(cr1 + cr2);//强制类型转换成char
System.out.println(cr3);
int it;
it = bt1 + bt2;
System.out.println(it);
it = st1 + st2;
System.out.println(it);
it = cr1 + cr2;
System.out.println(it);
//两个long、float或者double类型的变量进行运算,结果还是long、float或者double类型
long lg1 = 1;
long lg2 = 2l;
long lg3 = lg1 + lg2;
System.out.println(lg3);
float fl1 = 0.1f;
float fl2 = 0.2f;
float fl3 = fl1 + fl2;
System.out.println(fl3);
double dl1 = 1.1;
double dl2 = 1.2d;
double dl3 = dl1 + dl2;
System.out.println(dl3);
double dbl = 1.0 +2.0;
System.out.println();
/*第四段内容(显示转换的数据丢失)*/
System.out.println("第四段内容(显示转换的数据丢失)");
Integer inter = new Integer(0);
int inters = inter.MAX_VALUE;//将int类型的最大值赋值给inters,inters = 0xffffffff
byte bytes = (byte)inters;//byte截取inters的最后一个字节 bytes = 0xff(即-1)
System.out.println(bytes);
short shorts = (short)inters;//short截取inters的最后两个字节 shorts = 0xffff(即-1)
System.out.println(shorts);
char chars = (char)inters;//chars截取inters的最后两个字节 chars = 0xffff(即代表Unicode的'?');
System.out.println(chars);
//将long类型的最大值强制赋值给int类型
System.out.println(
Integer.toBinaryString((int)Long.MAX_VALUE)
+ "/n" +
Integer.toHexString((int)Long.MAX_VALUE)
);
System.out.println();
/*第五段内容(变量的生命周期)*/
System.out.println("第五段内容(变量的生命周期)");
{//外层括号
int outer = 10;
int same = 20;
{//内层括号
int inner = 30;
//int same = 40;错误,
//Java中不允许像C/C++那样重复定义与全局变量同名的局部变量,用于覆盖全局变量.
System.out.println(
"outer = " + outer
+ " inner = " + inner
);
}//在括号外inner将结束其生命周期.
System.out.println(
"outer = " + outer
/*+ *" inner = " + inner*/
//局部变量inner在括号外将结束它的生命周期,所以不能被访问.
);
}//在最外层的括号外outer和inner均结束其生命周期.
System.out.println(
/*"outer = " + outer*/
//outer在括号外将结束它的生命周期,所以不能被访问.
);
}
}
/* ~~Console out~~
第一段内容(基本类型初始化)
0 0 0 0 0.0 0.0
0 0 0 0 0.0 0.0
第二段内容(char的特殊性)
c
d
a
b
第三段内容(隐式类型转化)
2
7
b
3
7
98
3
0.3
2.3
第四段内容(显示转换的数据丢失)
-1
-1
?
11111111111111111111111111111111
ffffffff
第五段内容(变量的生命周期)
outer = 10 inner = 30
outer = 10
*/