Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the comparison between C++ function parameters and Java transfer

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to analyze the C++ function parameters and Java transmission comparison, the editor thinks it is very practical, so share it with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

For the comparison of C++ function parameters with Java passing, I believe this is a headache for many technicians and some developers.

Let's start with Java. Let's start with a few notes:

In Java, there are only two types, namely, the basic type and the object type inherited from Object, and the object type includes String, which cannot change the content once initialized, and BufferString, which can change the content after initialization. Then take a look at the code example:

Public class Test {public static void main (String args []) {Integer interger1, interger2; int I, j; interger1 = new Integer (10); interger2 = new Integer (50); I = 5; j = 9; System.out.println ("Before Swap, Interger1 is" + interger1); System.out.println ("Before Swap, Interger2 is" + interger2); swap (interger1, interger2); System.out.println ("After Swap Interger1 is" + interger1) System.out.println ("After Swap Interger2 is" + interger2); System.out.println ("Before Swap i is" + I); System.out.println ("Before Swap j is" + j); swap (I, j); System.out.println ("After Swap i is" + I); System.out.println ("After Swap j is" + j); StringBuffer sb = new StringBuffer ("I am StringBuffer"); System.out.println ("Before change, sb is") Change (sb); System.out.println ("After change sb is");} public static void swap (Integer ia, Integer ib) {Integer temp = ia; ia = ib; ib = temp;}

This is easy to explain. For basic types such as int, what is passed in is a copy in the "memory unit" that stores int values, so the int inside the function swap and the int outside are not the same thing at all, and of course they cannot be reflected out to affect the outside.

Int. As for the object type, we can also think that the C++ function argument is passed in the "memory unit" a copy that holds the pointer of the object type (although there is no concept of pointer in Java, but this does not prevent us from understanding). In this way, in the swap function, doing anything to the value of the pointer itself certainly does not affect the outer Integer, because the value in the "memory unit" of interger1 and interger2 is the same, and the object type it points to is the same.

Then there is a problem that needs to be explained here, that is, objects of this type StringBuffer. Because its content can be changed, it is obvious that the "pointer" in the change function changes the StringBuffer object itself through a similar "*" operation. (there is only one copy of the StringBuffer object itself) and then say C++, in which the basic types of value transfer such as int are well known, so let's not talk nonsense here.

Then another kind of value passing can be called pointer reference passing (this is similar to the value passing of the object type in Java mentioned above), and you can change the value the pointer points to through the * operation. The example program is as follows, which can be seen at a glance:

# include int main () {void test (int*, const char*); int I = 1; int* iptr = & I; cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report