Friday 30 October 2015

Numeric Type Conversion and Casting


I am really sorry for not publishing anything yesterday. I had some electrical problems that's why but I am here now and I am going to continue from where I stopped.

Thank you for accepting my apologies. lol

Today, I want to talk about numeric type conversion and casting in Java programming language.

I hope you still remember what I told you about datatypes. Please if you don't re-visit one of my previous posts to do.

Take for example you want to convert an integer to a double. How do you do that?

This is done by what we call CASTING.

Casting is the process of converting a value with a particular datatype to another datatype.
There are two types of casting which are:



widening a type
narrowing a type

In widening a type, you are converting a value from a smaller datatype to a value of a larger datatype.
In narrowing a type, you are converting a value from a larger datatype to a value of a smaller datatype.

So, to convert an integer to a double datatype here is how it is done.

double number = (double)4;

to convert a double to an integer here is how it is done.

int num = (int)3.2

Don't worry we would code it soon. Just know the commands and how to do it.

Moving over to characters. 

For you to fully understand this char datatype you must have to know what ASCII code means.

ASCII comprises 128 characters of which each has a unique number assigned to it.

Now you can convert a value with a char datatype to an integer.

Take for example:

char egg = 'a';
int eggCode = (int)egg;

or 

char egg = 'a';
int eggCode = 'a';

As you can see there are two ways of converting a character to an integer.

Now if you want to convert an integer to a character. Here is how it goes.

int number = 99;
char symbol = (char)number;

or 

char symbol = 99;

As you can see there are two ways of converting an integer to a character also but in the second case you must write the real integer and not the variable in order for it to run effectively and efficiently.


Finally, there is something you should know which is known as escape sequences.
They are:

\t  -   Tab
\b  -   Backspace
\n  -   Move To the next line
\\  -   backslash
\'  -   Single quote
\"  -   Double quote

Adding any of these sequences carries out the command written beside it.

Take for example: If I want the computer to output this:

Nicholas said "I love hip hop".

I code it this way:

System.out.println("Nicholas said \"I love hip hop\".");

Let's Write a Program

A program that converts a double to an integer, an integer to a double, a character to an integer and an integer to a character.

package nicholas;
public class MyCast{
public static void main(String [] args){
// converting a double to an integer
double num = 32.78;
int number = (int)num;
// converting an integer to a double (floating point number)
int count = 23;
double counter = (double)count;
// converting a character to an integer
char symbol = 'u';
int symbolCode = (int)symbol;
// converting an integer to a char
int signCode = 111;
char sign = (char)signCode;
// displaying our new converts
System.out.print(number+"\n"+counter+"\n"+symbolCode+"\n"+sign+"\n");
}
}

Customize and Run...

Here is the screenshot of my code:

Here is the output:


I HOPE YOU GOT THE GIST.


Peace!

No comments:

Post a Comment