What is the way data is passed in Java!

Source: Internet
Author: User

This morning I had a question about Java. The main testing center is the test of Java is worth the way to pass.

What I have summed up in Javaoo is that the underlying data type holds the actual value, the reference data type holds the referenced memory address, and the base data type is passed by value, and the reference data type is passed by address. (I was wrong in the beginning)

The problem is really kind of:

1  Public classsendvalue{2      PublicString str="6";3      Public Static voidMain (string[] args) {4Sendvalue sv=NewSendvalue ();5 Sv.change (SV.STR);6System. out. println (SV.STR);7     }8      Public voidChange (String str) {9Str="Ten";Ten     } One}

The result of the output is:? 6

There is an explanation: The change method inside is to modify the parameters passed to 10, but there is no Sendvalue property str on the line to modify, to modify must indicate this.str= "10", all the answer is 6.

One more question is this:

1  Public classexample{2String str=NewString ("Hello");3     Char[]ch={' A ', ' B '};4      Public Static voidMain (String args[]) {5Example ex=NewExample ();6 Ex.change (ex.str,ex.ch);7System.out.print (ex.str+ "and");8 System.out.print (ex.ch);9     }Ten      Public voidChange (String str,Charch[]) { Onestr= "Test OK"; ACh[0]= ' C '; -     } -}

The answer to the output is:? Hello and CB;

It is explained that the string class is of the final type and cannot inherit and modify the class. str= "Tesk OK", in fact, is implicitly let Java generate a new string object, then it has nothing to do with the original "Hello", when the function ends, str function is finished, so the output is "hello". Char ch[] is a pass-through reference that modifies the original content.

Then I go online to check whether Java is passed by value or by reference, and some say:

http://blog.csdn.net/zzp_403184692/article/details/8184751

Pass by value: Refers to the passed parameter is passed as a copy of the value when the method is called. This means that the copied value is passed, regardless of the previous value.

Reference passing: Refers to when a method is called, the passed parameter is passed by reference, in fact, the address of the reference to pass, that is, the variable corresponding to the memory space address. Both before and after delivery are the same reference.

But others say it is wrong, he says, in Java only by the value of the pass, not by reference!

http://guhanjie.iteye.com/blog/1683637

Please explain it to me, you great gods!

What is the way data is passed in Java!

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.