Values in Karel

Java has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.

public class Values {
    public static void main(String[] args) {
        // Strings, which can be added together with +.
        System.out.println("java" + "lang");

        // Integers and floats.
        System.out.println("1+1 = " + (1 + 1));
        System.out.println("7.0/3.0 = " + (7.0 / 3.0));

        // Booleans, with boolean operators as you'd expect.
        System.out.println(true && false);
        System.out.println(true || false);
        System.out.println(!true);
    }
}

To run the program, compile it and then use java to execute:

$ javac Values.java
$ java Values
javalang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

In this example, we’ve demonstrated:

  1. String concatenation using the + operator.
  2. Basic arithmetic operations with integers and floating-point numbers.
  3. Boolean operations using logical AND (&&), OR (||), and NOT (!) operators.

Java, like many programming languages, provides these fundamental value types and operations. The System.out.println() method is used to output values to the console, similar to how fmt.Println() is used in other languages.

Note that in Java, we need to wrap arithmetic operations in parentheses when including them in string concatenation (as in "1+1 = " + (1 + 1)). This ensures that the operation is performed before the string concatenation.