Home > Java > javaTutorial > body text

[java tutorial] Java basic data types

黄舟
Release: 2016-12-26 11:23:24
Original
1392 people have browsed it

Java basic data types

Variables apply for memory to store values. In other words, when creating a variable, you need to apply for space in memory.

The memory management system allocates storage space for variables according to their type, and the allocated space can only be used to store data of this type.

Therefore, by defining variables of different types, integers, decimals or characters can be stored in memory.

Java's two major data types:

Built-in data types

Reference data types

Built-in data types

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

byte: The

byte data type is an 8-bit, signed, integer represented in two's complement; the minimum value of

is -128 (-2^7) ;

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

The default value is 0;

byte type is used to save space in large arrays, mainly replacing integers. Because the byte variable occupies only one-quarter of the space of the int type;

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

short:

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

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.

int: The

int data type is a 32-bit, signed two's complement integer; the

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

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

Generally, integer variables default to int type;

The default value is 0;

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

long: The

long data type is a 64-bit, signed integer represented in two's complement; the minimum value of

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, int b = -200000L.

float:

float data type is a single-precision, 32-bit, IEEE 754-compliant floating point number;

float can save memory when storing large floating point arrays Space;

The default value is 0.0f;

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

Example: float f1 = 234.5f.

double:

double data type is a double-precision, 64-bit, IEEE 754-compliant floating point number;

The default type of floating point number is double type;

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

The default value is 0.0f;

Example: double d1 = 123.4.

boolean:

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;

Default value is false;

Example: boolean one = true.

char:

The char type is a single 16-bit Unicode character;

The minimum value is 'u0000' (that is, 0);

The maximum value The value is 'uffff' (that is, 65,535);

char data type can store any character;

Example: char letter = 'A'.

Example

For the value range of the basic types of numerical types, we do not need to be forced to remember, 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);  
    }  
}
Copy after login

Compile the above code and the output result 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
Copy after login

The minimum and maximum values ​​​​of Float and Double are output in the form of scientific notation, and the ending "E+number" indicates how many times 10 the number before E should be multiplied. For example, 3.14E3 is 3.14×1000=3140, and 3.14E-3 is 3.14/1000=0.00314.

Actually, there is another basic type void in JAVA, which also has a corresponding packaging class java.lang.Void, but we cannot directly operate on them.

Reference types

Reference type variables are created by the constructor of a class and you can use them to access the referenced object. These variables are specified as a specific type when declared, such as Employee, Pubby, etc. Once a variable is declared, its type cannot be changed.

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

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

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

例子:Animal animal = new Animal(“giraffe”)。

Java常量

常量就是一个固定值。它们不需要计算,直接代表相应的值。

常量指不能改变的量。 在Java中用final标志,声明方式和变量类似:

final double PI = 3.1415927;
Copy after login

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

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

byte a = 68;
char a = 'A'
Copy after login

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

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

int decimal = 100;
int octal = 0144;
int hexa =  0x64;
Copy after login

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

"Hello World"
"two\nlines"
"\"This is in quotes\""
Copy after login

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

char a = '\u0001';
String a = "\u0001";
Copy after login

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


符号   字符含义


\n    换行 (0x0a)    

\r    回车 (0x0d)    

\f    换页符(0x0c)    

\b    退格 (0x08)    

\s    空格 (0x20)    

\t    制表符    

\"    双引号    

\'    单引号    

\\    反斜杠    

\ddd    八进制字符 (ddd)    

\uxxxx    16进制Unicode字符 (xxxx)    


这一节讲解了Java的基本数据类型。下一节将探讨不同的变量类型以及它们的用法。

 以上就是【java教程】Java基本数据类型的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!