Java Basic data types

Source: Internet
Author: User

Java data types are divided into basic data types and reference data types.

  

1 Basic data types

byte: Smallest data type in Java, 1 bytes (8 bit) in memory, value range -128~127, default value 0

short: Shorter integer, 2 bytes ( -32768~32717), range of values, default value 0

int: integer, for storing integers, 4 bytes in memory, value range -2147483648~2147483647, default value 0

Long: Length integer, 8 bytes -2^63~2^63-1 in memory, default value 0L

float: floating point, 4 bytes in memory, used to store a number with a decimal point (the difference from a double is that the float type has a valid decimal point of only 6~7 bit), and the default value of 0

Double: dual-precision floating point for storing numbers with decimal points, 8 bytes in memory, default 0

Char: Character type, used to store a single character, 2 bytes in memory, range 0~65535, default value is empty

Boolean: boolean type, 1 bytes, used to determine true or false (only two values, that is, true, false), default value False

2 reference types

The Java language itself does not support a struct (struct) or union (Union) data type in C + +, and its composite data types are typically constructed from classes or interfaces, which provide a way to bundle data and methods while hiding information outside the program. There are 3 kinds of reference types: class, interface, array;

class: All classes, whether the Java itself already exists, or the user later created;

Objec class: It is the parent class of all classes, each class implements the method of the class, and object can be used to define all classes;

String:string class represents strings, Java

interface (interface): system comes with or user-created

array: The system comes with or user-created

3 How data types in Java are stored in memory

(1) Basic data type Storage principle: All simple data types do not have the concept of "reference", the basic data types are directly stored in memory on the memory stack, the value of the data itself is stored in the stack space, the Java language inside eight types of data is this storage model;

(2) The storage principle of reference types: reference types inherit from the object class (also reference type) are stored according to the memory model of Java storage objects, using the Java memory heap and memory stack for this type of data storage, simply speaking, "reference" ( The address of the storage object on the memory heap is stored on an ordered memory stack, and the value of the object itself is stored on the memory heap;

The difference between a base data type and a reference type is primarily that the base data type is allocated on the stack, and the reference type is allocated on the heap

Examples Show

Let's analyze the difference between = = and Equals ()

First, define two string array objects

String[] a={"Latiny1", "Latiny2"};
String[] b={"Latiny1", "Latiny2"};

and then compare
if (a==b)
{
System.out.println ("A==b");
}
Else
{
System.out.println ("A!=b");
}

Program Output A!=b

Cause: The address of A and B is not the same, A==b compares the address of two variables, the address of data A and B is not the same

Whether it is a basic data type or a reference type, they will first allocate a piece of memory on the stack, for the base type, the area contains the basic type of content, and for the reference type, this area contains a pointer to the real content, the real content is manually allocated to the heap.

4 Basic Type conversions

(1) Automatic type conversion

All Java numeric type variables can be converted to each other, and if the system assigns a value of a primitive type directly to a variable of another primitive type, this is called an automatic type conversion.

int e = 1;

Double d =e;

The above code will not error, the system will automatically convert.

(2) Forced type conversion

A cast is required when assigning a large range of values or variables to another variable with a small range.

Double d = 1.5;

int e = D;

The above code will error, the large range of values assigned to a small range of variables, need to cast, so as to compile, and a large range of values will lose precision.

int e = (int) d;

Java Basic data types

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.