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

Java Type Conversion

The document provides a comprehensive overview of Java type conversions, including primitive to primitive casting, conversions between primitives and strings, and handling characters and arrays. It also covers conversions involving wrapper classes, number systems, collections, object casting, and streams, along with key examples and rules. The master summary table consolidates the categories and methods for quick reference.

Uploaded by

johovit123
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 views6 pages

Java Type Conversion

The document provides a comprehensive overview of Java type conversions, including primitive to primitive casting, conversions between primitives and strings, and handling characters and arrays. It also covers conversions involving wrapper classes, number systems, collections, object casting, and streams, along with key examples and rules. The master summary table consolidates the categories and methods for quick reference.

Uploaded by

johovit123
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 REVISION SHEET

1️⃣ PRIMITIVE → PRIMITIVE (Type Casting)


🔹 Widening (Automatic / Implicit)
byte → short → int → long → float → double
char → int → long → float → double

int a = 10;
double d = a;

✔ No data loss​
✔ Automatic

🔹 Narrowing (Manual / Explicit)


double d = 10.9;
int a = (int) d; // 10

⚠ Data loss possible​


⚠ Explicit cast required

2️⃣ PRIMITIVE ↔ STRING


✅ Primitive → String
String s1 = [Link](10);
String s2 = [Link](10);
String s3 = "" + 10;

✅ String → Primitive
int i = [Link]("10");
double d = [Link]("10.5");
float f = [Link]("3.14");
long l = [Link]("100");
boolean b = [Link]("true");

3️⃣ CHAR ↔ OTHER TYPES


🔹 char → int (Unicode)
int n = (int) 'A'; // 65

🔹 digit char → int


int n = '7' - '0'; // 7

🔹 int → char
char c = (char) 65; // 'A'

🔹 char → String
String s = [Link]('A');

🔹 String → char
char c = [Link](0);

4️⃣ STRING ↔ ARRAY


🔹 String → char[]
char[] arr = [Link]();

🔹 char[] → String
String s = new String(arr);
🔹 String → String[]
String[] parts = [Link](",");

🔹 String[] → String
String s = [Link](",", parts);

5️⃣ NUMBER ↔ NUMBER OBJECT (WRAPPER CLASSES)


🔹 Primitive → Wrapper (Autoboxing)
Integer x = 10;
Double d = 10.5;

🔹 Wrapper → Primitive (Unboxing)


int n = x;
double v = d;

6️⃣ STRING ↔ WRAPPER


🔹 String → Wrapper
Integer x = [Link]("100");
Double d = [Link]("10.5");

🔹 Wrapper → String
String s = [Link]();

7️⃣ NUMBER SYSTEM CONVERSIONS


🔹 int → binary / octal / hex
[Link](10); // 1010
[Link](10); // 12
[Link](10); // a
🔹 binary / octal / hex → int
int b = [Link]("1010", 2);
int o = [Link]("12", 8);
int h = [Link]("A", 16);

8️⃣ ARRAY ↔ COLLECTION


🔹 Array → List
List<String> list = [Link](arr);

⚠ Fixed size list

🔹 Array → Dynamic List


List<String> list = new ArrayList<>([Link](arr));

🔹 List → Array
String[] arr = [Link](new String[0]);

9️⃣ OBJECT ↔ OBJECT (CASTING)


🔹 Upcasting
Animal a = new Dog();

✔ Safe​
✔ Automatic

🔹 Downcasting
Dog d = (Dog) a;

⚠ Risky​
⚠ Needs instanceof

🔟 BOOLEAN CONVERSIONS
🔹 String → boolean
boolean b = [Link]("true");

🔹 boolean → String
String s = [Link](true);

1️⃣1️⃣ STREAM CONVERSIONS


🔹 Array → Stream
[Link](arr);

🔹 List → Stream
[Link]();

🔹 Stream → List
list = [Link]();

🧠 MASTER SUMMARY TABLE


Category Examples

Primitive → Primitive Casting

Primitive ↔ String valueOf / parse


char ↔ int Unicode

String ↔ Array split / join

Primitive ↔ Wrapper Autoboxing

String ↔ Wrapper valueOf

Number System Binary / Hex

Array ↔ List [Link]

Object Casting Up / Down

Stream Conversion stream()

✅GOLDEN RULES
✔ parseXxx() → String → Primitive​
✔ valueOf() → Anything → String / Wrapper​
✔ Widening = automatic​
✔ Narrowing = explicit cast​
✔ char is numeric internally

You might also like