Home  >  Article  >  Java  >  JAVA Tutorial | Chapter 2 Basic Data Types

JAVA Tutorial | Chapter 2 Basic Data Types

黄舟
黄舟Original
2017-02-25 09:46:321465browse

JAVA Chapter 2 Basic Data Types

Table of Contents


  • Java basic data types: two major categories

    • Built-in data types

    • Reference data types

  • Java type conversion

    • ##Automatic type conversion

    • Forced type conversion

  • ##Java constants



##Java basic data types

1. Built-in data types


##The Java language

provides eight basic types. Six numeric types (four integers, two floating point types), a character type, and a Boolean type.

JAVA Tutorial | Chapter 2 Basic Data Types


#Numeric type - integer type byte:

The byte data type is an 8-bit, signed, integer represented in two's complement;
    • The minimum value is -128 (-2^7);

    • The maximum value is 127 (2^7-1);

    • The default value is 0;

    • The byte type is used to save space in large arrays, mainly replacing integers, because the space occupied by byte variables Only a quarter of the int type;

    • #Example: byte a = 100, byte b = -50.

##Numeric type--integer type

short:

    • The short data type is a 16-bit, signed two's complement integer representation

    • The minimum value is -32768 (-2^15);

    • ##The maximum value is 32767 (2^15 - 1);

    • Short data type can also save space like byte. A short variable is half the space occupied by an int type variable;

    • The default value is 0;

    • Example: short s = 1000, short r = -20000.

##Numeric type--Integer type int:

    • int data type is a 32-bit, signed integer represented in two’s complement;

    • The minimum value is -2,147,483,648 (-2^31);

    • The maximum value is 2,147,483,647 (2^31 - 1);

    • Generally, integer variables default to int type;

    • The default value is 0;

    • Example: int a = 100000, int b = -200000.

##Numeric type--integer type long:

      The long data type is a 64-bit, signed integer represented in two’s complement;
    • The minimum value is -9,223,372,036,854,775,808 (-2^63);
    • ##The maximum value is 9,223,372,036,854,775,807 (2^63 -1);
    • This type is mainly used on systems that require relatively large integers;
    • The default value is 0L;
    • Example: long a = 100000L, Long b = -200000L.
##Numeric type--floating point type

float:

##The float data type is a single-precision, 32-bit floating point number that complies with the IEEE 754 standard;
    • float can save memory space when storing large floating point arrays;

    • The default value is 0.0f;

    • Floating point numbers cannot be used to represent precise values, such as currency;

    • Example: float f1 = 234.5f.

##Numeric type--Floating point type double:

    • The double data type is a double-precision, 64-bit, floating point number that complies with the IEEE 754 standard;

    • The default type of floating point numbers is double type;

    • The double type also cannot represent precise values, such as currency;

    • The default value is 0.0d;

    • Example: double d1 = 123.4.

##Boolean --boolean:

    • The boolean data type represents one bit of information;

    • has only two values: true and false;

    • This type is only used as a flag to record true/false situations;

    • ##The default value is false;
    • Example: boolean one = true.

Character type --char:

      The char type is a single 16-bit Unicode character;
    • The minimum value is '\ u0000' (that is, 0);
    • The maximum value is '\uffff' (that is, 65,535);
    • The char data type can store any character;
    • Example: char letter = 'A'.
Example

For basic types of numerical types We don't need to remember the value range, because their values ​​have been defined in the corresponding packaging class in the form of constants. Please look at the following example:

