Introduction to basic data types for Java

Source: Internet
Author: User

1. Data types for Java

The data types of Java include basic data types and reference data types. The basic data type is what we typically call 4 classes of 8 data types, as shown in the 4 classes are integer type, floating-point type, character type, and Boolean, 8 of which are byte, short, int, long, float, double, char, Boolean, Where byte is 1 bytes (1 bytes have 8 bits, which is 8bit), short is 2 bytes, int is 4 bytes, Long is 8 bytes, float is 4 bytes, double is 8 bytes, char is 2 bytes, Boolean is 1/8 bytes, That is, a 1-bit (bit). In addition to the base data type, or reference data type, the reference data type is an array, an interface, or a class.


2. Conversion of Java basic data types

This is called an automatic type conversion if the system supports assigning a value of a primitive type directly to a variable of another base type. In Java, when a number or variable with a small range of tables is assigned to a variable with a large range of other tables, the system can automatically convert, just like two bottles of water, without any problem when pouring the vial of water into a large bottle.

As shown, the numeric value on the left side of the arrow can automatically go to the numeric type on the right side of the arrow.

The following code shows the automatic conversion of basic data types in Java (code from the Crazy Java Handout):

public class Autoconversion {public static void main (string[] args) {int a  = 6;//int can be automatically converted to float type float f = a;//below will output 6 .0SYSTEM.OUT.PRINTLN (f);//defines an integer variable of type byte byte B = 9;//The following code will be faulted, byte type cannot be automatically converted to char type//char c = b;// The following is a byte variable that can be converted automatically to double type double d = b;//below will output 9.0system.out.println (d);}}
In addition, if you want to convert the type on the right side of the arrow to the left type, you must use the cast syntax: (type) value, such as int a = (int) 1.0. The following code shows the functionality of Java's basic data type coercion (the code comes from the Crazy Java Handout):
public class Narrowconversion {public static void main (string[] args) {int ivalue = 233;//forces a value of type int to be converted to a value of type byte bVal UE = (byte) ivalue;//outputs -23system.out.println (bValue);d ouble dvalue = 3.98;//forces a value of type double to intint ToI = (int) dvalue ;//Will output 3system.out.println (ToI);}}

In addition, there are several important notes:

    • The Boolean type cannot be converted to another type, and other types cannot be converted to a Boolean type.
    • Byte, short, char do not convert to each other, the three in the calculation is first converted to the type int before the operation.
    • When a large data type is converted to a small-capacity data type, it is necessary to strengthen the conversion character, but may result in reduced precision or removal.
    • There are multiple types of data in a mixed operation, the system first automatically converts all data types to the largest data type, and then calculates.

See the following code for an explanation of these points:

public class Testconvert {public static void main (string[] args) {int a = $, b = 300;double D1 = (A + b) * 1.2;//System converts it A double calculates the float F1 = (float) ((A + B) * 1.2); To strengthen the conversion character byte B1 = 127;byte B2 = 10;//The system converts it to an int type for calculation, to strengthen the conversion character byte B3 = (byte) (B1 + b2); SYSTEM.OUT.PRINTLN (B3); Output -119double D2 = 1e200;float F2 = (float) D2; Float is up to 10e38system.out.println (F2); Output infinityfloat F3 = 1.53f; Must be added flong L1 = 123;long L2 = 4000000000L; Must be added lfloat f = L1 + L2 + F3; The system converts all types to float type to calculate long L = (long) F; Cut off the decimal point}}




Reference documents:

Crazy Java handout, Li Gang, electronic industry press.

Introduction to basic data types for Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.