Search⌘ K
AI Features

Solution Review: Variable Declaration

Explore variable declaration in Java and understand the ranges and characteristics of primitive data types. Learn how to declare and print variables correctly, reinforcing your knowledge of Java basics for coding challenges.

We'll cover the following...

Solution review

Let’s take a look at the code first:

Java
public class PrimitiveDataTypes {
public static void main(String args[]) {
byte age = 10;
// byte distance = 144; // exceeding range
short height = 30000; // inside range
// short elevation = 40000; // outside range
int distanceBetweenTwoSatellites = 1000000000; // inside range
// int distanceBetweenTwoGalaxy = 10000000000; // outside range
long distanceBetweenTwoGalaxy = 10000000000L; // inside range
float rateOfInterest = 6/5f; // range is beyond the scope of discussion
double rateOfHike = 456/123d; // range is beyond the scope of discussion
char capitalA = 'A';
boolean isPossible = true;
System.out.println("Age is: " + age);
System.out.println("Height is: " + height);
System.out.println("Distance between two satellites is: " + distanceBetweenTwoSatellites);
System.out.println("Distance between two galaxies is: " + distanceBetweenTwoGalaxy);
System.out.println("Rate of interest is: " + rateOfInterest);
System.out.println("Rate of hike is: " + rateOfHike);
System.out.println("CapitalA: " + capitalA);
System.out.println("isPossible: " + isPossible);
}
}

Explanation

The solution is pretty straight forward. The highlighted lines (5, 7, ...