Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Type Casting and Promotion | Type Conversion and Wrapper Classes
Java Data Types

bookType Casting and Promotion

Understanding how Java handles type conversion is essential for writing safe and predictable code. Java supports two main forms of type conversion between primitive types: implicit conversion (also called widening) and explicit conversion (also called narrowing). Implicit conversions happen automatically when you assign a value of a smaller data type to a larger one, such as assigning an int to a double. Explicit conversions require you to manually cast the value, usually because there is a risk of losing data, such as when converting a double to an int or an int to a byte.

When you assign a value from a smaller type to a larger type, Java performs the conversion automatically. This is safe because the larger type can represent all possible values of the smaller type. Assigning an int to a double does not lose information, since all int values can be represented as double values. This is known as widening conversion.

When assigning a value from a larger type to a smaller type, Java requires you to use an explicit cast. This is because the smaller type might not be able to represent all values of the larger type, which can lead to data loss or unexpected results. Casting a double to an int will truncate the decimal part, and casting an int to a byte can cause the value to wrap around if it is outside the range of a byte. This process is called narrowing conversion.

Main.java

Main.java

copy
1234567891011121314151617
package com.example; public class Main { public static void main(String[] args) { int i = 150; double d = i; // Implicit widening conversion: int to double System.out.println("int to double: " + d); // 150.0 double d2 = 123.456; int i2 = (int) d2; // Explicit narrowing conversion: double to int System.out.println("double to int: " + i2); // 123 int i3 = 300; byte b = (byte) i3; // Explicit narrowing conversion: int to byte System.out.println("int to byte: " + b); // 44 (data loss due to overflow) } }

1. Which of the following primitive type conversions can be performed implicitly in Java?

2. What is the result of casting the value 9.99 (as a double) to an int in Java?

question mark

Which of the following primitive type conversions can be performed implicitly in Java?

Select the correct answer

question mark

What is the result of casting the value 9.99 (as a double) to an int in Java?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you give examples of implicit and explicit type conversions in Java?

What happens if I try to assign a larger type to a smaller type without casting?

Are there any exceptions or errors that can occur during type conversion in Java?

Awesome!

Completion rate improved to 7.14

bookType Casting and Promotion

Stryg for at vise menuen

Understanding how Java handles type conversion is essential for writing safe and predictable code. Java supports two main forms of type conversion between primitive types: implicit conversion (also called widening) and explicit conversion (also called narrowing). Implicit conversions happen automatically when you assign a value of a smaller data type to a larger one, such as assigning an int to a double. Explicit conversions require you to manually cast the value, usually because there is a risk of losing data, such as when converting a double to an int or an int to a byte.

When you assign a value from a smaller type to a larger type, Java performs the conversion automatically. This is safe because the larger type can represent all possible values of the smaller type. Assigning an int to a double does not lose information, since all int values can be represented as double values. This is known as widening conversion.

When assigning a value from a larger type to a smaller type, Java requires you to use an explicit cast. This is because the smaller type might not be able to represent all values of the larger type, which can lead to data loss or unexpected results. Casting a double to an int will truncate the decimal part, and casting an int to a byte can cause the value to wrap around if it is outside the range of a byte. This process is called narrowing conversion.

Main.java

Main.java

copy
1234567891011121314151617
package com.example; public class Main { public static void main(String[] args) { int i = 150; double d = i; // Implicit widening conversion: int to double System.out.println("int to double: " + d); // 150.0 double d2 = 123.456; int i2 = (int) d2; // Explicit narrowing conversion: double to int System.out.println("double to int: " + i2); // 123 int i3 = 300; byte b = (byte) i3; // Explicit narrowing conversion: int to byte System.out.println("int to byte: " + b); // 44 (data loss due to overflow) } }

1. Which of the following primitive type conversions can be performed implicitly in Java?

2. What is the result of casting the value 9.99 (as a double) to an int in Java?

question mark

Which of the following primitive type conversions can be performed implicitly in Java?

Select the correct answer

question mark

What is the result of casting the value 9.99 (as a double) to an int in Java?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1
some-alt