0% found this document useful (0 votes)
2 views2 pages

Java Type Conversions Guide

The document provides a comprehensive guide on Java type conversions, detailing both implicit (widening) and explicit (narrowing) conversions. It includes examples for converting between various data types such as strings, numbers, characters, and arrays. Additionally, it covers methods for printing arrays and converting between integer arrays and Integer objects.

Uploaded by

i66922215
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java Type Conversions Guide

The document provides a comprehensive guide on Java type conversions, detailing both implicit (widening) and explicit (narrowing) conversions. It includes examples for converting between various data types such as strings, numbers, characters, and arrays. Additionally, it covers methods for printing arrays and converting between integer arrays and Integer objects.

Uploaded by

i66922215
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Type Conversions - Complete Guide with

Examples

Implicit Type Conversion (Widening)


byte → short → int → long → float → double
char → int

Example:
int a = 10;
double b = a; // 10.0

Explicit Type Conversion (Narrowing)


double d = 10.75;
int i = (int)d; // 10

String and Number Conversions


String → int : [Link](s)
int → String : [Link](n)
String → long : [Link](s)
long → String : [Link](n)
String → double : [Link](s)
double → String : [Link](d)

Character Conversions
char → int : int x = ch
int → char : (char)x
String → char : [Link](0)
char → String : [Link](ch)

String and Array Conversions


String → char[] : [Link]()
char[] → String : new String(arr)
String → String[] : [Link](" ")
String[] → String : [Link](",", arr)

Array and ArrayList Conversions


Array → ArrayList : new ArrayList<>([Link](arr))
ArrayList → Array : [Link](new String[0])
int[] → ArrayList<Integer> : loop and add
ArrayList<Integer> → int[] : stream().mapToInt(...).toArray()

Array Printing
int[] arr = {1,2,3};
[Link]([Link](arr));

String to int Array


String s = "1 2 3 4";
String[] temp = [Link](" ");
for(...) arr[i] = [Link](temp[i]);

Integer[] and int[] Conversions


int[] → Integer[] : [Link](arr).boxed().toArray(Integer[]::new)
Integer[] → int[] : [Link](arr).mapToInt(Integer::intValue).toArray()

You might also like