can anyone explain what is typecasting and type conversion in java with examples,,, im a beginner,,plzz help me,,! THANK YOU,,!
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: int age = 23; int temp - 32; float is a single precision point decimal number Code: float pressure = 5.5; float meters = 3.7; double is a double precision point decimal and all decimal types are considered double. Code: double percent = 98.56; double ratio = 1.56; For text there is char and String char is for a single character Code: char money = '$'; char percent = '%'; String is for text Code: String name = "Pein"; String quote = "Never die alone."; Type casting/conversion is when you change one type into another. Code: 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.
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,,, [COLOR="Red"]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]); }[/COLOR] plzz help me with this code....! thank you,! Code:
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=(Integer)O; // cast the elements of the Object array to Integers and add them to the Integer array k=j.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; //juz some code to add up sum System.out.println(k); } 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
Thank youuu soo much brother,,,! yu made me to understand that program completely,, now im clear with that stuff,,! thank yu,,,God blezz,,,!