Saturday, May 21, 2016

Typecasting

In cases of specific programming, sometimes we need to change the data type to the other type. If both types are compatible or suitable, then Java will perform automatic type conversion. For example, we can always enter a value of int type to a variable declared with long type. However, if the types are not compatible, then we need to explicitly typecast to the variable to be converted. Typecasting is the casting process of data type to other data type.


Automatic Conversion in Java


By the time we enter a value into a variable that has a specific data type to the value of other types, then Java will internally perform automated conversion, with terms as follows:

  • Both data types (origin and destination) are compatible
  • The destination data type has a greater range of origin data type

For example, int data type always has greater range than byte type, so we do not need to perform explicit conversion from byte to int. Consider the following example code:

byte b = 2;
int c = b;

we don’t need to write as follow:

int c = (int) b;


Conversion of Not Compatible Data Type


We can also do the conversion from real numbers (floating-point) to integers. In such cases, a decimal value contained behind the comma will be discarded. For example, now we will convert the value of double to int. Consider the following code snippet:

double d = 274.5678;
int x = (int) d;   // Typecasting from double to int

Here, variable x will be worth 274 and the fractional value (0.5678) is discarded.

3 comments:

  1. Thank you for sharing valuable information with us.

    Java web application is a type of free programming language used to develop applications and software that are becoming highly in demand on the internet. The software engineer can provide the web application or software in the form of windows, Linux, and MAC OS. There are various types of systems that can not work without java and it is getting necessary day by day even the world’s no.1 application depends on java. Java has become a most popular feature in android smartphone applications that's why Aptech learning provides java training in Dwarka, Janakpuri, and in Gurgaon also. Java training helps to get you complete knowledge about web frameworks, Java testing tools, and design patterns in java.

    Let's get learn about the PHP training course
    PHP is a type of programming language that is most commonly used in dynamic functions such as modifying, collecting, and deleting all types of databases It also helps to encrypt all types of data in the form of HTML and XML file. Aptech learning provides you with a complete PHP training course at a reasonable cost that will help you to grow more in the era of the software industry.

    For more details please visit our website : Aptech Janakpuri

    ReplyDelete
  2. Thank You for Sharing valuable information with us.

    E-Commerce is the fastest-growing platform which is becoming more demandable day by day this is a tremendous form of the vibrant sector that is bringing several opportunities to the new generation to become more skilled and professional. Taking up this great advantage in the E-Commerce sector would need the right path and the right skillset you need to learn the latest technologies and high-demand skills such as building payment gateways and developing an accessible and easy website along with the basics of E-commerce that‘s why Aptech learning official Pro E-Commerce training program in Dwarka, Janakpuri, and Gurgaon which will help to make a successful career in this booming industry. Java Programming Course helps to become an expert in Java web application development. It helps you to bring efficiency in collecting and analyzing the data through Java programming in this course you will learn effective web UI/UX design and Secure web applications by using the advanced technologies of the Java program.

    For more details please visit our website : Aptech Gurgaon

    ReplyDelete