Review Java Foundation-java data types

Source: Internet
Author: User
Tags wrapper java reference

1 Java data type. 8 basic types, 3 reference types

Base type (also called primitive type): Byte, short, int, long, char, float, double, boolean

Reference type: Class, interface, array

2 Java Basic data types

There are eight basic types of Java, the basic type can be divided into three categories, character type char, Boolean type Boolean and numeric type Byte, short, int, long, float, double. Numeric types can also be divided into integer types, byte, short, int, long, and floating-point number type float, double. There are no unsigned numeric types in Java, and their range is fixed and will not change with the machine hardware environment or the operating system. In fact, there is another basic type Void in Java, which also has a corresponding wrapper class java.lang.Void, but we can't manipulate them directly.

Basic data types can be divided into four different types

Integer: Byte, short, int, long

Floating point: float, double

Character: Char

Truth Value: Boolean

byte

short

char

long

float

double

boolean

void

size

1 bytes

2 bytes

4 bytes

2 bytes

8 bytes

4 bytes

8 bytes

1 bit

--

wrapper class

Byte

short

integer

character

long

float

double

boolean

void

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

-128~127

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

P align= "left" >-32768~32767

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

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

 

Number of binary digits

8 Guests

16 Guests

32 Guests

16 Guests

64 guests

32 Guests

64 guests

1 guests

--

3 conversion between Java data types

Auto Convert: Low to advanced conversion. For example byte a;int b = A;

Cast: advanced to Low-level conversions (which can result in overflow or loss of precision). For example: int B;short a = (short) b;

Note: High and low here refer to the range of values.

4 Java reference types

4.1 What is a reference type

The reference type (reference type) points to an object, not the original value, and the variable that points to the object is a reference variable:

4.2 Value passing and reference passing

1) passed by value

Refers to when a method call passes a parameter that is passed by a copy of the value. Examples are as follows:

1. public class Temptest {
2. Privatevoid test1 (int a) {
3.//Do something
4. a++;
5.}
6.
7. public static void Main (String args[]) {
8. Temptest t = new temptest ();
9. int a = 3;
T.test1 (a);//The parameter a passed here is passed by value.
System.out.printIn ("a=== in the Main method" + a);
12.}
13.}

Important features by value: A copy of the value is passed, which means that it is not correlated after it is passed. A and 2nd rows of line 9th are two variables, and when you change the value of a in line 2nd, the value of 9th row A is constant, so the print result is 3.

A is 3 in the Main method
A is 4 in the Test1 method

We refer to the 9th row of a as the argument, and a of the 2nd line as the formal parameter, and for the basic data type, the parameter data changes without affecting the data of the argument.

2) passed by reference

When a method is called, the passed parameter is passed by reference, in fact, the address of the reference, that is, the address of the memory space corresponding to the variable.

Examples are as follows:

1. public class Temptest {
2. Privatevoid Test1 (a a) {
3. a.age = 20;
4. System.out.printIn ("age= in the Test1 method" +a.age);
5.}
6. Public staticvoid Main (String args[]) {
7. Temptest t = new temptest ();
8. A = new A ();
9. a.age = 10;
T.test1 (a);//The parameter a passed here is passed by reference
One. System.out.printIn ("age= in the Main method" +a.age);
12.}
13.}
ClassA {
public int: Age = 0;
16.}

   The results of the operation are as follows: Age = 20 in the Test1 method in the Main method

Important features that are passed by reference:

A reference to a value is passed, meaning that both before and after delivery point to the same reference (that is, the same memory space).

To understand correctly the process of passing by reference, you must learn to understand the memory allocation process, and memory allocation can help us to understand the process.

Use the example above to analyze:

(1), run start, run line 8th, create an instance of a, memory allocation is as follows:

A in the Main method



(2), run line 9th, modify the value of age in a instance, memory allocation is as follows:

A in the Main method



(3), run line 10th, is the main method of the variable a refers to the memory space address, by reference to the Test1 method in the A variable. Note: These two a variables are completely different and don't be fooled by the same name, but they point to the same instance of a. The memory allocations are as follows:



(4), Run line 3rd, for the Test1 method in the variable a point to the an instance of the age of the assignment, completed after the formation of a new memory as follows:


The change in the age value of the A instance is caused by the Test1 method at this time.

(5), run line 4th, according to the memory at this time, output test1 method of age=20

(6), run line 11th, according to the memory at this time, output the main method of age=20

3) changes to the above example

Understanding the above example, one might ask, can you let the values passed by reference not affect each other? Is the Test1 method inside the change does not affect the main method inside it?

A new instance of the Test1 method is available. Change into the following example, where the 3rd Act is added:

1. public class Temptest {
2. Privatevoid Test1 (a a) {
3. A = new A ();//newly Added line
4. a.age = 20;
5. System.out.printIn ("age= in the Test1 method" +a.age);
6.}
7. publicstatic void Main (String args[]) {
8. Temptest t = new temptest ();
9. A = new A ();
Ten. A.age = 10;
T.test1 (a);//The parameter a passed here is passed by reference
System.out.printIn ("age= in the Main method" +a.age);
13.}
14.}
ClassA. {
0. Public int.
17.}

The result is: The age=10 in the Age=20 Main method in the Test1 method

Implements whether the values passed by reference pass before and after they are passed, or use memory to understand:

(1), run start, run line 9th, create an instance of a, memory allocation is as follows:
  


(2), running line 10th, is to modify the value of age in a instance, after running memory allocation as follows:
  


(3), run line 11th, is the Mian method in the variable a refers to the memory address, by reference to the Test1 method of a variable. Please note: These two a variables are completely different and should not be blinded by the same name.
  


(4), Run line 3rd, for the Test1 method in the variable a re-generated a new A instance, the completion of the formation of the new memory as follows:



(5), Run line 4th, for the Test1 method in the variable a point to the new a instance of the age of the assignment, completed after the formation of new memory as follows:
  


Note: The age of variable A in the Test1 method is changed at this time, and the A variable in the main method is unchanged.

(6), run line 5th, according to the memory at this time, output test1 method of age=20

(7), run line 12th, according to the memory at this time, output the main method of age=10

Description:

(1), "in Java parameter passing is passed by value" This sentence means: Pass by value is a copy of the value passed, by reference is actually passed the value of the referenced address, so collectively by value passed.

(2), in Java only the basic type and according to the definition of the following string is passed by value, others are passed by reference. Is the string that is defined directly using double quotation marks: string str = "Java Express";

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

Review Java Foundation-java data types

Related Article

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.