public class PrimitiveTypeTest {  
    public static void main(String[] args) {  
        // byte  
        System.out.println("基本类型:byte 二进制位数:" + Byte.SIZE);  
        System.out.println("包装类:java.lang.Byte");  
        System.out.println("最小值:Byte.MIN_VALUE=" + Byte.MIN_VALUE);  
        System.out.println("最大值:Byte.MAX_VALUE=" + Byte.MAX_VALUE);  
        System.out.println();  
  
        // short  
        System.out.println("基本类型:short 二进制位数:" + Short.SIZE);  
        System.out.println("包装类:java.lang.Short");  
        System.out.println("最小值:Short.MIN_VALUE=" + Short.MIN_VALUE);  
        System.out.println("最大值:Short.MAX_VALUE=" + Short.MAX_VALUE);  
        System.out.println();  
  
        // int  
        System.out.println("基本类型:int 二进制位数:" + Integer.SIZE);  
        System.out.println("包装类:java.lang.Integer");  
        System.out.println("最小值:Integer.MIN_VALUE=" + Integer.MIN_VALUE);  
        System.out.println("最大值:Integer.MAX_VALUE=" + Integer.MAX_VALUE);  
        System.out.println();  
  
        // long  
        System.out.println("基本类型:long 二进制位数:" + Long.SIZE);  
        System.out.println("包装类:java.lang.Long");  
        System.out.println("最小值:Long.MIN_VALUE=" + Long.MIN_VALUE);  
        System.out.println("最大值:Long.MAX_VALUE=" + Long.MAX_VALUE);  
        System.out.println();  
  
        // float  
        System.out.println("基本类型:float 二进制位数:" + Float.SIZE);  
        System.out.println("包装类:java.lang.Float");  
        System.out.println("最小值:Float.MIN_VALUE=" + Float.MIN_VALUE);  
        System.out.println("最大值:Float.MAX_VALUE=" + Float.MAX_VALUE);  
        System.out.println();  
  
        // double  
        System.out.println("基本类型:double 二进制位数:" + Double.SIZE);  
        System.out.println("包装类:java.lang.Double");  
        System.out.println("最小值:Double.MIN_VALUE=" + Double.MIN_VALUE);  
        System.out.println("最大值:Double.MAX_VALUE=" + Double.MAX_VALUE);  
        System.out.println();  
  
        // char  
        System.out.println("基本类型:char 二进制位数:" + Character.SIZE);  
        System.out.println("包装类:java.lang.Character");  
        // 以数值形式而不是字符形式将Character.MIN_VALUE输出到控制台  
        System.out.println("最小值:Character.MIN_VALUE="  
                + (int) Character.MIN_VALUE);  
        // 以数值形式而不是字符形式将Character.MAX_VALUE输出到控制台  
        System.out.println("最大值:Character.MAX_VALUE="  
                + (int) Character.MAX_VALUE);  
    }  
}


The output of compiling the above code is as follows:


基本类型:byte 二进制位数:8
包装类:java.lang.Byte
最小值:Byte.MIN_VALUE=-128
最大值:Byte.MAX_VALUE=127

基本类型:short 二进制位数:16
包装类:java.lang.Short
最小值:Short.MIN_VALUE=-32768
最大值:Short.MAX_VALUE=32767

基本类型:int 二进制位数:32
包装类:java.lang.Integer
最小值:Integer.MIN_VALUE=-2147483648
最大值:Integer.MAX_VALUE=2147483647

基本类型:long 二进制位数:64
包装类:java.lang.Long
最小值:Long.MIN_VALUE=-9223372036854775808
最大值:Long.MAX_VALUE=9223372036854775807

基本类型:float 二进制位数:32
包装类:java.lang.Float
最小值:Float.MIN_VALUE=1.4E-45
最大值:Float.MAX_VALUE=3.4028235E38

基本类型:double 二进制位数:64
包装类:java.lang.Double
最小值:Double.MIN_VALUE=4.9E-324
最大值:Double.MAX_VALUE=1.7976931348623157E308

基本类型:char 二进制位数:16
包装类:java.lang.Character
最小值:Character.MIN_VALUE=0
最大值:Character.MAX_VALUE=65535

Float和Double的最小值和最大值都是以科学记数法的形式输出的,结尾的"E+数字"表示E之前的数字要乘以10的多少次方。比如3.14E3就是3.14 × 103 =3140,3.14E-3 就是 3.14 x 10-3 =0.00314。



实际上,JAVA中还存在另外一种基本类型 void,它也有对应的包装类 java.lang.Void,不过我们无法直接对它们进行操作。




二、引用类型


