Bắt đầu ngayBắt đầu miễn phí

Scala is object-oriented

Scala is an object-oriented language because every value is an object and every operation is a method call. This fact means the following lines of code both result in the value 6:

val sumB = 2.+(4)
val sumA = 2 + 4

The same principle applies to the subtraction operator (-), as operators are methods in Scala. In this exercise, you'll experience how Scala is object-oriented by converting code that Scala executes behind-the-scenes to the code you'll normally write in practice.

scala> val symbolAceSpades: String = "A♠"
symbolAceSpades: String = A♠

Bài tập này là một phần của khóa học

Introduction to Scala

Xem khóa học

Hướng dẫn bài tập

  • Rewrite the provided subtraction code in standard arithmetic notation (e.g., 2 + 4).
  • Print the difference using the println function.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

// Calculate the difference between 8 and 5
val difference = 8.-(5)

// Print the difference
println(___)
Chỉnh sửa và Chạy Mã