Bob Reynolds Bob Reynolds - 5 months ago 29
Scala Question

Scala what means the place older _ in var declaration

I was reading the guide to use Junit with scala on this site
And there is line of code I don't understand in the code lines I copy pasted at the end of this message.

The code line is :

var pizza: Pizza = _

I know that the place holder is used in the pattern matching to say "if it's anything else do this". But I don't understand what it means hear.

Can someone explain ?


import org.junit.Test
import junit.framework.TestCase
import org.junit.Assert._
class PizzaTests extends TestCase {

var pizza: Pizza = _

override def setUp {
pizza = new Pizza

def testOneTopping {
pizza.addTopping(Topping("green olives"))
assertEquals(pizza.getToppings.size, 1)

def testAddingAndRemovingToppings {
pizza.addTopping(Topping("green olives"))
pizza.removeTopping(Topping("green olives"))
assertEquals(pizza.getToppings.size, 0)


Answer Source

It means assign default value to it. For example,

scala> var num:Int = _
num: Int = 0

scala> var name:String = _
name: String = null

When i declare an integer value,it assigns 0 as default value to it and null as default value to a string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download