在Java中,引用类型的变量非常类似于C/C++的指针。引用类型指向一个对象,指向对象的变量是引用变量。这些变量在声明时被指定为一个特定的类型,比如Employee、Pubby等。变量一旦声明后,类型就不能被改变了。


需要注意:

  1. 对象、数组都是引用数据类型。

  2. 所有引用类型的默认值都是null。

  3. 一个引用变量可以用来引用与任何与之兼容的类型。

  • 例子:Site site = new Site("Runoob")。




Java数据类型转换


Java 语言是一种强类型的语言。强类型的语言有以下几个要求:


  • 变量或常量必须有类型:要求声明变量或常量时必须声明类型,而且只能在声明以后才能使用。

  • 赋值时类型必须一致:值的类型必须和变量或常量的类型完全一致。

  • 运算时类型必须一致:参与运算的数据类型必须一致才能运算。



但是在实际的使用中,经常需要在不同类型的值之间进行操作,这就需要一种新的语法来适应这种需要,这个语法就是数据类型转换。


在数值处理这部分,计算机和现实的逻辑不太一样,对于现实来说,1和 1.0 没有什么区别,但是对于计算机来说,1 是整数类型,而 1.0 是小数类型,其在内存中的存储方式以及占用的空间都不一样,所以类型转换在计算机内部是必须的。


Java 语言中的数据类型转换有两种:



  • 自动类型转换:编译器自动完成类型转换,不需要在程序中编写代码。

  • 强制类型转换:强制编译器进行类型转换,必须在程序中编写代码。



由于基本数据类型中 boolean 类型不是数字型,所以基本数据类型的转换是出了 boolean 类型以外的其它 7 种类型之间的转换。下面来具体介绍两种类型转换的规则、适用场合以及使用时需要注意的问题。

一、自动类型转换


自动类型转换,也称隐式类型(隐含类型)转换,是指不需要书写代码,由系统自动完成的类型转换。由于实际开发中这样的类型转换很多,所以 Java 语言在设计时,没有为该操作设计语法,而是由 JVM 自动完成。 当范围低的基本类型和范围高的基本类型做运算时,范围低的就会自动转换为高范围的类型,还有,当子类赋给父类变量时,也属于自动类型转换。而一般强制类型转换就是反过来说,也是他们的区别

  • 1. 整数的默认类型时int

  • 2. 浮点型不存在这种情况,因为在定义float类型时必须在数字后面跟上F或者f



转换规则:从存储范围小的类型到存储范围大的类型。
具体规则为:byte→short(char)→int→long→float→double

所有,整型、实型(常量)、字符型数据可以混合运算。运算中,不同类型的数据先转化为同一类型,然后进行运算。转换从 --> 低级到高级。


byte,short,char—> int —> long—> float —> double


也就是说 byte 类型的变量可以自动转换为 short 类型,示例代码:


byte  b  =  10;



short  sh  =  b;这里在赋值时,JVM 首先将 b 的值转换为 short 类型,然后再赋值给 sh。
在类型转换时可以跳跃。示例代码:



byte  b1  =  100;int  n  =  b1;



注意问题 : 在整数之间进行类型转换时,数值不发生改变,而将整数类型,特别是比较大的整数类型转换成小数类型时,由于存储方式不同,有可能存在数据精度的损失。

 

  • int i =128;   byte b = (byte)i;

    因为byte类型时8位,最大值为127,所以当强制转换为int类型值128时候就会导致溢出。


  • ——浮点数到整数的转换是通过舍弃小数得到,而不是四舍五入,例如:

    (int)23.7 == 23;		(int)-45.89f == -45

必须满足转换前的数据类型的位数要低于转换后的数据类型,例如: short数据类型的位数为16位,就可以自动转换位数为32的int类型,同样float数据类型的位数为32,可以自动转换为64位的double类型。

数据类型转换必须满足如下规则:

1. 不能对boolean类型进行类型转换。

2. 不能把对象类型转换成不相关类的对象。

3. 在把容量大的类型转换为容量小的类型时必须使用强制类型转换。

4. 转换过程中可能导致溢出或损失精度,例如:

