100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Scala Operators and Expressions

How Scala treats operators as method calls, covers arithmetic, comparison, and logical operators, and why almost everything in Scala is an expression that returns a value.

FoundationsBeginner8 min readJul 10, 2026
Analogies

Scala Operators and Expressions

In Scala, an expression like 3 + 4 is not a special built-in operation at all — it is shorthand infix syntax for the method call 3.+(4), since Int is itself a class with a + method defined on it. This uniformity extends throughout the language: almost every construct in Scala, including if/else and code blocks wrapped in {}, is an expression that evaluates to a value, rather than a statement that merely performs a side effect the way many constructs do in Java.

🏏

Cricket analogy: In Scala, writing 3 + 4 is really shorthand for calling 3.+(4) — like how a scorer writing '4' on the board is really shorthand for the umpire's raised-arm signal confirming a boundary; and just as every ball bowled produces a countable outcome, every Scala construct, including if blocks, produces a value rather than being a silent statement.

Arithmetic, Comparison, and Logical Operators

Scala provides the familiar arithmetic (+ - * / %), comparison (== != < > <= >=), and logical (&& ||, short-circuiting) operators, but with one important difference from Java: == performs structural (value) equality by default, delegating to .equals, rather than reference equality. This means two separately created case class instances with identical field values are considered ==, whereas checking whether two references point to the exact same object in memory requires the separate eq method.

🏏

Cricket analogy: Scala's == performing structural (value) equality rather than Java-style reference equality is like two identical scorecards from different matches being judged 'equal' if every run, wicket, and over matches exactly — Scala compares the actual runs recorded, not which physical scorebook they're written in.

Operators Are Just Methods

Because operators are methods, you can define your own on any class you write — for example, a Vector2D case class can implement def +(other: Vector2D): Vector2D so that v1 + v2 reads naturally while actually invoking your custom addition logic. This is a genuine language feature, not a hack, but it comes with a responsibility: an overloaded operator's behavior should map intuitively to what a reader would expect from that symbol, or the resulting code becomes cryptic rather than clear.

🏏

Cricket analogy: Defining a custom + operator on a Scala class, such as def +(other: Score): Score, is like the ICC formally defining what it means to 'add' two innings' worth of Duckworth-Lewis-adjusted par scores — you're specifying precisely what combination means for your own custom type, not just relying on plain arithmetic.

Expressions vs Statements: if and Blocks Return Values

Because if/else is an expression, val result = if (x > 0) "positive" else "non-positive" is perfectly valid Scala — the whole conditional evaluates to whichever branch's value matches, and can be assigned directly to a val. The same applies to blocks: { statement1; statement2; finalExpr } evaluates to finalExpr, the value of its last line. This is precisely why Scala has no separate ternary operator (?:) the way Java does — if/else already fills that role while also working as a full multi-line conditional.

🏏

Cricket analogy: In Scala, val result = if (runs > 200) "Strong total" else "Below par" treats if as an expression that yields a value directly, much like a match referee's on-field decision immediately produces a result (out or not out) that's recorded on the scorecard, rather than the decision existing separately from the outcome.

scala
// Operators are just methods written in infix notation
val sum = 3 + 4        // desugars to: 3.+(4)
val isEqual = sum == 7 // desugars to: sum.==(7)  -> structural equality

// Comparison and logical operators
val isAdult = age >= 18 && hasId

// Custom operator defined on a case class
case class Vector2D(x: Double, y: Double) {
  def +(other: Vector2D): Vector2D = Vector2D(x + other.x, y + other.y)
  def *(scalar: Double): Vector2D = Vector2D(x * scalar, y * scalar)
}

val v1 = Vector2D(1.0, 2.0)
val v2 = Vector2D(3.0, 4.0)
val v3 = v1 + v2          // Vector2D(4.0, 6.0), calls v1.+(v2)

// if is an expression: it evaluates to a value
val label: String =
  if (v3.x > 3.0) "far right"
  else "near origin"

