Directory
[TPC]
Java Data types
There are two types of Java variables, one is the basic data type stored in the stack, and one is a Java object stored in the heap.
There are 9 basic data types, 4 integers, 2 floating-point, 1 Unicode, 1 Boolean, and 1 void types.
The void type is used for the method return value, which indicates no return value.
Basic Type |
size (bytes) |
wrapper type |
Byte |
1 |
Byte |
Short |
2 |
Short |
Int |
4 |
Integer |
Long |
8 |
Long |
Float |
4 |
Float |
Double |
8 |
Double |
Char |
2 |
Character |
Boolean |
-- |
Boolean |
void |
-- |
Void |
Reference
A reference is a commonly seen word that can be understood as a means of manipulating objects. Creating a reference to an object does not represent creating an object.
For example String str
, a reference variable is created for an object, and the variable str
does not currently reference any object.
str="hello"
, at which point the variable refers to the str
"hello"
string object.
A variable of the parent class can refer to the object of the subclass. An interface variable can reference an object that implements a class.
Method parameters
The delivery of method parameters in Java is all passed by value, meaning that the parameters passed to the method are a new copy.
Here's how you want to swap two numbers, but the result is wrong. Because the argument passed to the method is a new copy, the previous value is unchanged.
package com.hnzhrh.fundamental; Import java.util.*; public class test{public static void main (St Ring[] args) {int a=10; int b=5; Swap (A, b); } private static void swap (int a, int b) {int temp=a; A=b; B=temp; } }
The execution of a method relies on the operand stack, the local variable table, and the program counter, and is private to the method. Decompile The above program:
javap -v -p Test.class
To get the bytecode with additional information, the -v
option indicates that the output has additional information, and the -p
option indicates that all classes and members are displayed.
To intercept the main method part and the swap method part of the content:
n public static void main (java.lang.string[]); Descriptor: ([ljava/lang/string;) V Flags:acc_publi C, acc_static code:44 stack=2, locals=3, args_size=1-0:bipush 3:iconst_5 4:istore_2 5:iload_1 6:iload_2 Wuyi 7:invokestatic #2//Method Swap: (II) V 10:return linenumbertable:54 line 7:0 55 Line 8:3-line 9:5-line 10:10-----private static void swap (int, int); Descriptor: (II) V-flags:acc_private, acc_static code:63 stack=1, locals=3, args_size=2 64 0:iload_0 1:istore_2 2:iload_1 3:istore_0 4:iload_2 69 5:istore_1 6:return linenumbertable:72 Line 12:0-line 13:2-Line 1 4:4 Line 15:6
(44 lines start)
Initial state:
The parameter of the main method, args, is stored in the local variable table No. 0 position.
bipush 10
10 go to the operand stack, istore_1
stack top element 10 out of the stack and into the local variable table 1th position.
iconst_5
The constant 5 enters the operand stack, the istore_2
top element of the stack is 5 out of the stack and stored in the local variable table 2nd position.
iload_1
Local variable table 1th in the position of 10 in the stack.
iload_2
Local variable table 2nd in the position of 5 in the stack.
Call the method swap(10,5)
, which has its own operand stack, local variable table, and program counter.
Parameter 10 is stored in the local variable table No. 0 position, and parameter 5 is stored in the local variable table 1th position.
iload_0
The local variable table NO. 0 positions the 10 elements into the stack.
istore_2
The operand stack top element 10 is out of the stack and stored in the local variable table 2nd position.
iload_1
Local variable table 1th position 5 into the stack.
istore_0
The operand stack top element 5 is out of the stack and deposited in the local variable table number No. 0 position.
iload_2
Local variable table 2nd position 10 into the stack.
istore_1
Stack top element 10 is out of the stack and stored in the local variable table 1th position.
return
Method, the method executes, destroys the stack frame, but the swqp
method in the local variable table parameter A and B are exchanged, and the original A and B are still intact.
It is important to note that the element of the static method local variable table No. 0 is the first parameter of the method, and the local variable table No. 0 element of the non-static method is this, that is, the object itself.
Technically, the static method cannot be called through this method.
Java data types and method parameters