Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Java doubt-Type casting and type conversion. (http://www.go4expert.com/forums/java-doubt-type-casting-type-conversion-t29002/)

Boaz 4Sep2012 02:19

Java doubt-Type casting and type conversion.
 
can anyone explain what is typecasting and type conversion in java with examples,,,
im a beginner,,plzz help me,,!
THANK YOU,,!

pein87 6Sep2012 14:06

Re: Java doubt-Type casting and type conversion.
 
When you create a variable, array, or method you need to give it a data type. I wont go to in-depth with all the various data types but I will list just a few. Java like C/C++ and C# have strict typing. What that means is you need to say what type of data a variable, array, or method is or will return. These data types include text, numbers, and objects.

Number data types include int, float, and double

int is for any whole number with in a certain range and includes negative numbers as well.

Code: Java

int age = 23;
int temp - 32;


float is a single precision point decimal number

Code: Java

float pressure = 5.5;
float meters = 3.7;


double is a double precision point decimal and all decimal types are considered double.

Code: Java

double percent = 98.56;
double ratio = 1.56;


For text there is char and String

char is for a single character

Code: Java

char money = '$';
char percent = '%';


String is for text

Code: Java

String name = "Pein";
String quote = "Never die alone.";


Type casting/conversion is when you change one type into another.

Code: Java

double percent = 89.45;

int num = (int)percent;


As you can see I turned the percent variable into an int type by adding (int) in front of the variables name. num now has the value of 89. When you type cast from double to int you drop off the decimal point from that number. No rounding happens when this is done.

Hope this helps you.

Boaz 6Sep2012 16:50

Re: Java doubt-Type casting and type conversion.
 
hiiii,,
Thank you so much brother,,,,
Your Reply helped me lot.,!
My tutor wrote a code,,but i cant able to understand that,,,i'll write that code here,,plz have a loot at it,,and say what he have done in this code,,if possible yu give me a example in these kind of casting,!
Code:

//this is a program to add elements in an arraylist.
i dont knw wat they have done here,,,
Object O[]=al.toArray();
Interger j[]=new Integer[O.length];
int k[]=new int[O.length]
for(int i=0;i<O.length;i++)
{
j[i]=(Integer)O[i];
k[i]=j[i].intValue();
sum=sum+k[i];
System.out.println(k[i]);
}


plzz help me with this code....! thank you,!

ManzZup 15Sep2012 16:48

Re: Java doubt-Type casting and type conversion.
 
I assume al = an Array LIst

Object O[]=al.toArray();
Make a new Object Array (it means it can hold any object) and assign the array created with the elements of the array list al (check toArray() function)

Interger j[]=new Integer[O.length];
New integer of Integer wrapper to hold the size of the array (this can also be assign to a normal int)

int k[]=new int[O.length]
Make new array of int with the length of the size of the previous object

for(int i=0;i<O.length;i++)
{
//for each element in the array made by the array list

j[i]=(Integer)O[i];
// cast the elements of the Object array to Integers and add them to the Integer array

k[i]=j[i].intValue();
// again get int value of the Integer wrapper

here a lot of coversions happen
first ArrayList to a Array of Objects
Then Object -> Integer //this can only be done if the original content of the array list are Integer
The Integer to int conversion [the .intValue() isnt necessary with java 6)



sum=sum+k[i];
//juz some code to add up sum
System.out.println(k[i]);
}


well here there's a lot of parts to be known
you must need info about Array Lists, wrapper classes, conversions from one type to another

Summary
this code is in summary convert an Array List of Integers to an array of int

Boaz 15Sep2012 23:56

Re: Java doubt-Type casting and type conversion.
 
Thank youuu soo much brother,,,!
yu made me to understand that program completely,,
now im clear with that stuff,,!
thank yu,,,God blezz,,,!


All times are GMT +5.5. The time now is 22:06.