Java: melewatkan parameter dengan nilai atau referensi

Penjelasan sederhana tentang prinsip-prinsip penerusan parameter di Java.





Banyak programmer sering bingung parameter mana di Java yang diteruskan oleh nilai dan yang mana dengan referensi. Mari kita visualisasikan proses ini, dan kemudian Anda akan melihat betapa sederhananya semuanya.





Mari kita mulai dengan dasar-dasarnya.





Data diteruskan antar metode melalui parameter. Ada dua cara untuk melewatkan parameter:





  1. (by value). . . , .





  2. (by reference). () . , . , , , .





Java :





  1. , , .





  2. — (heap).





: ?





Java





, .





Contoh melewati primitif dengan nilai

Java , processData



. , ( main



) .





:





Transfer objek

? Java , ? , Java - ? , . : "Java ".





, .





Stack dan heap memory
(stack) (heap)

, ,  fruits



processData



. fruitRef



fruit



. fruits



fruitsRef



. . , . , , , .





:





Meneruskan objek dengan referensi
Stack dan heap memory
(stack) (heap)

fruitRef



new



. fruitRef



, , , , , .





, Java . .





.





, .





Menghapus node dalam daftar tertaut

:





class Node {
   int data;
   Node next;
   Node(int d){
       data = d;
       next = null;
   }
}
class LinkedList {
   public static Node push(Node head, int data) {
       Node newNode = new Node(data);
       newNode.next = head;
       head = newNode;
       return head;
   }
   public static void deleteNode(Node head, int position) {
  
       // List is empty
       if (head == null){
           return;
       }

      // If position is 1st, removing head node
      if (position == 1) {
          head = head.next;
          return;
      }
      Node prevNode = head;
      int i = 2;
      while (prevNode != null && i != position) {
          prevNode = prevNode.next;
          i++;
      }
     // When position is more than number of node
     if (prevNode == null || prevNode.next == null) {
         return;
     }
     prevNode.next = prevNode.next.next;
   }
   public static void printList(Node head) {
       Node currNode = head;
       while (currNode != null) {
           System.out.print(currNode.data + " ");
           currNode = currNode.next;
       }
   }
   public static void main(String[] args) {
       Node head = null;
       head = push(head, 5);
       head = push(head, 4);
       head = push(head, 3);
       head = push(head, 2);
       head = push(head, 1);
       System.out.println("Created Linked list is: ");
       printList(head);

       // Delete node at position 2
       deleteNode(head, 2);

       System.out.println("\nLinked List after Deletion at position 2: ");
       printList(head);
   }
}

      
      



, — (Position = 1



). , ? , .





Menghapus simpul pertama dari daftar tertaut tunggal

:





public static Node deleteNode(Node head, int position) {
   // List is empty
   if (head == null){
      return head;
   }

   // If position is 1st, removing head node
   if (position == 1) {
       head = head.next;
       return head;
   }
   Node prevNode = head;
   int i = 2;
   while (prevNode != null && i != position) {
       prevNode = prevNode.next;
       i++;
   }
   // When position is more than number of node
   if (prevNode == null || prevNode.next == null) {
       return head;
   }
   prevNode.next = prevNode.next.next;
   return head;
}
public static void main(String[] args) {
   Node head = null;
   head = push(head, 5);
   head = push(head, 4);
   head = push(head, 3);
   head = push(head, 2);
   head = push(head, 1);
   System.out.println("Created Linked list is: ");
   printList(head);

   // Delete node at position 2
   head = deleteNode(head, 2);

   System.out.println("\nLinked List after Deletion at position 2: ");
   printList(head);
}
//Rest of the code remains same

      
      



, Java: . 






" Oracle Java Programmer (OCAJP)".





, 15 .












All Articles