程序是一套数字处理的游戏框架,也就是说在整个程序开发的过程之中,所达到的最终目的就是对一些数据的处理过程,那么既然要进行数据的处理,就一定需要提供有各种类型的数据定义。
Java数据分类
在Java语言之中对于数据类型一共分为两类:
基本数据类型:描述的是一些具体的数字单元,例如:1、1.1;
数值型:
整形:byte、short、int、long; → 默认值:0
浮点型:float、double; → 默认值:0.0
布尔型:boolean; → 默认值:false
字符型:char; → 默认值:’\u0000’
引用数据类型:牵扯到内存关系的使用;
数组、类、接口。 → 默认值:null
而本次讨论的主要是基本数据类型,这里面不牵扯到复杂的内存关系的匹配操作。每一种基本数据类型都有每一种类型的数据范围。
不同的类型保存有不同范围的数据,但是这里面实际上就牵扯到了数据类型的选择上,那么对于以上的数据类型划分,可以给出个人的一些使用参考原则:
如果要是描述数字首选的一定是int(整数)、double(小数);
如果要进行数据传输或者是进行文字编码转换使用byte类型(二进制处理操作);
处理中文的时候最方便的操作使用的是字符char来完成(可选概念);
描述内存或文件大小、描述表的主键列(自动增长)可以使用long;
课程代码:
1、
public class JavaDemo {
public static void main(String args[]) {
// int 变量名称 = 常量(10是一个常量,整数类型为int) ;
int x = 10 ; // 定义了一个整型变量x
// int型变量 * int型变量 = int型数据
System.out.println(x * x) ;
}
}
2、
public class JavaDemo {
public static void main(String args[]) {
// int 变量名称 = 常量(10是一个常量,整数类型为int) ;
int x = 10 ; // 定义了一个整型变量x
x = 20 ; // 改变了x的已有内容
// int型变量 * int型变量 = int型数据
System.out.println(x * x) ;
}
}
3、
public class JavaDemo {
public static void main(String args[]) {
int max = Integer.MAX_VALUE ; // 获取int的最大值
int min = Integer.MIN_VALUE ; // 获取int的最小值
System.out.println(max) ; // 2147483647
System.out.println(min) ; // -2147483648
System.out.println("------------- 无以言表的分割线 -----------") ;
// int型变量 + int型常量 = int型计算结果
System.out.println(max + 1) ; // -2147483648,最大值 + 1 = 最小值
System.out.println(max + 2) ; // -2147483647,最大值 + 1 = 次最小值
// int型变量 - int型常量 = int型计算结果
System.out.println(min - 1) ; // 2147483647,最小值 - 1 = 最大值
}
}
4、
public class JavaDemo {
public static void main(String args[]) {
// long long变量 = int的数值
long max = Integer.MAX_VALUE ; // 获取int的最大值
long min = Integer.MIN_VALUE ; // 获取int的最小值
System.out.println(max) ; // 2147483647
System.out.println(min) ; // -2147483648
System.out.println("------------- 无以言表的分割线 -----------") ;
// long型变量 + int型常量 = long型计算结果
System.out.println(max + 1) ; // 2147483648
System.out.println(max + 2) ; // 2147483649
// long型变量 - int型常量 = long型计算结果
System.out.println(min - 1) ; // -2147483649
}
}
5、
public class JavaDemo {
public static void main(String args[]) {
int max = Integer.MAX_VALUE ; // 获取int的最大值
int min = Integer.MIN_VALUE ; // 获取int的最小值
System.out.println(max) ; // 2147483647
System.out.println(min) ; // -2147483648
System.out.println("------------- 无以言表的分割线 -----------") ;
// int型变量 + long型常量 = long型计算结果
System.out.println(max + 1L) ; // 2147483648
System.out.println(max + 2l) ; // 2147483649
// long型变量 - int型常量 = long型计算结果
System.out.println((long)min - 1) ; // -2147483649
}
}
6、
public class JavaDemo {
public static void main(String args[]) {
long num = 2147483649L ; // 此数据已经超过了int范围
int temp = (int) num ; // long范围比int范围大,不能够直接转换
System.out.println(temp) ;
}
}
7、
public class JavaDemo {
public static void main(String args[]) {
byte num = 20 ;
System.out.println(num) ;
}
}
8、
public class JavaDemo {
public static void main(String args[]) {
byte num = (byte) 200 ;
System.out.println(num) ;
}
}
9、
public class JavaDemo {
public static void main(String args[]) {
int x = 200 ;
byte num = (int) x ;
System.out.println(num) ;
}
}
10、
public class JavaDemo {
public static void main(String args[]) {
// 10.2是一个小数其对应的类型为double
double x = 10.2 ;
int y = 10 ;
// double类型 * int类型 = double类型
double result = x * y ;
System.out.println(result) ;
}
}
11、
public class JavaDemo {
public static void main(String args[]) {
float x = (float) 10.2 ;
float y = 10.1F ;
System.out.println(x * y) ; // float型
}
}
12、
public class JavaDemo {
public static void main(String args[]) {
char c = 'B' ; // 一个字符变量
System.out.println(c) ;
}
}
13、
public class JavaDemo {
public static void main(String args[]) {
char c = 'A' ; // 一个字符变量
int num = c ; // 可以获得字符的编码
System.out.println(num) ;
}
}
14、
public class JavaDemo {
public static void main(String args[]) {
char c = '仁' ; // 一个字符变量
int num = c ; // 可以获得字符的编码
System.out.println(num) ;
}
}
15、
public class JavaDemo {
public static void main(String args[]) {
boolean flag = true ;
if (flag) { // 判断flag的内容,如果是true就执行
System.out.println("我很帅,我很年轻!") ;
}
}
}
16、
public class JavaDemo {
public static void main(String args[]) {
String str = "Hello World !" ; // 使用“"”进行描述
System.out.println(str) ;
}
}
17、
public class JavaDemo {
public static void main(String args[]) {
String str = "Hello" ; // 使用“"”进行描述
str = str + " World" ; // 字符串连接
str += " !!!" ; // 字符串连接
System.out.println(str) ;
}
}
18、
public class JavaDemo {
public static void main(String args[]) {
double x = 10.1 ;
int y = 20 ;
String str = "计算结果:" + (x + y) ;
System.out.println(str) ;
}
}
19、