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);
}
}
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:
-
int myInt = 9;: Declares an integer variable namedmyIntand assigns it the value9. -
double myDouble = myInt;: Automatically casts the integer value to adouble. Because adoublehas a larger memory footprint and can hold decimals, Java performs this conversion safely behind the scenes without any data loss. -
System.out.println(myInt);: Prints the integer value (9) to the console. -
System.out.println(myDouble);: Prints the converted double value (9.0) to the console.
The output of the program will be:
9
9.0
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 (
inttodoubleorfloat) preserves whole numbers while adding a decimal representation (e.g.,9becomes9.0). However, casting very largelongvalues tofloatordoublecan 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;
References :
https://www.w3schools.com/sql/sql_where.asp
https://www.w3schools.com/java/java_examples.asp
Top comments (0)