The long data type is a 64-bit signed two’s complement integer capable of representing a wide range of values from -9,223,372,036,854,775,808 (-2⁶³) to 9,223,372,036,854,775,807 (2⁶³ - 1).
This data type is used when a wider range than int is needed. The default value for a long variable is 0L.
Syntax:
Source Code:
public class LongRectangle {
public static void main(String[] args) {
// Declaring long variables for the length and width of the rectangle
long length = 50L;
long width = 20L;
// Calculating the area and perimeter of the rectangle
long area = length * width; // Area = length * width
long perimeter = 2 * (length + width); // Perimeter = 2 * (length + width)
// Displaying the results
System.out.println("Length of the rectangle: " + length);
System.out.println("Width of the rectangle: " + width);
System.out.println("Area of the rectangle: " + area);
System.out.println("Perimeter of the rectangle: " + perimeter);
}
}Result:
Watch the video:
Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

