Results 1 to 4 of 4

Thread: What is type conversion and casting?

  1. #1
    Join Date
    Jan 2009
    Posts
    44

    What is type conversion and casting?

    Hi friends,

    One of the most essential concept about programming and which is uncleared for me is "type conversion and casting". I am unable to understand this concept because of it's complexity. what is meant by type conversion and casting? What is the use of type conversion and casting? Please recommend me to understand the concept of type conversion and casting. Your help will be appreciated.
    Last edited by Jagriti; 24-11-2009 at 11:01 AM.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: What is type conversion and casting?

    In the field of programming "Type conversion" and "casting" are use to convert an entity of one data-type into another datatype.

    In OOPs(object-oriented programming), "Type conversion" allows programs to behave objects of one data-type as one of their types of ancestor to make simple interaction with them.

    Every programming language has different rule of type conversion and casting.Mainly there are 2 types of conversion i.e explicit & implicit. The more common form of explicit conversion type is called as casting

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: What is type conversion and casting?

    Hi friend,

    The process of converting value of the one data-type into the another data-type is called as "casting" OR "Type conversion".

    Basically there are 2 types of casting 1.Up-casting and 2.Down-casting:

    1.Up-casting:It is the process of converting value of the lowar data-type into the data-type which is larger.

    2.Down-casting:It is the process of converting value of the larger data-type into the data-type which is smaller.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What is type conversion and casting?

    Below go through below program code ,which will definitely give you the basic idea behind the "Type conversion" OR "Casting":

    Program code:

    public class conversion{
    public static void main(String[] args){
    boolean t1 = true;
    byte b1 = 2;
    short s1 = 100;
    char c1 = 'C';
    int i1 = 200;
    long l1 = 24000;
    float f1 = 3.14f;
    double d1 = 0.000000000000053;
    String g1 = "string";
    System.out.println("Value of all the variables like");
    System.out.println("t1 = " + t1 );
    System.out.println("b1 = " + b1 );
    System.out.println("s1 = " + s1 );
    System.out.println("c1 = " + c1 );
    System.out.println("i1 = " + i1 );
    System.out.println("l1 = " + l1 );
    System.out.println("f1 = " + f1 );
    System.out.println("d1 = " + d1 );
    System.out.println("g1 = " + g1 );
    System.out.println();
    //Convert from boolean to byte.
    b1 = (byte)(t1?1:0);
    System.out.println("Value of b1 after conversion : " + b1);
    //Convert from boolean to short.
    s1 = (short)(t1?1:0);
    System.out.println("Value of s after conversion : " + s1);
    //Convert from boolean to int.
    i1 = (int)(t1?1:0);
    System.out.println("Value of i after conversion : " + i);
    //Convert from boolean to char.
    c = (char)(t?'1':'0');
    System.out.println("Value of c1 after conversion : " + c1);
    c = (char)(t?1:0);
    System.out.println("Value of c1 after conversion in unicode : " + c1);
    //Convert from boolean to long.
    l1 = (long)(t1?1:0);
    System.out.println("Value of 1l after conversion : " + l1);
    //Convert from boolean to float.
    f1 = (float)(t1?1:0);
    System.out.println("Value of f1 after conversion : " + f1);
    //Convert from boolean to double.
    d1 = (double)(t1?1:0);
    System.out.println("Value of d1 after conversion : " + d1);
    //Convert from boolean to String.
    g1 = String.valueOf(t);
    System.out.println("Value of g1 after conversion : " + g1);
    g1 = (String)(t1?"1":"0");
    System.out.println("Value of g1 after conversion : " + g1);
    int sum = (int)(b1 + i1 + l1 + d1 + f1);
    System.out.println("Value of sum after conversion : " + sum);
    }
    }

Similar Threads

  1. Replies: 4
    Last Post: 02-01-2011, 12:25 AM
  2. Replies: 1
    Last Post: 16-08-2010, 05:25 PM
  3. Casting int to float in C
    By Jensen Ackles in forum Software Development
    Replies: 4
    Last Post: 05-04-2010, 05:08 PM
  4. Type Casting in Java
    By Aakarshan.d in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 02:56 PM
  5. Casting a table dynamically
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 11:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,514,834.66665 seconds with 16 queries