Tại sao không có con trỏ trong java và python?

Có rất nhiều mã đẹp, gọn gàng mà bạn có thể viết mà không cần biết về. Nhưng một khi bạn học cách sử dụng sức mạnh của con trỏ, bạn không bao giờ có thể quay lại. Có quá nhiều thứ chỉ có thể được thực hiện với con trỏ. Nhưng với sức mạnh gia tăng đi kèm với trách nhiệm gia tăng. Con trỏ cho phép các loại lỗi mới và xấu xí hơn, đồng thời lỗi con trỏ có thể gặp sự cố theo những cách ngẫu nhiên khiến chúng khó gỡ lỗi hơn. Tuy nhiên, ngay cả với các vấn đề của chúng, con trỏ là một cấu trúc lập trình mạnh mẽ không thể cưỡng lại được.

Con trỏ giải quyết hai vấn đề phần mềm phổ biến. Đầu tiên, con trỏ cho phép các đoạn mã khác nhau chia sẻ thông tin dễ dàng. Bạn có thể nhận được hiệu ứng tương tự bằng cách sao chép thông tin qua lại, nhưng con trỏ giải quyết vấn đề tốt hơn. Thứ hai, con trỏ cho phép các cấu trúc dữ liệu được liên kết phức tạp như và

5. 2. 1. 2. Tài liệu tham khảo là gì?

Java thực sự sử dụng một phiên bản hạn chế của khái niệm con trỏ, được gọi là. Mặc dù chúng có nghĩa gần giống nhau, nhưng thuật ngữ "con trỏ" có xu hướng được sử dụng trong các cuộc thảo luận không dành riêng cho bất kỳ ngôn ngữ hoặc triển khai cụ thể nào. Từ “con trỏ” hàm ý việc triển khai C/C++ phổ biến của con trỏ như hoặc vị trí trong bộ nhớ. Một tham chiếu chỉ “trỏ tới” một đối tượng. Điều này có nghĩa là các lập trình viên được cấp quyền truy cập hạn chế hơn với một tài liệu tham khảo. Mặc dù điều này giới hạn những gì họ có thể làm, nhưng triết lý Java là điều này được bù đắp bằng cơ hội lớn hơn để mã hoạt động chính xác. Các lập trình viên Java chỉ có thể gán cho một tham chiếu và so sánh hai tham chiếu cho bằng nhau. Các cách sử dụng khác của tham chiếu được thực hiện hoàn toàn mà không có sự kiểm soát nào từ lập trình viên. Những hạn chế này làm giảm cơ hội cho các lỗi

5. 2. 2. Các kiểu dữ liệu trong Java

[Nhân tiện, không có từ nào được sử dụng phổ biến cho khái niệm con trỏ—con trỏ chỉ là từ mà chúng tôi sử dụng trong các giải thích này. ]

Quay trở lại những thứ đơn giản như biến

String myName = empRef.name[]
4 và
String myName = empRef.name[]
5 chỉ lưu trữ một giá trị trong hộp. Trong Java, chúng được gọi là. Trong Java, Đối tượng và Mảng là các kiểu dữ liệu không nguyên thủy và chúng luôn được truy cập bằng các tham chiếu. Java tự động sử dụng các tham chiếu đằng sau hậu trường cho các loại phức tạp như vậy và không có cú pháp dành riêng cho tham chiếu [như trong C/C++]. Bạn chỉ cần nhận ra rằng các thao tác gán như
String myName = empRef.name[]
6 sẽ tự động được triển khai với các tham chiếu nếu
String myName = empRef.name[]
7 và
String myName = empRef.name[]
8 là các mảng hoặc đối tượng, khác với hành vi mà bạn nhận được nếu
String myName = empRef.name[]
7 và
String myName = empRef.name[]
8 là các đối tượng nguyên thủy như int. Các phép gán và tham số với mảng và đối tượng về bản chất là nông cạn hoặc dùng chung—, điều này được thảo luận trong phần Nông cạn và. Phần Deep Copy bên dưới

5. 2. 3. Tham chiếu và hủy bỏ tham chiếu

có nghĩa là theo dõi một tham chiếu để lấy giá trị của điểm của nó. Dereferencing

/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
0 trong hình trên trả lại điểm của nó, đối tượng
/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
1. Vì vậy, “dereference” chỉ có nghĩa là truy cập giá trị của pointee. Trực quan, kết quả của một dereference là đối tượng được chỉ bởi mũi tên. Trong Java, điều này thường được thực hiện với toán tử "dấu chấm" để truy cập một trường hoặc phương thức của một đối tượng. Ví dụ

String myName = empRef.name[]

Điều này sẽ hủy đăng ký

/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
0 để gọi phương thức
/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
3 cho đối tượng đó

Hạn chế chính là tài liệu tham khảo phải có một người được chỉ định để truy cập. Rất nhiều lỗi trong mã tham chiếu liên quan đến việc vi phạm một hạn chế đó, dẫn đến sự phổ biến chưa từng có của

/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
4. Một tham chiếu phải được chỉ định một điểm trước khi hoạt động hủy tham chiếu sẽ hoạt động

Hằng số

