Java Type Conversion

What is data type conversion?

  • Type conversion is common to assign a value of one type to a variable of another type.
  • There are two types of type conversion.
    • Implicit conversion: Java will perform the conversion automatically if two types are compatible.
    • Explicit conversion: Conversion between two types are incompatible. there is no conversion defined from double to byte. To do so, we must use cast, which performs explicit conversion between incompatible types.

What is implicit conversion?

Implicit conversion: also known as automatic conversions. when one type of data is assigned to another type of variable, implicit conversion will be done if below condition are satisfied.
  • Two types are compatible.
  • Destination variable type is larger than the source type.
Implicit conversion Example:
//Compatible types Example
byte a = 50;
int b;

//automatic conversion happens here if data of byte type is assinged to int variable.
// int variable is always large enought to hold data of byte type.
b = a;

What is explicit conversion?

Explicit conversion: To create a conversion between two incompatible types, we must use a casting. A cast is simply a explict type conversion.
  • Syntax: (target-type) value
  • target type specifies the required type to convert the specified value.
Explicit conversion Example:
int a = 100;
byte b;
// ....
//here data of int type is assigned to variable of byte type. byte type variable is not large enough to hold value of int type data.
//Need casting here, to convert int data to be assigned in byte variable.
b = (byte) a;
Type Casting in java:
// Type conversion in java                                                      
class TypeConversionTest {                                                      
 public static void main(String args[]) {                                       
  byte a;                                                                       
  int b = 310;                                                                  
  double c = 500.14;                                                            
  // Conversion of int to byte.                                                 
  a = (byte) b;                                                                 
  System.out.println("a and b: " + a + " " + b);                                
  // Conversion of double to int.                                               
  b = (int) c;                                                                  
  System.out.println("b and c: " + b + " " + c);                                
  //Conversion of double to byte.                                               
  a = (byte) c;                                                                 
  System.out.println("a and c: " + a + " " + c);                                
a and b: 54 310
b and c: 500 500.14
a and c: -12 500.14

Converting string to integer

int num = Integer.parseInt("302");
Integer.parseInt method is used to convert string into integer value.
If trying to convert empty value or null using Integer.parseInt, throws NumberFormatException exception.
]$ java ArithmeticTest
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
	at java.lang.NumberFormatException.forInputString(
	at java.lang.Integer.parseInt(
	at java.lang.Integer.parseInt(
	at ArithmeticTest.main(
string str = Integer.toString(302);
Integer.toString method is used to convert integer into string.
If trying to convert null or invalid value using Integer.toString, compiler error will be generated.
Java Programming Language Examples

GCD and Rational Number Java Program

Java Queue Program using exception

Java Stack Program using exception

Addition of three integers java program

Biggest of three integers java program

Fibonacci numbers java program

Arithmetic Operations Menu java program

Second Smallest Element In Array Java Program

Transpose Of A Matrix Java Program

Java Program to Display triangle of stars (*)

Java Programming Prints Product Tables

Java Program to Display triangle of numbers

Java Programming Gets Current Date

Java Programming Finds Character Vowel or Consonent

Java Programming HCF and LCM Computation

Java Programming Sum of Command Line Integer Arguments

Java Programming Multiplication of Command Line Integer Arguments

Java Programming Multiplication of Command Line Floating Point Numeric Arguments

Java Programming String Contains or Not

Java Programming Gets Grade Description using Switch

Java Programming CSV File Reader

Java Programming Character Frequency Count in String using for each

Java Programming Finds Min from Integer Array by Passing Array to Method

Java Programming Linear Search

Java Programming Binary Search

Java Programming Ternary Search

Java Programming Generates Range of Numbers

Java Programming Generates Range of Characters

Java Programming Computes Square Root Value

Java Programming Checks Number is Positive or Negative

Java Programming Checks Number is Odd or Even

Java Programming Computes Plot Area

Java Programming Convert Number of Days into Years

Java Programming Checks a Year is Leap Year, Century Year or Not

Java Programming Checks a Character is Digit, Letter or neither digit nor letter

Java Programming Checks a Number is Palindrome or not

Java Programming Sum of Two Matrix

Java Programming Power Computation

Java Programming Checks Number is an Armstrong Number or not

Java Programming Temperature Unit Conversions

Java Programming Generates Random Numbers in Specified Range

Java Programming Computes Sum of Digits and Product of Digits

Java Programming Computes Reverse Number

Java Programming Computes Factorial Value

Java Programming Checks Prime Number or not

Java Programming Computes Harmonic Series

Java Programming Generates Floyd's Triangle

Java Programming Reverses String

Java Programming Checks Palindrome String or not

Java Programming Opens Notepad

Java Programming Searches String using RegEx Pattern

Java Programming Searches Word in String

Java Programming Gets System Environment Variables

Java Programming Gets IP Address of Server Name

Java Programming Arrays Sort and Reverse using sort method

Java Programming Bubble Sorting

Java Programming Selection Sorting

Java Programming Insertion Sorting

Java Programming Merge Sorting

Java Programming Quick Sorting

Java Programming Counting Sort

Java Programming Radix Sorting

Java Programming Sorting Array of Strings

Java Programming String Characters Sorting

Java Programming Sum of First N Numbers

Java Programming Product of First N Numbers

Java Program to get URL details

Java Program to get URL HTML Content

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter