0

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 ?

package com.acme.pizza

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)
  }

}
Omegaspard
  • 1,828
  • 2
  • 24
  • 52

2 Answers2

2

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.

Mahesh Chand
  • 3,158
  • 19
  • 37
0

In this case, it initializes the variable with the default value depending on its type. You can find more details in documentation under 4.2 Variable Declarations and Definitions.

Here are the default values:

 default  type T
 0        Int or one of its subrange types
 0L       Long
 0.0f     Float
 0.0d     Double
 false    Boolean
 ()       Unit
 null     all other types
fcat
  • 1,251
  • 8
  • 18