Java Basics--Basic Data Types

Table of contents

Foreword:

built-in data types

type default

Example:

Built-in data type conversion

Automatic type conversion (implicit type conversion):

Casting (explicit type conversion):

Implicit cast:

reference type


Foreword:

All data types in Java are shown in the figure below.

built-in data types

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

  1. Integer Types:

    • byte: 8-bit signed integer ranging from -128 to 127.
    • short: 16-bit signed integer in the range -32,768 to 32,767.
    • int: 32-bit signed integer in the range -2,147,483,648 to 2,147,483,647.
    • long: 64-bit signed integer in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  2. Floating-Point Types:

    • float: 32-bit IEEE 754 single-precision floating-point number.
    • double: 64-bit IEEE 754 double-precision floating-point number.
  3. Character Type:

    • char: 16-bit unsigned Unicode character ranging from '\u0000' (0) to '\uffff' (65535).
  4. Boolean Type:

    • boolean: Indicates true or false value, only two values: true and false.

type default

The following table lists the default values ​​for each type in Java:

type of data Defaults
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char 'u0000'
String (or any object) null
boolean false

Example:

public class Test {
    static boolean bool;
    static byte by;
    static char ch;
    static double d;
    static float f;
    static int i;
    static long l;
    static short sh;
    static String str;
 
    public static void main(String[] args) {
        System.out.println("Bool :" + bool);
        System.out.println("Byte :" + by);
        System.out.println("Character:" + ch);
        System.out.println("Double :" + d);
        System.out.println("Float :" + f);
        System.out.println("Integer :" + i);
        System.out.println("Long :" + l);
        System.out.println("Short :" + sh);
        System.out.println("String :" + str);
    }
}

The output is:

Bool     :false
Byte     :0
Character:
Double   :0.0
Float    :0.0
Integer  :0
Long     :0
Short    :0
String   :null

Built-in data type conversion

Automatic type conversion, mandatory type conversion, and implicit type conversion are the three types of type conversion in Java.

  • Automatic type conversion (implicit type conversion):

Under certain conditions, the conversion between the two data types can be done automatically by the compiler without explicit declaration. This conversion needs to meet two conditions:

  1. Data types are compatible with each other
  2. The target type has a larger value range than the source type.

For example:

byte b = 3;
int x = b;

In the above code, the variable b of type byte is assigned to the variable x of type int. Because the value range of the int type is larger than that of the byte type, the compiler will automatically perform type conversion without data loss. 

  • Casting (explicit type conversion):

When the two types are incompatible with each other, or the value range of the target type is smaller than that of the source type, it is necessary to explicitly declare the type conversion.

For example:

int a = 1000;
byte b = (byte) a;

When executing this code, a data overflow will occur. Since the value range of the byte type is -128 to 127, and 1000 is beyond the value range of the byte type. Therefore, the cast will lose high-order information, and the result is that the b variable will be assigned the value -24 , which is the result of 1000 modulo 256 (-24 = 1000 % 256).

In the above code, the variable a of type int is cast to the variable b of type byte. Since the value range of the byte type is smaller than that of the int type, forced conversion may result in loss of data precision. (byte)Therefore, it needs to be explicitly declared when converting to tell the compiler to perform mandatory type conversion.

It should be noted that when performing forced type conversion, the value range between the source type and the target type needs to be carefully considered to avoid data overflow or precision loss.

  • Implicit cast:

  1. The default type of integers is int: In Java, the default type of integer literals is int. For example, if you directly initialize an integer variable with a literal value, the compiler defaults it to type int.
  2. Decimals are double type floating point by default: In Java, the default type of decimals (including decimal point and scientific notation) is double. If you want to define a decimal number of type float, you must add F or f suffix after the number to clearly indicate that it is of type float. (Note that if no F or f suffix is ​​specified, the compiler will default the decimal to double type.)

For example:

int a = 10; // 这里的整数字面值10被隐含地当作int类型处理
float b = 3.14F; // 这里的3.14被隐含地当作double类型,添加F后缀指定为float类型
System.out.println("a=" + a);
System.out.println("b=" + b);

operation result:

a=10
b=3.14

reference type

  • In Java, variables of reference type are very similar to C/C++ pointers. A reference type points to an object, and a variable pointing to an object is a reference variable. These variables are specified as a specific type when declared, such as Employee, Puppy, etc. Once a variable is declared, its type cannot be changed.
  • Objects and arrays are reference data types.
  • The default value for all reference types is null.
  • A reference variable can be used to refer to any compatible type.

In Java, we can create custom reference types, such as the Site class. In this example, a reference variable named site is declared first, and its type is Site. Then use the keyword new to create a Site object and assign its address to the site variable.

Site site = new Site("Runoob");

In the above code, Sitea custom Siteclass is referenced, which may contain properties and behaviors (methods) related to the website. The parameters passed in parentheses are initialized objects "Runoob"through the constructor .Site

It is worth noting that in this process, Sitethe class must be defined and exists, otherwise it will cause compilation errors.

In this way, we can use reference variables siteto manipulate and access Sitethe properties and methods of the object. For example, we can call sitemethods on an object or access its properties:

site.getName();      // 调用Site对象的getName方法
site.setName("Google");    // 修改Site对象的name属性

To summarize, variables of a reference type specify a specific type when declared and can be used to refer to objects that are compatible with it. By using reference variables, we can manipulate and access properties and methods of objects. At the same time, you need to pay attention to the default value of the reference type null, which means that the reference variable does not currently point to any object.

Guess you like

Origin blog.csdn.net/m0_74293254/article/details/132209748
Recommended