Java is divided into basic data types and reference data types (question: the difference between heap and stack, and what the system does to differentiate stack memory)

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators wrapper

First, the basic data type:

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

short: Shorter integer, 16 bits in memory, 2 bytes, range -32768~32717, default value 0

int: integral type, used to store integers, 32 bits, 4 bytes, value range -2147483648~2147483647, default value 0

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

float: floating point, 32 bits in memory, 4 bytes, for storing a number with a decimal point (the difference from a double is that the float is a valid decimal point only 6~7 bit), and the default value of 0

Double: dual-precision floating point, used to store a number with a decimal point, 64 bits in memory, 8 bytes, default value 0

Char: Character type, used to store a single character, 16 bits, or 2 bytes, value 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

Ii. Basic concepts of Java data types :

The data type in computer language is an abstract expression of memory location, which can be understood as an abstract expression for memory. With each language, there will be data types of understanding, there are complex, simple, all kinds of data types need to understand at the beginning of learning, Java is a strongly typed language, so Java for Data type specification will be relatively strict. The data type is the abstract atomic concept of language, which can be said to be the most basic unit definition in language, in Java, there are essentially two types of data: basic type and reference data type.

Basic types: Simple data types are not simplified, built-in data types, defined by the programming language itself, and represent real numbers, characters, and integers.

Reference data type: 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 of the program.

Third, the relationship between data types and memory in Java

In Java, each variable that holds data is of a type, such as:

char ch; float x; int a,b,c;

CH is a character type and is allocated to 2 bytes of memory. Different types of variables have different bytes allocated in memory, and they are stored in different ways.

Therefore, it is necessary to determine the type of the variable before assigning a value, and determine the type of the variable, that is, determine the size of the memory space to allocate the data, and how the data will be stored in memory.

Iv. Storage of Java data types 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, and the Java language inside the eight data types is this storage model;

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

difference: The difference between the base data type and the reference type is primarily that the base data type is allocated on the stack, and the reference type is allocated on the heap (requires Java stack, heap concept),

The memory model of the base type and the reference type is essentially different.

Example 1: Let's analyze the difference between "= =" and Equals ().

First, I'll set the two string object

Stringa= "ABC";

stringb= "ABC";

And then

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

Example 2: Defining two basic types

int a=4;

int b=4;

if (a==b) {System.out.println ("a==b");}

Else

{System.out.println ("a!=b");}

Output: A==b

Cause: = = Compares the contents of a two variable

conjecture: Regardless of the basic data type or reference type, they will first allocate a piece of memory in the stack, for the base type, the area contains the basic type of content, and for the object type, this area contains a pointer to the real content, the real content is manually allocated to the heap .

V. Java basic Type value range calculation

From the point of view of the computer composition principle can be explained:

Byte is 8 bytes in the computer, and byte is a signed shape, with a binary representation when the highest bit is the symbol bit 0 for a positive number 1 for negative numbers.

Maximum value: 127 is 2 of the 7-square minus 1, the minimum: 2 of the preceding negative symbol: 7. (Contains the beginning, does not contain the end);

Positive numbers exist in the form of the original code in the computer;

Negative numbers in the computer is in its complement form exists, that is, the absolute value of the original code to the binary and then bitwise reverse after adding 1.

The following 10 and 10 are examples of: 10 original code: 00001010 It is stored in the computer is 0000 1010,-10 According to the previous calculation except its absolute value is 10, to the binary 0000 1010 bitwise REVERSE 1111 0101 plus 1:1111 0110, This is-10 complement, OK, 1111 0110 of the computer is represented-10.

Let's take a look at the binary representation of 128 absolute value 128:1000 0000 Bitwise counter 0111 1111 plus 1:1000 0000, that is 128 in the computer is 1000 0000, and then look at-129 in the computer representation, the absolute value of 129 of the fan has exceeded the number of bits in byte. So pay attention to this kind of problem;

Vi. Description of Java types

1. Overview:

Data types in Java are categorized into reference data types and basic data types.

There are 3 types of reference data: Class, interface, array;

The basic data type is also divided into Boolean type and numeric type;

Boolean type: Boolean (logical) Trure or false default is false;

Numeric types are divided into fixed-point types and floating-point types;

Fixed-point type of integer type and character type;

2. Default initialization for Java variables

Type

Default initialization value

Boolean

False

Int

0

Short

0

Float

0.0

Double

0.0

Char

\

Long

0

Byte

0

Object

Null

3, type detailed explanation:

