Exercises with Solutions – Data Types in Java

Data types in Java

We have looked at how data types work in Java! We have learned what the most common data types in Java are. In particular, we have looked at integers (int), text strings (string), floating-point (double), and logical values (booleans). Additionally, we have looked at how to declare variables and assign values to them. Finally, we have learned how to use mathematical operations in Java, as well as how to illustrate their calculations with the built-in Scanner function.

Do you need to repeat some parts before we start?

Now, let’s try what we have learned by doing a few simple exercises!

Exercise 1: Assign values to variables in Java

First, use the class DeclareDatatype that is provided below,

public class DeclareDatatype {
    public static void main(String[] args) {

       // Enter your code here
    }
}

Secondly,  declare and assign variables of the following different data types.

  • Declare a variable named value_int of the data type int that is assigned the value 20
  • Secondly, declare a variable named value_string of the data type string that is assigned the value “Java”
  • Third, create a variable named value_double of the data type double that is assigned the value 23.63
  • Finally, print all variables in the console.

To create a new variable in Java,

  • First specify the data type followed by the variable name
  • Then assign the value equal to the “=” operator
  • Don’t forget the semicolon at the end
public class DeclareDatatype {
    public static void main(String[] args) {

        // Assigns the value 20 to the variable value_int of type int
        int value_int = 20;       

        // Assigns the value "Java" to value_string  
        String value_string = "Java";       

        // Assigns the value 23.63 to the value type double value_double
        double value_double = 23.63;        

        // Print the variables
        System.out.println(value_int);      
        System.out.println(value_string);   
        System.out.println(value_double);   
    }
}

Resulting in,

20 
Java 
23.63

Exercise 2: Assign values to variables and convert data type

Similarly, use the class DeclareDatatype that is provided below,

public class DeclareDatatype {
    public static void main(String[] args) {

       // Enter your code here
    }
}

First,

  • Declare a variable named value of the data type int that is assigned the value 50
  • Secondly, declare a variable named another_value of the data type double that is assigned the value 30.2

Secondly,

  • Convert the variable value from data type int to data type double. Save the new variable to a new variable value_double

Finally,

  • Add the variable value and another_value and assign the value to a new variable which is assigned the name sum_value of type double.
  • Print the result
  • We easily create variables by first specifying the data type, followed by the variable name and the value we want to assign to the variable.
  • We can convert an int to a double by creating a new variable of the data type double.
  • Summing up is easy with the plus operator +
public class DeclareDatatype {
    public static void main(String[] args) {

        //First, declaring the variables 
        int value = 50;                                     
        double another_value = 30.2;                       

        // Convert the variable value to a double
        double value_double = value;   

        // Finally, Sum the variables                      
        double sum_value = value_double + another_value;  

        // Print the result  
        System.out.println(sum_value);                      
    }
}
Previous Page    |    Next Page