Java data types and method parameters

Source: Internet
Author: User

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.classTo 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 1010 go to the operand stack, istore_1 stack top element 10 out of the stack and into the local variable table 1th position.

iconst_5The 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_1Local variable table 1th in the position of 10 in the stack.
iload_2Local 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_0The local variable table NO. 0 positions the 10 elements into the stack.
istore_2The operand stack top element 10 is out of the stack and stored in the local variable table 2nd position.

iload_1Local variable table 1th position 5 into the stack.
istore_0The operand stack top element 5 is out of the stack and deposited in the local variable table number No. 0 position.

iload_2Local variable table 2nd position 10 into the stack.
istore_1Stack top element 10 is out of the stack and stored in the local variable table 1th position.

returnMethod, 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

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.