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

Java All Conversions With Examples

The document provides a comprehensive guide on Java type conversions, detailing both implicit and explicit conversions with examples and outputs. It covers conversions between various data types including int, double, String, char, and arrays, demonstrating how to convert from one type to another and vice versa. The guide also includes practical code snippets to illustrate each conversion process.

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 views4 pages

Java All Conversions With Examples

The document provides a comprehensive guide on Java type conversions, detailing both implicit and explicit conversions with examples and outputs. It covers conversions between various data types including int, double, String, char, and arrays, demonstrating how to convert from one type to another and vice versa. The guide also includes practical code snippets to illustrate each conversion process.

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 & Output


Implicit Conversion
int a = 10;
double b = a;
[Link](b);

Output: 10.0

Explicit Conversion
double d = 10.75;
int i = (int)d;
[Link](i);

Output: 10

String to int
String s = "123";
int n = [Link](s);
[Link](n);

Output: 123

int to String
int n = 123;
String s = [Link](n);
[Link](s);

Output: 123

String to long
String s = "1000";
long n = [Link](s);

Output: 1000

long to String
long n = 1000;
String s = [Link](n);

Output: 1000
String to double
String s = "12.5";
double d = [Link](s);

Output: 12.5

double to String
double d = 12.5;
String s = [Link](d);

Output: 12.5

char to int
char ch = 'A';
int x = ch;
[Link](x);

Output: 65

int to char
int x = 65;
char ch = (char)x;
[Link](ch);

Output: A

String to char
String s = "Hello";
char ch = [Link](0);

Output: H

char to String
char ch = 'A';
String s = [Link](ch);

Output: A

String to char[]
String s = "Hello";
char[] arr = [Link]();
char[] to String
char[] arr = {'H','e','l','l','o'};
String s = new String(arr);

Output: Hello

String to String[]
String s = "Java Python C";
String[] arr = [Link](" ");

String[] to String
String[] arr = {"Java","Python","C"};
String s = [Link](",", arr);

Output: Java,Python,C

int[] to String
int[] arr = {1,2,3};
[Link]([Link](arr));

Output: [1, 2, 3]

Array to ArrayList
String[] arr = {"A","B","C"};
ArrayList list =
new ArrayList<>([Link](arr));

ArrayList to Array
String[] arr =
[Link](new String[0]);

String to int[]
String s = "1 2 3 4";
String[] temp = [Link](" ");
int[] arr = new int[[Link]];

for(int i=0;i{
arr[i] = [Link](temp[i]);
}

int[] to ArrayList
int[] arr = {1,2,3};

ArrayList list =
new ArrayList<>();

for(int x : arr)
{
[Link](x);
}

ArrayList to int[]
int[] arr =
[Link]()
.mapToInt(Integer::intValue)
.toArray();

int[] to Integer[]
Integer[] arr2 =
[Link](arr)
.boxed()
.toArray(Integer[]::new);

Integer[] to int[]
int[] arr2 =
[Link](arr)
.mapToInt(Integer::intValue)
.toArray();

You might also like