Saturday, 7 December 2013

Typecasting in Java

Typecasting is a technique in which we change one type of data to another type to suit our needs. Let's say we have a double and we wish to convert it to integer. Or maybe we have a long that needs to be converted to byte.

Before we start converting one datatype to another, lets read up about available datatypes in java: http://mycodedock.blogspot.in/2013/11/data-types-in-java.html

In-fact, lets establish a few thumb-rules-

  • whenever converting smaller-bit datatypes to larger-bit datatypes we use Implicit Casting.
  • whenever converting larger-bit datatypes to smaller-bit datatypes we use Explicit Casting.
  • boolean datatype cannot be converted to any other datatype
  • conversion from string type to any other datatype needs the string to be parsed

  • conversion of any datatype (except boolean) to string needs to be done by passing value

Now you know all datatype conversion techniques that are used in java. Though, you might want to brush your skills up by some practice.


| Advertisement |

No comments:

Post a Comment