1) integer types: Byte, short, int, long are all integers, except that they do not have the same range of values.
Byte (byte type) A byte 8-bit with a value range of -128~127 and 1 bytes (-2 7 to 2 7) by default is 1
Short 16 bits, with a value range of -32768~32767 and 2 bytes (-2 15 to 2 15) by default is 1 (= 0)
int (integer) an int 32 bits with a value range of ( -2147483648~2147483647) and 4 bytes (-2 31 to 2 31) by default is 1
Long (length integer) A long 64-bit, the value range is ( -9223372036854774808~9223372036854774807), occupies 8 bytes (-2 of 63 square to 2 of 63) By default is 0L or 0l recommended capitalization;

You can see that the value range of byte and short is small, and Long's range is too large, occupy more space, basically int can meet our daily calculations, and int is the most used integer type. In general, if there is an integer number such as 35 in Java, then this number is the int type, if we want it to be a byte type, we can add the uppercase b:35b after the data, indicating that it is byte type, the same 35S means short type, 35L means long, which means int we can do nothing, but if you want to represent a long type, be sure to add "L" after the data.

1.1) fixed-point constants

Fixed-point constants are integer constants, which can be represented in decimal, octal, and 16-based three ways.

Decimal fixed-point constants: such as 123,-456, 0.

Octal fixed-point constant: 0 preamble, in form 0DD...D. such as 0123 means that the decimal number 83,-011 represents the decimal number-9.

Hexadecimal fixed-point constants: Starting with 0x or 0X, such as 0x123 means decimal number 291,-0x12 represents decimal number-18.

1.2) fixed-point variable

Fixed-point variables, integer variables, can be subdivided into byte-type variables, integer variables, short-integer variables, and long-integer variables of four.

A brief description of the overhead memory bytes and the range of values for various fixed-point variables.

Note that if you are assigning a fixed-point constant to a fixed-point variable, you need to check to see if the constant is within the range of the variable's expression, such as an out-of-scope program that compiles an error.

2) Char type (character type)

one character (char) in Java represents an element in the Unicode character set.

Unicode characters are made up of 16 bits, so there are (65535) different characters available,

The Unicode character set contains all the characters in different languages, as well as the usual symbols in math, science, and text, so it gives us a lot of flexibility.
A single character expressed in single quotes, usually in 16 notation,

range from ' to '? (U tells the compiler that you are using two-byte [16-bit] character information to represent a Unicode character).

The data type used to hold the character, which occupies 2 bytes, is Unicode encoded, and its first 128 byte encoding is ASCII compatible,
What is the storage range of characters in \~\?, when defining character-type data, be careful to add ' ', such as ' 1 ' for the character ' 1 ' instead of the value 1.

2.1) Character constants

Character constants are single characters enclosed in single quotes, such as ' a ', ' a ', and note that the delimiter of a character is a single quote, not a double quote.

In addition to the character constants in the form described above, Java also allows the use of a special form of character constant value,

This is commonly used to denote characters that are difficult to represent in general characters, a character sequence that begins with a "\", called an escape character.

Common escape characters in Java are shown in table

2.2) Character variables

variables defined in char, such as Char c= ' a ';

In particular, Java's text encoding takes a Unicode set, Java character 16-bit unsigned data, and a character variable that occupies 2 bytes in memory.

Note: char c = ' 1 ', we try to output C to see, System.out.println (c), the result is 1, and if we output it like this System.out.println (c+0), the result becomes 49, because 0 is the int type, An upward-type conversion is made, and the result is an int.
If we define C as such, char c = ' \1 '; The result of the output is still 1, because the character ' 1 ' corresponds to the Unicode encoding that is \1.

3) Floating-point type: float and double are data types that represent floating-point types, and the difference between them is that they differ in their accuracy.

3.1) floating-point constants

That is, a numeric value with a decimal point, which can be expressed in two forms: a numeric value with a decimal point and a scientific counting method.

numeric form with decimal point: consists of a number and a decimal point, such as 0.123,. 123, 123., 123.0.

Scientific notation representation: consists of the general real numbers and E±n (e±n), such as 12.3e3, 5E-3, respectively, which represent 12.3 times 10 of the 3, 5 times 10-3 of the square. Note that there must be a number before e or E, and the exponent after E or E must be an integer.

3.2) floating-point variable

Floating-point variables have a single-precision variable and a double-precision variable, and different precision overhead memory bytes and the expression of the range of values differ. Two floating-point variables for memory bytes and range of values

Floating-point constants also have a single-precision and double-precision, the constants listed above are double-precision constants, if you want to specifically describe a single-precision constant, you can end the data with F or f as a suffix, such as 12.34f. If you want to specifically indicate that a floating-point constant is a double-precision constant, you do not need to add a suffix at the end of the data, or D or D as a suffix, such as 12.34d.

Float (single-precision floating-point type) A float 32-bit, occupies 4 bytes, example 3.2F, the default is 0.0f, 3.402823e+38 ~1.401298e-45 (e+38 means that is multiplied by 10 38 times, similarly, e-45 is multiplied by 10 of the negative 45 times).
Double (dual-precision floating-point type) One Dobule 64 bits takes 8 bytes, example 3.2, default is 0.0, 1.797693e+308~4.9000000e-324 occupies 8 bytes

Note: The double type has a larger storage range than the float type and is more accurate, so the usual floating-point data is double in the case of non-declaration.

If you want to indicate that a data is of type float, you can add "F" to the data behind it. The floating-point data is not completely accurate, so sometimes it is normal to have floating in the last few decimal places when calculating.

Related introduction:

There are several simple features in the Java basic type when using literal assignment:

1 "When the integer type of data using literal assignment, the default value is int type, that is, when using 0 or other numbers directly, the value is of type int, so when using a long a = 0 this way of assignment, there is data conversion inside the JVM.

2 "floating-point type of data when using literal assignment, the default value is double type, that is, when the literal two appear, the JVM will use a double type of data type.

3 "Starting with JDK 5.0, there is an automatic unpacking unpacking operation in Java, which requires a certain description:

corresponding to the original data type, each data type has a wrapper class of reference type, namely Boolean, short, Float, Double, Byte, Int, Long, Character, which are the built-in encapsulation classes. These encapsulation classes (Wrapper) provide a very intuitive approach, for the encapsulation class needs to be explained that each encapsulation class has a xxxvalue () method, in this way it can be referenced by the value of the object into the value of the basic variable, not only that, Each wrapper class also has a valueof (string) method that directly converts the string object to the appropriate simple type.

Prior to JDK 5.0, there was no automatic unpacking operation, auto box operation, so it was not possible to use an assignment code in the following way: Integer a = 0;//This assignment cannot be passed in JDK 1.4 and the JDK compiler below, but the JDK 5.0 there is an automatic disassembly box operation, so in the JDK more than 5.0 of the compiler, the above code can be passed

Reference data type:

Array

String: Strings for storing a string of characters

Java variable declaration and use:

Data type variable name = value, expression;

Example: String name = "Conan";

int a= 50;

Note: "=" is not an "equal sign" in mathematics, but an assignment operator

Java variable naming rules:

1: Must start with a letter, an underscore "_", or a "$" symbol

2: Can include numbers, case sensitive

3: Cannot use the Java language keyword, such as int, class, public, etc.

Six types of operators in Java:

· Arithmetic operators

· Assignment operators

· Relational operators

· logical operators

· Bitwise operators

· Ternary operators

Arithmetic operators:

+: addition operation, the sum of the operands

-: Subtraction operation, to find the difference of the operand

*: multiplication operation, the product of the operand

/: Division operation, operator of operands

%: The remainder of the division of the operand by calculating the residual operation

+ +: Self-increment, operand added 1

--: Self-reduction, 1 of operand

Assignment operators:

=: Assigns the right value to the left, example: int a = 1;

+ =: Left and right side of the sum, example: int a = 1; a+=2; The value of result A is 3

-=: The left side minus the right margin to the left, example: int a =5;a-=2; result A has a value of 3

*=: The value multiplied by both sides is assigned to the left, example: int a = 2;a*=2; result A has a value of 4

/=: To the left by dividing the left by the right side, for example: int a = 6;a/=2; result A has a value of 3

%=: Left divided by the remainder of the right, example: int a =7;a%=2; result A has a value of 1

Relational operators

: greater Than, example: int a = 1;int B = 2; System.out.print (a > B); The result is false

<: less than, example: int a = 1;int B = 2; System.out.print (a < b); The result is true

>=: greater than or equal, example: int a = 1;int B = 2; System.out.print (a >= b); The result is false

<=: less than Equals, example: int a = 1;int B = 2; System.out.print (a <= b); The result is true

= =: equals, example: int a = 1;int B = 2; System.out.print (A = = B); The result is false

! =: Not Equal, example: int a = 1;int B = 2; System.out.print (A! = b); The result is true

The result is a Boolean type, either True or False

logical operators

&&: With, and (short circuit), two conditions are true at the same time, the result is true

|| : Or, or (short circuit), two conditions have a true, the result is true

!: Non, (!+ condition) when the condition is true, the result is false

Data type conversions in Java

1: Automatic data type conversion (magnification conversion)

Satisfy automatic data type conversion conditions:

1) Two types to be compatible: such as numeric type (integer and floating point type)

2) The target type is greater than the source type: for example, int data can be automatically converted to double type

2: Force data type conversion (narrowing conversion)

Precede the variable with parentheses, specifying the type of the strong conversion in parentheses

Example: double A = 40.9;

int b= (int) A;

Note: Casting can lose numeric precision, such as a double type variable a, which is converted to an int type after the value becomes 40

Java is divided into basic data types and reference data types (question: the difference between heap and stack, and what the system does to differentiate stack memory)

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.