Why is a copy passed when java values are passed?

  java, question

As we all know, in java, whether it is reference passing or value passing, what is actually passed is only a copy of them, but I don’t understand one thing, why do you want to pass the copy instead of their original value or reference? I’d like to ask the Great God to give me some advice. Thank you very much.

After the big update, the original answer was deleted.
In order to answer the question from Lance_D, I went through the book of java virtual machine again.

1. Look at a code first

public static void main(String args[]){
 char[] c1 = {'a','b'};
 Out. println ("C1:" plus c1[0] plus ""plus c1[1]);
 changer1(c1);
 Out.println ("c1change1after:" plus c1[0] plus ""plus c1[1]);
 changer2(c1);
 Out.println ("c1change2after:" plus c1[0] plus ""plus c1[1]);
 bracket
 
 private static void changer1(char[] c1) {
 char[] c2 = {'e','f'};
 c1 = c2;
 bracket
 
 private static void changer2(char[] c1) {
 char[] c2 = {'e','f'};
 c1[0] = c2[0];
 bracket

Output

c1:a b
 c1 change1 after:a b
 c1 change2 after:e b

Take a look at the real process of memory inside change1
图片描述

Change2 process
图片描述

Change3 process
图片描述

2. Understanding String

The String class in Java is actually the wrapper class of char array. source code:

/** The value is used for character storage. */
 private final char value[];
 
 /** Cache the hash code for the string */
 private int hash;  // Default to 0
 
 public String(String original) {
 this.value = original.value;
 this.hash = original.hash;
 bracket

So whether it isString s1="ab";Or ..String s1=new ("ab");In fact, the real situation is
图片描述
Whens1 = "cd"When
图片描述

3. Review the original data types and reference data types supported in the java Virtual Machine

original data type

  • Numeric type
  • Boolean
  • ReturnAddress type

Numeric type

Byte Type: The value is an 8-bit signed complement of two’s integer, which defaults to zero
Short Type: The value is a 16-bit signed complement of two’s integer, which defaults to zero
Int Type: The value is a 32-bit signed complement of two’s integer, which defaults to zero
Long: The value is a 64-bit signed complement of two’s integer, which defaults to zero
Char type: Unicode code point pointing to Basic Multilingual Plane represented by 16-bit unsigned integer, encoded in UTF-16, with default value of nell code point (‘\u0000’)
Float type and double type are more complicated

Boolean

The value is BooleanTrueAndFalse, the default isFalse

ReturnAddress type

An opcode that represents a bytecode instruction

Reference data type

  • Class type
  • Array type
  • interface type

They are dynamically created by class instances, array instances and class instances that implement an interface

4. Take a more complicated example.

public class Test {
 
 class Student{
 
 public String name;
 public int number;
 
 public Student(String sName, int sNumber){
 name = sName;
 number = sNumber;
 bracket
 
 bracket
 
 public void test(){
 Student s1 = new Student("Jack",1);
 Out.println ("S1:" plus s1.name plus ""plus s1.number ");
 change1(s1);
 Out.println ("S1:" plus s1.name plus ""plus s1.number ");
 change2(s1);
 Out.println ("S1:" plus s1.name plus ""plus s1.number ");
 bracket
 
 
 private void change1(Student s1) {
 s1 = new Student("Mack",2);
 bracket
 
 private void change2(Student s1) {
 s1.name = "Mack";
 s1.number = 2;
 bracket
 
 
 public static void main(String args[]){
 new Test().test();
 bracket
 
 bracket

Output:

s1:Jack 1
 s1:Jack 1
 s1:Mack 2

Actual change1 process:

图片描述

Change2 process:
图片描述