0

I was just looking into a Play / scala example with hibernate. I case class I found some thing like this ....

class Buddy(first: String, last: String) {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
var id: Int = _

var firstName: String = first
var lastName: String  = last

def this() = this (null, null)

override def toString = id + " = " + firstName + " " + lastName 
}

Can any one explain me meaning of this line "var id: Int = _".

What the "__" meaning exactly in this code. It not not related with getter method I guess as in this case I guess getter method name will be id_.

Thanks in advance ...

Biswajit
  • 323
  • 4
  • 15

2 Answers2

5

"_" means "default value" Now default value could be different for different data types. For example

default is 0 for Int
default is 0.0 for double
default is null for reference types

so on

In your case the value in 0

Avishek Bhattacharya
  • 6,534
  • 3
  • 34
  • 53
  • One more thing , if possible , what is the requirement of this line "var firstName: String = first" and "var lastName: String = last" ? I think other than overwintering the toString method we do not have any other use of this. In fact in case of toString also we can use "first" and "last", is not is? No need to re initialize again... am I correct or any thing behind that as its is a Entity class in play / JPA... – Biswajit Sep 29 '17 at 08:42
0

Here you have an excellent explanation of what the underscore means and some use cases.

I like to see that like kind of wildcard for some operations.

Example from the link blog:

expr match {
  case List(1,_,_) => " a list with three element and the first element is 1"
  case List(_*)  => " a list with zero or more elements "
  case Map[_,_] => " matches a map with any key type and any value type "
  case _ =>
  }

Another example:

val someList = Seq(1,2,3,4,5)
//Prints every element of the list
someList.foreach(println(_))