DEV Community

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

Posted on

One Commit at a Time ! Series - 20

JAVA

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

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

Explanation :

This program demonstrates widening casting (also known as implicit casting or type promotion) in Java, where a smaller primitive data type is automatically converted into a larger primitive data type.

Step-by-step breakdown:

  1. int myInt = 9;: Declares an integer variable named myInt and assigns it the value 9.
  2. double myDouble = myInt;: Automatically casts the integer value to a double. Because a double has a larger memory footprint and can hold decimals, Java performs this conversion safely behind the scenes without any data loss.
  3. System.out.println(myInt);: Prints the integer value (9) to the console.
  4. System.out.println(myDouble);: Prints the converted double value (9.0) to the console.

The output of the program will be:

9
9.0

Enter fullscreen mode Exit fullscreen mode

Widening Casting in Detail

Widening casting happens when you assign a value of a smaller data type to a variable of a larger data type. In Java, this process is automatic (implicit) because there is no risk of losing magnitude or precision during the conversion.

The Java Primitive Type Hierarchy

To understand why casting is necessary or automatic, look at the size hierarchy of Java's numeric primitive types (from smallest to largest):

$$\text{byte} \rightarrow \text{short} \rightarrow \text{int} \rightarrow \text{long} \rightarrow \text{float} \rightarrow \text{double}$$

(Note: char behaves like a 16-bit unsigned integer and can also be widened to int or larger types).

Key Characteristics of Widening Casting

  • Safe Conversion: Since the target type has an equal or greater capacity than the source type, the original value fits completely within the new memory allocation.
  • No Explicit Syntax Required: Unlike narrowing casting (which requires manual casting syntax like (int) myDouble), widening happens implicitly. You do not need to write type cast operators.
  • Precision Preservation: Converting integers to floating-point numbers (int to double or float) preserves whole numbers while adding a decimal representation (e.g., 9 becomes 9.0). However, casting very large long values to float or double can occasionally result in a minor loss of fractional precision because of how floating-point numbers are stored in memory, though the magnitude remains accurate.

SQL

Operators in The WHERE Clause
You can use other operators than the = operator to filter the search.

Example
Select all customers with a CustomerID greater than 80:

SELECT * FROM Customers
WHERE CustomerID > 80;
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)