基本型とリテラル
数値、ブール値、文字について学びます。
「基本型とリテラル」はCoddyKit上の無料Scala for Backend Engineering & Functional Programmingレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはScala for Backend Engineering & Functional Programming学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Scala for Backend Engineering & Functional Programmingコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Scala's Basic Types
Scala ships with a small set of built-in value types you will use constantly.
Int,Longfor whole numbersDouble,Floatfor decimalsBoolean,Char,String
Each has a literal syntax for writing values directly in code.
Int Literals
A whole number with no decimal point is an Int by default, a 32-bit signed integer.
You can group digits with underscores for readability.
object Main extends App {
val year = 2026
val big = 1_000_000
println(year + big)
}Long Literals
When a number is too large for Int or you simply want 64 bits, add an L suffix to make it a Long.
The capital L is preferred over lowercase l for clarity.
val population: Long = 8_000_000_000L
val id = 42LDouble and Float Literals
A number with a decimal point is a Double by default. Add an f suffix for a Float.
You can also use scientific notation with e.
object Main extends App {
val price = 19.99
val rate = 0.5f
val mass = 6.02e23
println(price)
}Boolean Literals
A Boolean holds exactly one of two literals: true or false.
Booleans drive conditions, comparisons, and logical operators like && and ||.
object Main extends App {
val isReady = true
val isDone = false
println(isReady && !isDone)
}Char Literals
A Char is a single 16-bit Unicode character written in single quotes.
Do not confuse it with a one-letter String, which uses double quotes.
val grade = 'A'
val newline = '\n'
val letter: Char = 'z'String Literals
A String is text written in double quotes. You can join strings with +.
Escape sequences like \n and \t work inside ordinary strings.
object Main extends App {
val first = "Func"
val second = "tional"
println(first + second)
}String Interpolation
Prefix a string with s to embed expressions using $name or ${expr}.
This is cleaner than concatenating with + and is the idiomatic way to build text.
object Main extends App {
val user = "Sam"
val items = 3
println(s"$user has ${items * 2} items")
}Multiline Strings
Triple quotes """...""" create raw multiline strings where escapes are not processed.
They are great for JSON, SQL, or any block of text spanning several lines.
val sql = """SELECT *
FROM users
WHERE active = true"""The Unit Type
Unit is Scala's version of "no useful value", similar to void in Java.
Its single literal is (). Functions that exist only for side effects, like println, return Unit.
object Main extends App {
val nothing: Unit = ()
val result = println("side effect")
println(result)
}Types Form a Hierarchy
All these value types descend from AnyVal, which together with AnyRef sits under Any, the root of every Scala type.
At the bottom, Nothing is a subtype of everything. This unified hierarchy is why expressions always have a type.
Quick Check
Which literal matches which type?
Recap
You met Scala's core value types and their literals.
- Whole numbers:
Int,Long(withL). - Decimals:
Double,Float(withf). Boolean,Char(single quotes),String(double quotes).s"..."interpolation andUnitas the empty value.
よくある質問
「基本型とリテラル」レッスンは無料ですか?
はい。「基本型とリテラル」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Scala for Backend Engineering & Functional Programmingコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Scala for Backend Engineering & Functional Programmingコースには全4レッスンが含まれています。
「基本型とリテラル」で何を学びますか?
数値、ブール値、文字について学びます。 ブラウザで直接実行するハンズオンコードでScala for Backend Engineering & Functional Programmingを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Scala for Backend Engineering & Functional Programmingを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのScala for Backend Engineering & Functional Programmingは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「基本型とリテラル」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このScala for Backend Engineering & Functional Programmingレッスンでコードを書いて実行できますか?
はい。すべてのScala for Backend Engineering & Functional Programmingレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。