short

Scala String.format error: overloaded method value format with alternatives alvin November 10, 2018 - 3:33pm

I was writing some Scala code like this today:

val sb = new StringBuilder
for (b: Byte <- mdOut.digest) {
    val hexString: String = String.format("%02x", b)
    sb.append(hexString)
}
sb.toString

and encountered this error message:

Scala: How to parse a number from a String alvin July 10, 2017 - 1:25pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 2.1, “Parsing a Number from a String.”

Problem

You want to convert a String to one of Scala’s numeric types (Byte, Double, Int, Float, Long, Short).

How to override Scala's default numeric type when defining a field alvin May 31, 2015 - 3:22pm

Scala FAQ: How do I override the default numeric type that Scala assigns when I declare an Int, Long, Short, Float, or Double val or var field?

Solution

If you assign 1 to a variable, Scala assigns it the type Int:

Scala data types: bit sizes, ranges, and docs

Scala FAQ: What are the Scala numeric data types? How many bits do they use to store their data, and what is the range of those data types?

Courtesy of the excellent book, Programming in Scala, here is a list and description of the Scala data types, including bit sizes and data ranges:

Data Type  Definition

Boolean    true or false

Byte       8-bit signed two's complement integer (-2^7 to 2^7-1, inclusive)
           -128 to 127

Short      16-bit signed two's complement integer (-2^15 to 2^15-1, inclusive)
           32,768 to 32,767

Int        32-bit two's complement integer (-2^31 to 2^31-1, inclusive)
           2,147,483,648 to 2,147,483,647

Long       64-bit two's complement integer (-2^63 to 2^63-1, inclusive)
           -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

Float      32-bit IEEE 754 single-precision float
           1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative)

Double     64-bit IEEE 754 double-precision float
           4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative)

Char       16-bit unsigned Unicode character (0 to 2^16-1, inclusive)
           0 to 65,535

String     a sequence of Chars