println(label)

Scala's == performs structural equality by default (delegating to .equals), which is almost always what you want — two case class instances with identical field values are ==, even if they're different objects in memory. If you specifically need reference equality (are they the literal same object?), use eq instead.

Just because Scala lets you define operators like +, -, or even custom symbols like <+> on your own types doesn't mean you always should. Overusing symbolic operator names for non-obvious operations makes code harder to read for anyone unfamiliar with your API — reserve operator overloading for cases where the symbol's meaning is genuinely intuitive, like arithmetic on a Vector2D or Money type.

  • In Scala, operators like + and - are ordinary method calls written in infix notation: a + b means a.+(b).
  • Because everything is a method call, you can define custom operators on your own classes.
  • Scala's == performs structural (value) equality by default, unlike Java's reference-comparing ==; use eq for reference equality.
  • if/else in Scala is an expression that returns a value, so it can be assigned directly to a val.
  • Blocks {} evaluate to the value of their last expression, which is why Scala has no separate ternary operator.
  • Logical operators && and || short-circuit, just as in most C-family languages.
  • Overloading symbolic operators should be reserved for cases where the meaning is genuinely intuitive to readers.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#ScalaStudyNotes#ScalaOperatorsAndExpressions#Scala#Operators#Expressions#Arithmetic#StudyNotes#SkillVeris#ExamPrep

Frequently Asked Questions

21 categories · pick one to explore

