int

There are no ++ or -- operators in Scala (use += or -=)

In Scala there are no ++ or -- operators. You should instead use the += and -= operators, as shown below. First the += operator:

scala> var i = 1
i: Int = 1

scala> i++
<console>:9: error: value ++ is not a member of Int
              i++
               ^

scala> i += 1

scala> println(i)
2

Next the -= operator:

Scala - How to convert a String to an Int (Integer)

Scala FAQ: How do I convert a String to Int in Scala?

If you need to convert a String to Int in Scala, just use the toInt method which is available on String objects, like this:

scala> val i = "1".toInt
i: Int = 1

As you can see, I just cast a String (the string "1") to an Int object using the toInt method on the String.

However, beware that this can fail just like it does in Java, with a NumberFormatException, like this:

Scala data types (bit sizes, ranges, and docs)

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

A simple Java Random class example

As I was digging around through some code today, I found the following Java Random class example, and thought I'd share it here for anyone needed to see how to work with the Random class in Java. This example shows how to generate a random number in Java that is greater than or equal to 0, and less than 100:

A Java int array example

Java array FAQ: How do you create an array of Java int values (i.e., a Java "int array")?

There are several ways to define an int array in Java; let's take a look at a few examples.

Java int array - Example 1

If you know the desired size of your array, an you'll be adding elements to your array some time later in your code, you can define a Java int array using this syntax:

Java - How to convert a String to a number

Question: How do I convert a String to an int or a float?

Here's a sample Java program that shows how to convert a String to either an int or a float:

A Java String to int conversion example

Here's a little Java code sample that shows how to try to perform a Java String to int conversion. In this example, if the conversion works, the variable pageNum will hold the int version of the pageNumString. If the conversion doesn't work a NumberFormatException will be thrown, and you'll need to deal with that.

Java String to number conversions

Java String to number FAQ: How do I convert a String to a number in Java?

You convert a Java String to a number using the methods of the class whose type you want to convert to. For instance, if you want to try to convert a String to an int, you use the parseInt() method of the Java Integer class, like this: 

A Java String to int conversion example

Java String to int FAQ: How do I convert a String to an int data type in Java?

Answer: You convert a String to an int using the parseInt method of the Java Integer class. The parseInt method converts the String to an int, and throws a NumberFormatException when the String can't be converted to an int type.

Let's take a look at two short examples.

Java int FAQ - How to multiply two integers

Java FAQ: How do I multiple two integers (int) in Java?

Here's a quick example:

int i = 3;
int j = 4;
int k = i * j;
System.out.println("k = " + k);

The value of k here will be 12.

 

Syndicate content