DEV Community

Cover image for One Commit at a Time ! Series - 21
Annapoorani Kadhiravan
Annapoorani Kadhiravan

Posted on

One Commit at a Time ! Series - 21

JAVA

public class Main {
  public static void main(String[] args) {
    double myDouble = 9.78d;
    int myInt = (int) myDouble; // Explicit casting: double to int

    System.out.println(myDouble);
    System.out.println(myInt);
  }
}
Enter fullscreen mode Exit fullscreen mode

Explanation :

This Java program demonstrates type casting, specifically converting a double (a floating-point decimal number) into an int (a whole number) using explicit casting.

Here is a step-by-step breakdown:

  1. double myDouble = 9.78d;
  2. Declares a variable named myDouble of type double and assigns it the value 9.78. The d suffix explicitly denotes a double literal (though it is optional in Java).

  3. int myInt = (int) myDouble;

  4. Performs explicit casting (also known as narrowing conversion). Because a double can hold decimals and an int cannot, Java requires you to explicitly tell it to convert the type by putting (int) in front of the variable.

  5. Crucial detail: When casting a floating-point number to an integer, Java truncates the decimal part entirely—it does not round up. Therefore, 9.78 becomes 9.

  6. System.out.println(myDouble);

  7. Prints the original double value to the console: 9.78.

  8. System.out.println(myInt);

  9. Prints the converted integer value to the console: 9.

Output

9.78
9
Enter fullscreen mode Exit fullscreen mode

SQL

Text Fields vs. Numeric Fields
SQL requires single quotes around text values (most database systems will also allow double quotes).

However, numeric fields should not be enclosed in quotes:

Example

SELECT * FROM Customers
WHERE CustomerID = 1;
Enter fullscreen mode Exit fullscreen mode

References

https://www.w3schools.com/sql/sql_where.asp
https://www.w3schools.com/java/java_examples.asp

Top comments (0)