← Back to Scala Course | Chapter 2: Variables & Types | Lesson 2 of 8

Int, Double, Float और Long

Int, Double, Float, और Long अलग-अलग size के number boxes हैं: छोटी whole numbers, बड़ी whole numbers, और decimals वाली numbers। आप अपनी number के हिसाब से box चुनते हैं।

In this page:

  1. Int, Double, Float, Long
Syntax
scala
val i: Int = 42
val l: Long = 42L
val d: Double = 3.14
val f: Float = 3.14f

Int, Double, Float, Long

Int एक 32-bit whole number है, Long इसे 64 bits तक बढ़ाता है (literals पर L suffix के साथ), और Double/Float क्रमशः 64-bit और 32-bit floating-point numbers को represent करते हैं (Float literals के लिए f suffix के साथ)।

Double, decimal literals के लिए default type है जब तक specifically Float की मांग न की जाए। सही numeric type चुनना precision और memory usage दोनों के लिए मायने रखता है।

Note: बिना explicit type annotation या suffix के, 3.14 जैसा decimal literal Float नहीं बल्कि Double के रूप में inferred होता है।

उदाहरण: Int, Double, Float, Long

markup
object Main extends App {
  val age: Int = 30
  val population: Long = 8000000000L
  val price: Double = 19.99
  val temperature: Float = 36.6f

  println(s"$age $population $price $temperature")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. val big: Long = 8000000000 जैसी बड़ी number को बिना L के store करना, जिससे integer number too large error आता है।
  2. val f: Float = 36.6 को बिना f के लिखना, जिससे type error आता है क्योंकि 36.6 एक Double है।
  3. 7 / 2 जैसी दो Int values को divide करना और 3.5 की उम्मीद करना, जबकि यह 3 देता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.