string type casting java code example
Example 1: int to string java
int x = 3;
Integer.toString(int)
Example 2: java type casting
// You can typecast to convert a variable of one data type to another.
// Wide Casting converts small data types to larger ones.
// Narrow Casting converts large data types to smaller ones.
// Java can automatically Wide Cast.
// Java will throw an error when trying to automatically Narrow Cast.
// This is because data is often lost when Narrow Casting.
// Narrow Casting must be done manually.
//Wide Cast:
int SomeNumber = 5;
double WideCastedNumber = (double)SomeNumber;
//Narrow Cast:
double SomeNumber = 5.39;
int NarrowCastedNumber = (int)SomeNumber;
//Note: The data that holds the decimal places will be lost!
Example 3: java int to string
Integer.toString(n) // converts n to String
Example 4: casting in java
Byte-->short-->char-->Int-->long-->float-->double