Search code examples
javaconstructordefault-constructor

What is the meaning of the leading underline of "val = _val"? Does it have some deep use or deep meaning? In Java


In one algorithm question "Implement Queue by Linked List", the solution shows create the class Node first:

class Node {
    public int val;
    public Node next;
    public Node(int _val) {
        val = _val;
        next = null;
    }
}

May I ask the meaning of the leading underline of val = _val?


Solution

  • _val is just a name. This code is exactly equivalent in every way to

    public Node(int iJustMadeUpAVariableName) {
      val = iJustMadeUpAVariableName;
      next = null;
    }
    

    and it initializes the val field in the Node class to the value passed into the constructor.