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

What is the value and reference of Java?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what is the value and citation of Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The common saying is: for basic data types (integer, floating-point, character, Boolean, etc.), pass values; for reference types (objects, arrays), pass references. No one has any doubts about the value passed by the basic type; the problem is the reference type.

In order to introduce the main topic, you might as well take a look at the following example. Can you correctly give the running results of the program?

/ * * @ (#) Swap.java * @ author * @ version 1.00 2007-1-5 * / public class Swap {public Swap () {} public static void main (String [] args) {Changer c = new Changer (); String stra = "Mighty" String strb = "Mouse"; c.swap (stra, strb); System.out.println (stra + "" + strb); String [] strArr = new String [2]; strArr [0] = stra; strArr [1] = strb; c.swap (strArr) System.out.println (strArr [0] + "+ strArr [1]);} static class Changer {public

< T >

Void swap (T a, T b) {T temp = a; a = b; b = temp;} public

< T >

Void swap (T [] t) {if (t.length)

< 2 ) { System.out.println( " error! " ); return ; } T temp = t[ 0 ]; t[ 0 ] = t[ 1 ]; t[ 1 ] = temp; } } } 上面程序的正确运行结果为: Mighty Mouse Mouse Mighty 你答对了嘛? 下面我们来分析一下:为什么会出现上面的运行结果? 为分析这个问题,我们必须对程序中的数据在内存中的布局有一定了解。上面main程序中和String相关的变量共有3个,其布局可以用下图所示:

When the swap (stra, strb) function is called, the copy values of the reference types stra and strb are passed, so any changes to the parameters in the function will not affect the values of stra and strb. When swap (strArr) is called, the copy value of strArr is passed, and any change in the parameter in the program still does not affect the value of strArr. However, what is changed in swap (T [] t) is not the value of strArr, but the value of strArr [0] and strArr [1], that is, the value of the object pointed to by the reference type strArr, so the value of strArr [0] and strArr [1] has changed.

From the above analysis, we can draw a conclusion: for reference types, parameters are still passed by value; of course, passing by reference is not entirely unreasonable, but the reference object is not the reference type itself. it's the object that the reference type points to.

This is the end of the content of "what is the value and citation of Java". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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