/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
5 là một giá trị tham chiếu đặc biệt mã hóa ý tưởng “không có điểm nào”. Hóa ra là thuận tiện khi có một giá trị tham chiếu được xác định rõ ràng để thể hiện ý tưởng rằng một tham chiếu không có điểm. Đó là lỗi thời gian chạy khi cố lấy điểm của tham chiếu
/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
5. Trong các bản vẽ, giá trị
/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
5 thường được vẽ dưới dạng đường chéo giữa các góc của hộp biến tham chiếu

5. 2. 4. thay đổiPointeeDataDirect

5. 2. 5. Lớp nhân viên

Chúng ta sẽ sử dụng đối tượng

/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
1 cho rất nhiều ví dụ của mình, vì vậy bây giờ chúng ta hãy giới thiệu chính thức. Gặp lớp
/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
1

/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}

/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}

5. 2. 6. Bài tập tham khảo

Một phép gán [

/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
0] của một tham chiếu đến một tham chiếu khác làm cho chúng trỏ đến cùng một điểm. Đó là một quy tắc đơn giản cho một tình huống có khả năng phức tạp, vì vậy nó đáng để lặp lại. gán một tham chiếu cho một tham chiếu khác làm cho chúng trỏ đến cùng một thứ. Ví dụ dưới đây thêm tham chiếu thứ hai, tên là
/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
1, được gán với câu lệnh

Kết quả là ________ 01 trỏ đến cùng một con trỏ như ________ 00. Trong hình vẽ, điều này có nghĩa là cả hai hộp

/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
1 và
/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
0 đều chứa các mũi tên chỉ vào đối tượng
/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
1. Việc gán giữa các tham chiếu không thay đổi hoặc thậm chí chạm vào các điểm. Nó chỉ thay đổi điểm mà một tham chiếu đề cập đến

Sau khi gán, kiểm tra cho

/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
7 sẽ trả về true

Thao tác gán cũng hoạt động với giá trị

/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
5. Thao tác gán với tham chiếu
/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
5 sao chép giá trị
/**
 * Employee is a class used in this tutorial to explain various pointer concepts
 */
class Employee {
  String name;
  int salary;

  /**
   * class constructor to initialize name and salary fields
   * @param name: employee name
   * @param salary: employee salary
   */
  public Employee[String name, int salary]
  {
    this.name = name;
    this.salary = salary;
  }

  /**
   * getter method for the name field
   * @return the value of name field
   */
  public String getName[]
  {
    return name;
  }

  /**
   * setter method for the name field
   * @param newName the value to be assigned to name field
   */
  public void setName[String newName]
  {
    name = newName;
  }

  /**
   * getter method for the salary field
   * @return the value of salary field
   */
  public int getSalary[]
  {
    return salary;
  }

  /**
   * setter method for the salary field
   * @param newSalary the value to be assigned to salary field
   */
  public void setSalary[int newSalary]
  {
    salary = newSalary;
  }
}
5 từ tham chiếu này sang tham chiếu khác

Bản vẽ bộ nhớ là chìa khóa để suy nghĩ về mã tham chiếu. Khi bạn đang xem mã, hãy nghĩ xem nó sẽ sử dụng bộ nhớ như thế nào trong thời gian chạy, sau đó tạo một bản vẽ nhanh để thực hiện các ý tưởng của bạn. Hướng dẫn này sử dụng rất nhiều hình vẽ để hiển thị cách thức hoạt động của tài liệu tham khảo. Bạn cũng nên

Tại sao con trỏ không được sử dụng trong Python và Java?

Python có xu hướng cố gắng trừu tượng hóa các chi tiết triển khai như địa chỉ bộ nhớ từ người dùng của nó. Python thường tập trung vào khả năng sử dụng thay vì tốc độ . Kết quả là, con trỏ trong Python không thực sự có ý nghĩa. Mặc dù vậy, đừng sợ, theo mặc định, Python cung cấp cho bạn một số lợi ích của việc sử dụng con trỏ.

Tại sao chúng ta không có con trỏ trong Python?

Python không cần con trỏ để đạt được điều này vì mọi biến đều là tham chiếu đến một đối tượng. Các tham chiếu này hơi khác so với các tham chiếu C++, ở chỗ chúng có thể được gán cho - giống như các con trỏ trong C++. Cách xử lý tiêu chuẩn của Python hỗ trợ bạn. Trong python, mọi biến đều là tham chiếu

Tại sao con trỏ không được sử dụng trong các câu hỏi phỏng vấn Java?

Java không sử dụng con trỏ vì chúng không an toàn và làm tăng độ phức tạp của chương trình . Vì Java được biết đến với mã đơn giản, nên việc thêm khái niệm con trỏ sẽ mâu thuẫn.

Cái gì được sử dụng thay vì con trỏ trong Java?

Java sử dụng ý tưởng [an toàn hơn] của tham chiếu thay vì con trỏ. Ngôn ngữ Java không _not_ cung cấp con trỏ. Thay vào đó, tất cả các đối tượng được xử lý bởi các tham chiếu, đừng nhầm lẫn với con trỏ hoặc tham chiếu C++.

Chủ Đề