Where can I get free study notes for programming and tech subjects?
SkillVeris offers completely free study notes covering programming and tech subjects, with no signup fees or paywalls. The notes are structured by course and topic, written for quick understanding, and enriched with the Learn Through Hobbies analogy method, so you can revise concepts through cricket, music, gaming, cooking and more.
Are SkillVeris study notes good for exam revision?
Yes, the study notes are designed for efficient revision: each topic answers its heading immediately, keeps explanations concise, and links to related glossary terms and cheat sheets. Students preparing for university exams or certification tests use them as quick revision notes because they distil concepts without the padding of full textbooks.
What subjects do the free study notes cover?
The study notes span the platform's main domains, including AI and machine learning, Python and programming, web development, DevOps, cloud, security and databases. Coverage mirrors the 37 live courses, so notes exist for the topics you are actually studying, and new note sets are added as courses launch.
How are SkillVeris study notes different from regular textbooks?
The notes are answer-first, concise and free, whereas textbooks are long and often expensive. Each section explains one concept directly, then reinforces it through selectable hobby analogies like cricket or cooking. Notes also cross-link to the glossary, blog and cheat sheets, letting you jump to related material instantly instead of flipping pages.
Can I use the developer study material without creating an account?
The study notes are free to access, and SkillVeris does not charge anything for its developer study material at any point. Browsing notes is straightforward from the Study Notes section, and if you want progress tracking, certificates and AI Mentor conversations tied to your learning, a free account unlocks those extras.
Do the study notes explain concepts with analogies?
Yes, this is a signature SkillVeris feature. Study notes use the Learn Through Hobbies method, explaining technical concepts through analogies from twelve domains including cricket, music, gaming, photography, travel, movies, fitness, chess, cooking, finance, business and sports. You can switch the analogy domain instantly to whichever hobby makes the concept click.
Are the revision notes suitable for last-minute exam preparation?
Yes, revision notes on SkillVeris work well for last-minute preparation because every section states the answer in its first sentences, so skimming is genuinely effective. Pair them with the relevant cheat sheet for formulas and syntax, and use the glossary for any unfamiliar term you meet while cramming.
Is there free study material for AI and machine learning?
Yes, SkillVeris provides free study notes across its AI and ML catalogue, covering Python for AI, deep learning frameworks like PyTorch and TensorFlow, Hugging Face Transformers, Large Language Models, RAG, AI agents and MLOps. All of it is free, making it a strong resource for Indian students and global learners alike.
Can beginners understand the study notes, or are they for experts?
Beginners can absolutely use them. The notes are written in plain language, define terms as they appear, and lean on hobby analogies to make abstract ideas concrete. Difficulty scales with the underlying course level, so beginner-course notes stay gentle while advanced-course notes go deeper, and the glossary supports you throughout.
How do study notes connect with SkillVeris courses?
Study notes are organised by course and topic, so they map directly to the structured courses and their 24–40-lesson curriculum. Many learners study a lesson first, then use the matching notes for revision before module assessments and the final exam, where 80 percent is required to pass and earn the certificate.
Are there study notes for Python specifically?
Yes, Python is well covered through notes tied to the Python-focused courses, including Python for AI and ML. Topics span fundamentals through applied machine learning usage. You can reinforce the notes with Python practice in Code Lab, which runs code in your browser with no installation required.
Do the study notes include code examples?
Yes, study notes include code examples wherever a concept is best shown in code, alongside explanations, key points and analogies. Reading a snippet in the notes and then reproducing it yourself in Code Lab is an effective loop, since Code Lab lets you run code in the browser across six languages.
How often is new study material added to SkillVeris?
Study material grows alongside the course catalogue. Whenever new courses join the platform's 37 live courses, matching study notes, glossary entries and cheat sheets are added so the resources stay in sync. Existing notes are also refined over time, so it is worth revisiting topics you studied earlier.
Can I use SkillVeris notes to prepare for technical interviews?
Yes, the notes make excellent interview revision because they compress each concept into direct, answer-first explanations, which mirrors how you should answer interview questions. Combine them with the SkillVeris interview questions feature, which includes readiness scoring, to test whether your revision has actually made you interview-ready.
Are the study notes mobile-friendly for studying on the go?
Yes, the study notes are built to load fast and read comfortably on mobile devices, so you can revise during a commute or between classes. Sections are short and answer-first, which suits small screens, and analogy switching works on mobile too, letting you study anywhere without carrying books.
What is the difference between study notes and cheat sheets?
Study notes explain concepts in depth with context, examples and analogies, making them ideal for learning and revision. Cheat sheets are compact quick-reference summaries of syntax, commands and key facts, ideal once you already understand a topic. Most learners study the notes first, then keep the cheat sheet handy while coding.
Do study notes help if I am stuck on a course lesson?
Yes, reading the matching study notes often clarifies a lesson because the same concept is explained from a different angle, frequently with a different analogy. If you are still stuck, ask the AI Mentor, which answers 24/7 at Quick, Detailed or Deep-dive depth until the idea genuinely makes sense.
Is there free study material for DevOps and cloud topics?
Yes, SkillVeris carries free study notes for DevOps and cloud topics as part of its coverage across 37 live courses. The material suits learners following the DevOps Engineer or Cloud Engineer paths, and it links to related glossary terms and cheat sheets so you can revise the whole toolchain in one place.
Can school or college students in India use these notes for projects?
Yes, students across India and worldwide use SkillVeris notes for coursework, projects and exam preparation, and everything is free, which matters for student budgets. The notes explain concepts clearly enough to cite in project reports, and Code Lab lets you prototype the project code directly in your browser.
How should I combine study notes with other SkillVeris resources?
A proven loop: learn from a course lesson, revise with the matching study notes, look up unfamiliar terms in the glossary, keep the cheat sheet open while practising in Code Lab, and quiz yourself with interview questions. The AI Mentor fills any remaining gaps 24/7, at whatever depth you need.

What Learners Say

Real journeys from the SkillVeris community — swipe for more.

SkillVeris taught me Python through Cricket. Now I’m building real projects and feeling confident!
Arjun S. · B.Tech Student
The best platform for hobby-based learning. Concepts finally stick.
Priya R. · Data Analyst
I went from zero coding to a portfolio of projects — all by learning through my love for gaming. Landed my first internship!
Kabir M. · CS Undergraduate
Trending Topics50 popular tags — tap to explore
Trending CoursesAll 37 free courses — tap to browse