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!
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.
To create a new variable in Java,
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
Similarly, use the class DeclareDatatype that is provided below,
public class DeclareDatatype {
public static void main(String[] args) {
// Enter your code here
}
}
First,
Secondly,
Finally,
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);
}
}