-1

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?

Linda Lu
  • 19
  • 2
  • 4
    `=` is the [assignment operator](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html), and `val` and `_val` are [variables](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html). If you aren't familiar with these concepts, I'd highly suggest working through all the languages basics tutorials. – azurefrog Nov 16 '21 at 22:30
  • See where it says `public Node(int _val) {`? `_val` means the same thing in `val = _val` that it does in `public Node(int _val) {`: it's a **name** for the value that was passed in. To be quite frank, if this confuses you, then you are many lessons away from understanding the assignment. – Karl Knechtel Nov 16 '21 at 22:31
  • See also - https://stackoverflow.com/questions/1899683. (NOT a duplicate) – Stephen C Nov 16 '21 at 22:34
  • @KarlKnechtel you might be right, but you completely forgot to mention why naming conventions and explicit naming is so important in programming. You know that sometimes even for experienced developers such as you and me, terrible naming can be confusing. Based on my experience, I am pretty sure the source of confusion is the leading underscore in the variable name. I am certain the OP thinks that it carries some deeper meaning (which we know it doesn't). – hfontanez Nov 16 '21 at 22:35
  • 1
    There are languages where that sort of thing could have special meaning, and there are best practice guidelines for those languages that (mandate, forbid, encourage, etc. depending) those kinds of thing. All of that *should still be discussed in a tutorial for the language*, if it's any good. – Karl Knechtel Nov 16 '21 at 22:39
  • 1
    @hfontanez Thank you. Yes, that's what confused me. I thought the leading underscore is for special use or deeper meaning that I should learn. Now I know. – Linda Lu Nov 18 '21 at 19:51

1 Answers1

5

_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.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • @Linda Lu, I would like to add to Louis' answer that the leading underscore DOES NOT carry any hidden meaning. It is just a character. The Java language places restrictions on the characters that can be used to start a variable name, but the underscore is obviously not one of those characters. I suggest you do some research into Java naming rules and conventions. – hfontanez Nov 16 '21 at 22:39
  • 1
    Thank you. I thought the underscore before have a deep meaning or use that I never learned. Then now I know. – Linda Lu Nov 18 '21 at 19:45