Software Development Exam  >  Software Development Notes  >  Basics of Java  >  Type conversion in Java with Examples

Type conversion in Java with Examples | Basics of Java - Software Development PDF Download

Introduction

Java is a strongly-typed programming language, which means that variables must be declared with a specific data type. However, there are situations where you may need to convert one data type to another. This process is known as type conversion or type casting. In this article, we will explore the different types of type conversion in Java and provide examples and explanations to help beginners understand the concepts.

Implicit Type Conversion (Widening Conversion)

In Java, implicit type conversion occurs when a smaller data type is automatically converted to a larger data type. This conversion is also known as widening conversion and does not require any explicit casting.

Example:

int num = 10;

double result = num;

System.out.println(result);

Output

10.0

Explanation: In the above example, the integer variable num is implicitly converted to a double data type because double can represent a larger range of values than int. The value of num (10) is automatically converted to a double value (10.0) and assigned to the variable result. The output will be 10.0.

Explicit Type Conversion (Narrowing Conversion)

Explicit type conversion, also known as narrowing conversion, occurs when a larger data type is explicitly converted to a smaller data type. This conversion requires the use of casting and may result in data loss or truncation.

Example:

double num = 10.5;

int result = (int) num;

System.out.println(result);

Output

10

Explanation: In the above example, the double variable num is explicitly converted to an integer data type using (int) casting. The fractional part of the double value is truncated, and the integer value (10) is assigned to the variable result. The output will be 10.

String to Primitive Conversion

Java provides methods to convert strings to their corresponding primitive data types. These methods are part of the wrapper classes for primitive types.

Example:

String number = "25";

int result = Integer.parseInt(number);

System.out.println(result);

Output

25

Explanation: In the above example, the string variable number contains the value "25". We use the Integer.parseInt() method to convert this string to an integer value. The converted integer value (25) is assigned to the variable result, and the output will be 25.

Primitive to String Conversion


Java also provides methods to convert primitive data types to their string representation.

Example: 

int number = 50;

String result = Integer.toString(number);

System.out.println(result);

Output

50

Explanation: In the above example, the integer variable number contains the value 50. We use the Integer.toString() method to convert this integer value to its string representation. The converted string value ("50") is assigned to the variable result, and the output will be 50.

Sample Problems with Solutions

Problem 1: Convert a float value to an integer and display the result.

float num = 15.8f;

int result = (int) num;

System.out.println(result);

Output

15

Problem 2: Convert a string "true" to a boolean value and display the result.

String value = "true";

boolean result = Boolean.parseBoolean(value);

System.out.println(result);

Output

true

Conclusion

Type conversion or type casting is an essential concept in Java programming. Implicit type conversion allows for automatic conversion between compatible types, while explicit type conversion requires casting and may involve data loss. Additionally, Java provides methods to convert strings to primitive types and vice versa. By understanding and using these concepts effectively, you can manipulate and transform data within your programs.

The document Type conversion in Java with Examples | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Up next

60 videos|37 docs|12 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Sample Paper

,

Important questions

,

study material

,

Objective type Questions

,

Exam

,

MCQs

,

video lectures

,

mock tests for examination

,

Free

,

shortcuts and tricks

,

past year papers

,

Previous Year Questions with Solutions

,

Type conversion in Java with Examples | Basics of Java - Software Development

,

practice quizzes

,

Type conversion in Java with Examples | Basics of Java - Software Development

,

Summary

,

pdf

,

Semester Notes

,

ppt

,

Type conversion in Java with Examples | Basics of Java - Software Development

,

Viva Questions

,

Extra Questions

;