Java doubt-Type casting and type conversion.

Discussion in 'Java' started by Boaz, Sep 3, 2012.

  1. Boaz

    Boaz New Member

    Joined:
    Sep 3, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    can anyone explain what is typecasting and type conversion in java with examples,,,
    im a beginner,,plzz help me,,!
    THANK YOU,,!
     
  2. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    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.
     
    Boaz likes this.
  3. Boaz

    Boaz New Member

    Joined:
    Sep 3, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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:
    
    
     
  4. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    http://zontek.zzl.org
    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
     
    Boaz likes this.
  5. Boaz

    Boaz New Member

    Joined:
    Sep 3, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thank youuu soo much brother,,,!
    yu made me to understand that program completely,,
    now im clear with that stuff,,!
    thank yu,,,God blezz,,,!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice