Review of java basics-Java data types, review of java Data Types

Source: Internet
Author: User
Tags java reference

Review of java basics-Java data types, review of java Data Types

1. java data type. 8 Basic Types and 3 reference types

Basic type (also called original type): byte, short, int, long, char, float, double, boolean

Reference Type: Class, interface, array

2. Basic java Data Types

Java has eight basic types: char, boolean, byte, short, int, long, float, and double. The numeric types include byte, short, int, long, float, and double. The value types in JAVA do not have unsigned values. Their value ranges are fixed and will not change as the hardware environment or operating system of the machine changes. In fact, there is another basic type of void in JAVA, which also has the corresponding packaging class java. lang. Void, but we cannot directly operate on them.

Basic data types can be dividedThuType

Integer: byte, short, int, long

Floating Point: float and double

Character: char

True Value: boolean

Type

Byte

Short

Int

Char

Long

Float

Double

Boolean

Void

Size

1 byte

2 bytes

4 bytes

2 bytes

8 bytes

4 bytes

8 bytes

1 digit

--

Package class

Byte

Short

Integer

Character

Long

Float

Double

Boolean

Void

Value Range

[-2 ^ 7, 2 ^ 7-1]

-128 ~ 127

[-2 ^ 15,2 ^ 15-1]

-32768 ~ 32767

[-2 ^ 31,2 ^ 31-1]

Stores Unicode codes, which are assigned with single quotes.

[-2 ^ 63,2 ^ 63-1]

The data range is 3.4e-45 ~ 1.4e38

The data range is 4.9e-324 ~ 1.8e308

Only valid values: true and false.

 

Binary digits

8-digit

16-bit

32-bit

16-bit

64-bit

32-bit

64-bit

1 digit

--

 

3 Conversion between java Data Types

Automatic conversion: low-level to advanced conversion. For example, byte a; int B =;

Forced conversion: advanced to low-level conversion (may cause overflow or loss of precision ). Example: int B; short a = (short) B;

Note: The value range is high or low.

4. java reference type

4.1 what is a reference type

The reference type points to an object, not the original value. The variable that points to the object refers to the referenced variable ..

 

4.2 value transfer and reference Transfer

1) Pass by value

It indicates that when a method is called, The transmitted parameters are copied and transmitted by value. Example:

1. public class TempTest {
2. privatevoid test1 (int ){
3. // do something
4. a ++;
5 .}
6.
7. public static void main (String args []) {
8. TempTest t = new TempTest ();
9. int a = 3;
10. t. test1 (a); // here, parameter a is passed by value.
11. System. out. printIn ("a =" + a) in the main method );
12 .}
13 .}

An important feature of passing by value: It transfers the copy of the value, that is, the transfer is irrelevant. The values of row a of row 9th and row a of row 2nd are two variables. When the value of row a of row 2nd is changed, the value of row a of row 9th remains unchanged, so the result is 3.

In the main method, a is 3.
In test1, a is 4.

We call row a of Row 1 as a real parameter, and row a of Row 2 as a form parameter. Changes to the basic data type and data of the form parameter do not affect the data of the real parameter.

2) Passed by reference

It indicates that when a method is called, The passed parameters are passed by reference. In fact, the referenced address is transmitted, that is, the address of the memory space corresponding to the variable.

Example:

1. public class TempTest {
2. privatevoid test1 (A ){
3. a. age = 20;
4. System. out. printIn ("age =" + a. age in test1 );
5 .}
6. public staticvoid main (String args []) {
7. TempTest t = new TempTest ();
8. A a = new ();
9. a. age = 10;
10. t. test1 (a); // The parameter a passed here is passed by reference.
11. System. out. printIn ("age =" + a. age in the main method );
12 .}
13 .}
14. classA {
15. public int age = 0;
16 .}

The running result is as follows: age = 20 in the main method of test1

Important features of transferring by reference:

The value reference is passed, that is, both before and after the transfer point to the same reference (that is, the same memory space ).

To correctly understand the process of transferring by reference, you must understand the process of memory allocation. memory allocation can help us understand this process.

The following example is used for analysis:

(1) When the operation starts, run row 8th and create an instance named A. The memory allocation is as follows:

A in main method



(2) Run row 9th and modify the age value in instance A. The memory allocation is as follows:

A in main method

 
 
(3) Run row 10th to pass the memory space address referenced by variable a in the main method to the variable in the test1 method according to the reference. Note: These two a variables are completely different. Do not be blinded by the same name, but they point to the same A instance. The memory allocation is as follows:



(4) Run row 3rd and assign a value to the age in the test1 method when variable A points to instance a. The new memory is formed as follows:


In this case, the age value of instance A is changed by the test1 method.

(5) Run 4th rows. Based on the memory at this time, output age = 20 in the test1 method.

(6) run the 11th rows. Based on the memory at this time, the age = 20 in the main method is output.

3) Changes to the above example

After understanding the above example, someone may ask, can we keep the value passed by reference without affecting each other? Is the modification in the test1 method not affecting the content in the main method?

The method is to create a new instance in the test1 method. Change to the following example, of which 3rd behavior is newly added:

1. public class TempTest {
2. privatevoid test1 (A ){
3. a = new A (); // a new row
4. a. age = 20;
5. System. out. printIn ("age =" + a. age in test1 );
6 .}
7. publicstatic void main (String args []) {
8. TempTest t = new TempTest ();
9. A a = new ();
10. a. age = 10;
11. t. test1 (a); // The parameter a passed here is passed by reference.
12. System. out. printIn ("age =" + a. age in the main method );
13 .}
14 .}
15. classA {
16. public int age = 0;
17 .}

The running result is: age = 20 in the test1 method and age = 10 in the main method.

It does not affect each other before and after passing the value passed by reference. Let's take a look at the memory:

(1) When the operation starts, run row 9th and create an instance A. The memory allocation is as follows:
  


(2) Run row 10th to modify the age value of instance A. The memory allocation after running is as follows:
  


(3) Run row 11th to pass the memory address referenced by variable a in the mian method to the a variable in the test1 method according to the reference. Note: These two a variables are completely different and should not be blinded by the same name.
  


(4) Run row 3rd to generate a new A instance for variable a in the test1 method. The new memory is formed as follows:



(5) Run row 4th and assign a value to the age of the new A instance pointed to by variable a in the test1 method. After completion, A new memory is formed as follows:
  


Note: The age of variable a in test1 is changed, but the variable in main is not changed.

(6) Run 5th rows. Based on the memory at this time, output age = 20 in the test1 method.

(7) run the 12th rows. Based on the memory at this time, the age = 10 in the main method is output.

Note:

(1) "parameter passing in Java is passed by value" means that passing by value is a copy of the passed value, passing by reference actually transfers the referenced address value, so it is collectively referred to as passing by value.

(2) in Java, only the basic type and String defined in the following way are passed by value, while others are passed by reference. It is a String defined by double quotation marks: String str = "Java Express ";

 

Reproduced to: http://blog.sina.com.cn/s/blog_7fb1495b01012sfn.html

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.