The best Java basic data types Tutorial In 2024, In this tutorial you can learn Built-in data types,Reference types,Java constants,

Java basic data types

Variable is to apply memory to store values. That is, when the variable is created when the need to apply for space in memory.

Memory management system according to the types of variables to allocate storage space for variables, the allocated space can only be used to store this type of data.

Therefore, by the definition of the different types of variables that can store integer, decimal, or character in memory.

Java two data types:

  • Built-in data types
  • Reference data types

Built-in data types

Java language provides eight basic types. Six kinds of numeric types (four integers, two floating-point) A character type, there is a Boolean.

byte:

  • byte data type is an 8-bit signed integer in two's complement representation;
  • The minimum value is -128 (-2 ^ 7);
  • The maximum value is 127 (2 ^ 7-1);
  • The default value is 0;
  • byte type used in a large array of space-saving, the main place of integers, because byte int variable space occupied by only a quarter of types;
  • Examples: byte a = 100, byte b = -50.

short:

  • short data type is 16-bit, signed integer in two's complement representation
  • The minimum value is -32768 (-2 ^ 15);
  • The maximum value is 32767 (2 ^ 15--1);
  • Short data types to be as space-saving as byte. A short variable is an int variable occupies one half of the space;
  • The default value is 0;
  • Examples: short s = 1000, short r = -20000.

int:

  • int data type is 32-bit, signed binary complement representation of integers;
  • The minimum value is -2,147,483,648 (-2 ^ 31);
  • The maximum value is 2,147,483,647 (2 ^ 31--1);
  • In general integer variable defaults to int type;
  • The default value is 0;
  • Examples: int a = 100000, int b = -200000.

long:

  • long data type is 64-bit signed integer in two's complement representation;
  • 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 in the system require a large integer;
  • The default value is 0L;
  • Examples: long a = 100000L, Long b = -200000L.

float:

  • float data type is a single-precision, 32-bit, IEEE 754-compliant floating point standard;
  • float float large group in the storage time can save memory space;
  • The default value is 0.0f;
  • Floating-point number can not be used for exact value, such as money;
  • Examples: float f1 = 234.5f.

double:

  • double data type is a double-precision, 64-bit, IEEE 754-compliant floating point standard;
  • The default type for floating point double type;
  • double type can not represent the exact same values, such as currency;
  • The default value is 0.0d;
  • Examples: double d1 = 123.4.

boolean:

  • boolean data type represents one bit of information;
  • Only two values: true and false;
  • This type is recorded only as a sign of true / false conditions;
  • The default value is false;
  • Examples: boolean one = true.

char:

  • 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);
  • char data type can store any character;
  • Examples: char letter = 'A'.

Examples

For basic types of numeric types in the range, we do not need to remember to force, because their values ​​have been defined as constants in the corresponding wrapper class. Consider 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);  
    }  
}  

Running instance »

Compile the above code output results are 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

Minimum and maximum values ​​Float and Double are in the form of scientific notation output, ending with "E + number" represents a number before E is multiplied by the number of 10-th. For example 3.14E3 is 3.14 × 10 3 = 3140,3.14E-3 is 3.14 x 10 -3 = 0.00314.

In fact, there is additionally a basic JAVA type void, it also has a corresponding wrapper class java.lang.Void, but we can not directly manipulate them.


Reference types

  • In Java, a variable reference type is very similar to C / C ++ pointer. Reference types point to an object, point to the object variable is a reference variable. These variables are specified in the declaration for a particular type, such as Employee, Pubby like. Once the declaration after the variable type can not be changed.
  • Objects, arrays are reference data types.
  • The default for all reference types is null.
  • A reference variable can be used with any compatible reference types.
  • Examples: Site site = new Site ( "w3big").

Java constants

Constants in the program is running, the amount will not be modified.

With the final keyword in Java modified constants and variables manner similar statement:

final double PI = 3.1415927;

Although the constant name to be lowercase, but in order to facilitate the identification, usually uppercase letters constant.

Literals can be assigned to a variable of any built-in types. E.g:

byte a = 68;
char a = 'A'

byte, int, long, short, and can be used in decimal, hexadecimal and octal way to represent.

When using a constant time, the prefix 0? Indicates octal, hexadecimal and prefix 0x representatives. E.g:

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

And other languages, Java string constants are also included in the sequence of characters between the two quotes. The following are examples of string literals:

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

String constants and character constants can contain any Unicode characters. E.g:

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

Java language support for some special escape sequences.

symbol Character Meaning
\ N Line feed (0x0a)
\ R Carriage return (0x0d)
\ F Formfeed (0x0c)
\ B Backspace (0x08)
\ S Space (0x20)
\ T Tabs
\ " Double quotes
\ ' apostrophe
\\ Backslash
\ Ddd Octal character (ddd)
\ Uxxxx Hexadecimal Unicode characters (xxxx)

This section explains the basic Java data types. The next section explores the different types of variables and their usage.

Java basic data types
10/30