public class ZiDongLeiZhuan{
        public static void main(String[] args){
            char cao1='a';//定义一个char类型
            int it1 = cao1;//char自动类型转换为int
            System.out.println("char自动类型转换为int后的值等于"+it1);
            char cao2 = 'A';//定义一个char类型
            int it2 = cao2+10;//char 类型和 int 类型计算
            System.out.println("char类型和int计算后的值等于"+it2);
        }
}


运行结果为:


char自动类型转换为int后的值等于 97
char类型和int计算后的值等于 75


解析:

     cao1 的值为字符'a', 查 ascii 码表可知对应的int类型值为 97( 文章最后会提供 ASCII 表 )

那么字符 cao2 的'A'   对应值为65,所以it2=65+10=75。


二、强制类型转换

  • 强制类型转换,也称显式类型转换,是指必须书写代码才能完成的类型转换。该类类型转换很可能存在精度的损失,所以必须书写相应的代码,并且能够忍受该种损失时才进行该类型的转换。

  • 1. 条件是转换的数据类型必须是兼容的。

  • 2. 格式:(type)value type是要强制类型转换后的数据类型 

  • 实例:

  • public class QiangZhiZhuanHuan{
            public static void main(String[] args){
                int i1 = 123456;
                byte b = (byte)i1;//强制类型转换为byte
                System.out.println("int强制类型转换为byte后的值等于"+b);
                
            }
    }
  • 运行结果:

    int强制类型转换为byte后的值等于 12356

转换规则:从存储范围大的类型到存储范围小的类型。

具体规则为:double→float→long→int→short(char)→byte

语法格式为:(转换到的类型)需要转换的值

示例代码:


double  d  =  3.10;int  n  =  (int)d;


这里将 double 类型的变量 d 强制转换成 int 类型,然后赋值给变量 n。需要说明的是小数强制转换为整数,采用的是“去 1 法”,也就是无条件的舍弃小数点的所有数字,则以上转换出的结果是 3。整数强制转换为整数时取数字的低位,例如 int 类型的变量转换为 byte 类型时,则只去 int 类型的低 8 位(也就是最后一个字节)的值。


示例代码:


int  n  =  123;byte  b  =  (byte)n;int  m  =  1234;byte  b1  =  (byte)m;


则 b 的值还是 123,而 b1 的值为-46。

b1 的计算方法如下:m 的值转换为二进制是10011010010,取该数字低8位的值作为b1的值,则b1的二进制值是11010010,按照机器数的规定,最高位是符号位,1 代表负数,在计算机中负数存储的是补码,则该负数的原码是 10101110,该值就是十进制的-46。


注意问题:强制类型转换通常都会存储精度的损失,所以使用时需要谨慎。


Java的常量

常量在程序运行时,不会被修改的量。

在 Java 中使用 final 关键字来修饰常量,声明方式和变量类似:

final double PI = 3.1415927;

虽然常量名也可以用小写,但为了便于识别,通常使用大写字母表示常量。

字面量可以赋给任何内置类型的变量。例如:

byte a = 68;
char a = 'A'

byte、int、long、和short都可以用十进制、16进制以及8进制的方式来表示。

当使用常量的时候,前缀0表示8进制,而前缀0x代表16进制。例如:

int decimal = 100;
int octal = 0144;
int hexa =  0x64;


和其他语言一样,Java的字符串常量也是包含在两个引号之间的字符序列。下面是字符串型字面量的例子:

"Hello World"
"two\nlines"
"\"This is in quotes\""

字符串常量和字符常量都可以包含任何Unicode字符。例如:

char a = '\u0001';
String a = "\u0001";



Java语言支持一些特殊的转义字符序列:


##Enter (0x0d)##\f\b\s
Symbol Character meaning
\n Line feed (0x0a)
##\r
##Form feed (0x0c)
Backspace(0x08)
Space (0x20) ##\t
Tab character \"
Double quotes \ ' Single quotes
##\\ Backslash
\ddd Octal characters (ddd)
\uxxxx 16 Unicode characters (xxxx)



##Attachment: ASCII table

ASCII 表
## The above is the JAVA pit tutorial | Chapter 2: Basic Data Types. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn