java数据类型划分
分为两大类型:
1)基本数据类型:类似于普通的值。
2)引用数据类型:传递的是内存的地址。
浮点类型实际上就是表示小数。
java基本数据类型
数据的溢出
当整数的数据大小超出了可以表示的范围,而程序中又没有做数值范围的检查时,这个整型变量所输出的值将发生絮乱,且不是预期的运行结果。
例如:求出整型的最大值
- public class T {
- public static void main(String[] args) {
- int max = Integer.MAX_VALUE;
- System.out.println("整型的最大值为:"+max); //整型的最大值为:2147483647
- }
- }
现在对求的最大值进行加法操作
- public class T {
- public static void main(String[] args) {
- int max = Integer.MAX_VALUE;
- System.out.println("整型的最大值为:"+max); //整型的最大值为:2147483647
- System.out.println("整型的最大值+1: "+(max+1)); //整型的最大值+1: -2147483648
- System.out.println("整型的最大值+2: "+(max+2)); //整型的最大值+2: -2147483647
- }
- }
如果现在要想避免数据的溢出,可以采用扩大数据类型的方式。int-->long
- public class T {
- public static void main(String[] args) {
- int max = Integer.MAX_VALUE;
- System.out.println("整型的最大值为:"+max); //整型的最大值为:2147483647
- System.out.println("整型的最大值+1: "+(max+1)); //整型的最大值+1: -2147483648
- System.out.println("整型的最大值+2: "+(max+2)); //整型的最大值+2: -2147483647
- System.out.println("整型的最大值+2: "+((long)max+2)); //2147483649
- }
- }
字符类型
字符类型在内存中占有2个字节,可以用来保存英文字母等字符。计算机处理字符类型时,是把这些字符当成不同的整数来看待,
因此,严格说来,字符类型也算是整数类型的一种。
- public class T {
- public static void main(String[] args) {
- char ch1 = 'a'; //字符是使用''括起来的数据
- char ch2 = 97; //通过数字定义字符变量
- System.out.println("ch1 = "+ch1);
- System.out.println("ch2 = "+ch2);
- }
- }
常用的转义字符
浮点数类型与双精度浮点数类型
在日常生活中经常会使用到小数类型的数值,如身高,体重等需要精确的数值时,整数就不能满足程序设计者的要求了。在数学中,这些带有小数点的数值
称为实数,在java中,这种数据类型称为浮点数类型(floag),其长度为32个字节,有效范围为-3.4E1038到3.4E1038。当浮点数的表示范围不够大的时候
还有一种双精度(double)浮点数可供使用。双精度浮点数类型的长度为64个字节,有效范围为-1.7E10308到1.7E10308
在java 中一个数字或者一个小数实际上也都是存在默认类型的:
小数(1.1,1.2)的默认类型是double类型
整数(1,2,3)的默认类型是int类型
布尔类型
布尔(boolean)类型的变量,只有 true(真)和false(假)两种
基本数据类型的默认值
赋值运算符号
一元运算符
算术运算符
关系运算符
递增与递减运算符
- public class T {
- public static void main(String[] args) {
- int a = 3 , b = 3 ; // 定义两个变量a和b
- int x = 6, y = 6 ; // 定义两个变量x和y
- System.out.println("a = " + a) ;
- System.out.println("\t a++ = " + (a++) + " , a = " + a) ; // 先计算后自增
- System.out.println("b = " + b) ;
- System.out.println("\t ++b = " + (++b) + " , b = " + b) ; // 先自增后计算
- System.out.println("x = " + x) ;
- System.out.println("\t x-- = " + (x--) + " , x = " + x) ; // 先计算后自减
- System.out.println("y = " + y) ;
- System.out.println("\t --y = " + (--y) + " , y = " + y) ; // 先自减后计算
- }
- }
运算结果
- a = 3
- a++ = 3 , a = 4
- b = 3
- ++b = 4 , b = 4
- x = 6
- x-- = 6 , x = 5
- y = 6
- --y = 5 , y = 5
逻辑运算符
不管是短路还是非短路,其基本的操作结果都是一样的。
现有如下的错误代码:
- public class T {
- public static void main(String[] args) {
- int i = 10/0;
- System.out.println(i);
- }
- }
以上的代码只要一运行就会出现问题。
- public class T {
- public static void main(String[] args) {
- if(10!=10&10/0==0){
- System.out.println("条件满足");
- }
- }
- }
短路与
- public class T {
- public static void main(String[] args) {
- if(10!=10&&10/0==0){
- System.out.println("条件满足");
- }
- }
- }
只要第一个条件满足,之后的程序代码都不在执行了。
位运算符
位运算符的结果表
- public class T {
- public static void main(String[] args) {
- int x = 3 ; // 3的二进制数据: 00000000 00000000 00000000 00000011
- int y = 6 ; // 6的二进制数据: 00000000 00000000 00000000 00000110
- System.out.println(x & y) ; //与: 00000000 00000000 00000000 00000010
- System.out.println(x | y) ; //或: 00000000 00000000 00000000 00000111
- System.out.println(x ^ y) ; //或: 00000000 00000000 00000000 00000101
- }
- }
程序的结构
一般来说程序的结构包含有下面三种:
1)顺序结构
2)选择结构
3)循环结构
在使用switch进行表达式判断的时候,在表达式中只能使用数字或字符。
- public class T {
- // 完成一个四则运算的功能
- public static void main(String args[]){
- int x = 3 ;
- int y = 6 ;
- char oper = '+' ;
- switch(oper){
- case '+':{ // 执行加法操作
- System.out.println("x + y = " + (x + y )) ;
- break ;
- }
- case '-':{ // 执行减法操作
- System.out.println("x - y = " + (x - y )) ;
- break ;
- }
- case '*':{ // 执行乘法操作
- System.out.println("x * y = " + (x * y )) ;
- break ;
- }
- case '/':{ // 执行除法操作
- System.out.println("x / y = " + (x / y )) ;
- break ;
- }
- default:{
- System.out.println("未知的操作!") ;
- break ;
- }
- }
- }
- }
在以上的操作中,每个语句后面都会存在一个break,此语句表示退出整个switch()语句,如果不写上此语句,则所有的操作将在第一个满足条件之后全部输出直到遇到break为止
- public class T {
- // 完成一个四则运算的功能
- public static void main(String args[]){
- int x = 1;
- int sum = 0 ; // 保存累加的结果
- while(x<=10){
- sum += x ; // 进行累加操作
- x++ ; // 修改循环条件
- }
- System.out.println("1 --> 10 累加的结果为:" + sum) ;
- }
- }
- public class T {
- public static void main(String args[]){
- int x = 1;
- int sum = 0 ; // 保存累加的结果
- do{
- sum += x ; // 执行累加操作
- x++ ;
- }while(x<=10) ;
- System.out.println("1 --> 10 累加的结果为:" + sum) ;
- }
- }
- public class T {
- public static void main(String args[]){
- int sum = 0 ; // 保存累加的结果
- for(int x=1;x<=10;x++){
- sum += x ;
- }
- System.out.println("1 --> 10 累加的结果为:" + sum) ;
- }
- }
中断语句
- public class T {
- public static void main(String args[]){
- for(int i=0;i<10;i++){
- if(i==3){
- break;
- }
- System.out.println("i = " + i) ;
- }
- }
- }
输出结果
- i = 0
- i = 1
- i = 2
使用continue就是中断一次循环的执行
- public class T {
- public static void main(String args[]){
- for(int i=0;i<10;i++){
- if(i==3){
- continue;
- }
- System.out.println("i = " + i) ;
- }
- }
- }
输出结果
- i = 0
- i = 1
- i = 2
- i = 4
- i = 5
- i = 6
- i = 7
- i = 8
- i = 9