Java Data Type Conversion

Instructor: Thomas Wall

Thomas is a professional software developer, online instructor, consultant and has a Masters degree.

You'll learn how to move values contained in one Java data type into a different data type, as well as the consequences of any conversion that may take place.


Conversions happen all the time in everyday life. We convert currency from one form to another and measurements between the Metric and English systems. These conversions don't alter the value, but just change the way it's represented. Java has these primitive (built-in) data storage types:

Type Contains Size Range
boolean true or false 1-bit NA
char Unicode character 16-bit \u0000 to \uFFFF
byte Signed integer 8-bit -128 to 127
short Signed integer 16-bit -32768 to 32767
int Signed integer 32-bit -2147483648 to 2147483647
long Signed integer 64-bit -(9 x 10^19) to 9 x 10^19
float IEEE 754 floating point 32-bit ±1.4E-45 to ±3.4E+38
double IEEE 754 floating point 64-bit ±4.9E-324 to ±1.8E+308

Each data type is designed to efficiently store one of three categories of information:

  • Numbers of various sizes and ranges (byte, short, int, long, float and double)
  • Unicode text characters (char)
  • True/false values (boolean)

During the execution of a Java program, the data type associated with a variable remains the same as specified when the variable was initially declared. In most situations, all other variables and calculations utilizing the initial information will be declared the same data type and information will flow smoothly without any type of conversion necessary. However, sometimes it becomes necessary to move information stored in one data type into a variable declared as a different data type. The necessary conversion can be done either implicitly or explicitly.

Implicit Conversion

Numbers can always be moved from a data type of a given size into a data type of wider size (i.e., one with more bits of storage) without any loss of information. Therefore, the Java system will implicitly and automatically move the data without raising an error condition. As illustrated below, implicit conversion is allowed when converting from a numeric data type to any data type to the right:


It's important to note that despite the fact the Unicode 'char' data type occupies 16-bits and 'boolean' true/false values occupy 1-bit, implicit conversion to/from other data types (even if their storage space is wider) is NOT allowed and the Java system will issue an error if it's attempted. Here are some examples:

// My age in years
byte myAge = 23;    //(8 bits)
// Arbitrary integers
long someNumber;    //(64 bits)
int anotherNumber;    //(32 bits)
// My age plus a fractional year
double myAgePlus;    //(64 bits)
// Implicit conversion
someNumber = myAge;    //8 bits to 64 bits
/* someNumber implicitly converted to 64-bit; then a floating point
value of 0.5 is added. Result is then stored in myAgePlus */
myAgePlus = someNumber + 0.5;
/* These implicit conversions are NOT allowed!
Java will report an error */
anotherNumber = someNumber;
float anotherAge = myAgePlus;

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account