Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:16:24+00:00 2026-06-12T14:16:24+00:00

I know this has been discussed on SO in other posts before and I

  • 0

I know this has been discussed on SO in other posts before and I understand the basic difference between the use of def and val. def is used for defining a method and val for an immutable reference. What I am trying to accomplish by asking this question is to understand if there is something more to def. Can it be used interchangeably with a val?

Recently I tried the following code and cannot convince myself if my present understanding of def is sufficient:

scala> def i: Int = 3
i: Int

scala> i
res2: Int = 3

So I am curious, is this equivalent to val i = 3?

Then I tried this:

scala> i()
<console>:9: error: Int does not take parameters
i()

I did this just to test my understanding of the semantics of def. Now I want to know, when i is a method, why Scala complains with “…does not take parameters”?

Next I tried the following:

scala> def i(): Int = 3
i: ()Int

scala> i()
res4: Int = 3

This time Scala seems to agree that i is a method. So can I use def in place of val interchangeable to declare and initialize a variable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:16:25+00:00Added an answer on June 12, 2026 at 2:16 pm

    Both

    def i = 3
    

    and

    def i() = 3
    

    declare methods. The only difference is, that the first one is a method without a parameter list and the second is a method with an empty parameter list. The former is usually used for methods without side effects and the latter for methods with side effects.
    You should use a val instead of a def if the value never changes and you want to avoid recomputing it. A def gets recomputed every time it is called, while a val is assigned a value only once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been discussed before, and I know this is such
This is my first post. I know this topic has been discussed before in
I know that this has been discussed lot of times. I just can't understand
I know this topic has been discussed before on Stack Overflow. But there are
I know this question has been discussed before, but i am interested in doing
I know this has been discussed a number of times before, but there was
I know this subject has been discussed before but still have problem with refresh
I know that this has been discussed several times in many other topics, blogs,
i know this topic has been discussed here before but i still can't get
I know this has been discussed a lot of times but is there any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.