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 pass values and arrays by java

2025-03-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the knowledge of "how to pass values and arrays in java". In the operation of actual cases, many people will encounter this 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!

Public class Test {String str = new String ("good"); char [] ch = {'aqiang test ok; int I = 10; public void change (String str,char [] ch,int I) {str = "test ok"; / / = new String ("test ok") ch [0] = 'glossy; iSense + } public static void main (String [] args) {Test tt = new Test (); System.out.println (tt.i); System.out.println (tt.str); System.out.println (tt.ch); System.out.println ("-"); tt.change (tt.str,tt.ch,tt.i) System.out.println (tt.i); System.out.println (tt.str); System.out.println (tt.ch);} 10goodabc-10goodgbc # here a-> g

Str is a reference to the String type; I is a basic type variable; ch is an array name and a reference to an array object

In the change () method, str= "test ok" is a new object that puts the first address on the reference variable str; and ch [0] ='g; because you pass a reference to the array, ch [0] ='g; it is an operation on the array element and can modify the contents of the source array

I is an integer value, but gives a copy of the value to the method, and the change in the method does not change the source I.

If an element of a single primitive type array is passed to a method and modified in the method, the modified value is not stored in the element at the end of execution of the called method, because the element is passed by value. If you pass a reference to the array, subsequent modifications to the array elements can be reflected in the original array

Package com.lifeibigdata.algorithms.blog;/** * Created by lifei on 16-6-22. * / public class Test {String str = new String ("good"); char [] ch = {'aqiao ch; int I = 10; the input parameter char [] ch in the public void change (String str,char ch,int I) {/ / change () method becomes-char ch str = "test ok"; ch =' galley; this.i = iTun1 / / this.i = new Test 1; the I to the left of the equal sign is attribute I, and the I to the right of the equal sign is the local variable (I in the parameter)} public static void main (String [] args) {Test tt = new Test (); System.out.println (tt.i); System.out.println (tt.str); System.out.println (tt.ch) System.out.println ("-"); tt.change (tt.str,tt.ch [0], tt.i); / / this time a single array element of char value is passed, parsing System.out.println (tt.i); System.out.println (tt.str); System.out.println (tt.ch) as above }} 10goodabc-11goodabc

When an object is passed as a parameter in Java, the address of the object in memory is copied and passed to the parameter. In this case, the object and the parameter point to the same address, and the data changes together. If in this process, if you change the address saved in the parameter, then the object and the parameter no longer point to the same address. If you change the value pointed to by the address in the parameter, then the parameters in the object will not change.

This is the end of "how to pass values and arrays in java". Thank you for 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

Servers

Wechat

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

12
Report