Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Java - Arithmetic Operation, Conversion and Casts in java 
 

In this section you will learn how to refers to changing an entity of one datatype into another.

 

Java - Arithmetic Operation, Conversion and Casts in java

                         

This example illustrates that what is type casting? Type Casting  refers to changing an entity of one datatype into another. This is important for the type conversion in developing any application. If you will store a int value into a byte variable directly, this will be illegal operation. For storing your calculated int value in a byte variable you will have to change the type of resultant data which has to be stored. This type of operation has illustrated below :

In this example we will see that how to convert the data type by using type casting. In the given line of the code c = (char)(t?1:0); illustrates that if t which is boolean type variable is true then value of c which is the char type variable will be 1 but 1 is a numeric value. So, 1 is changed into character according to the Unicode value. But in this line c = (char)(t?'1':'0'); 1 is already given as a character which will be stored as it is in the char type variable c.

Code of the program :

public class conversion{
 
 public static void main(String[] args){
    boolean = true;
    byte b = 2;
    short s = 100;
    char c = 'C';
    int i = 200;
    long l = 24000;
    float f = 3.14f;
    double d = 0.000000000000053;
    String g = 
"string";
    System.out.println("Value of all the variables like");
    System.out.println("t = " + t );
    System.out.println("b = " + b );
    System.out.println(
"s = " + s );
    System.out.println("c = " + c );
    System.out.println("i = " + i );
    System.out.println("l = " + l );
    System.out.println("f = " + f );
    System.out.println("d = " + d );
    System.out.println("g = " + g );
    
System.out.println();
    //Convert from boolean to byte.
    b = (byte)(t?1:0);
    System.out.println("Value of b after conversion : " + b);
    //Convert from boolean to short.
    s = (short)(t?1:0);
    System.out.println("Value of s after conversion : " + s);
    //Convert from boolean to int.
    i = (int)(t?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 c after conversion : " + c);
    c = (char)(t?1:0);
    System.out.println("Value of c after conversion in unicode : " + c);
    //Convert from boolean to long.
    l = (long)(t?1:0);
    System.out.println("Value of l after conversion : " + l);
    //Convert from boolean to float.
    f = (float)(t?1:0);
    System.out.println("Value of f after conversion : " + f);
    //Convert from boolean to double.
    d = (double)(t?1:0);
    System.out.println("Value of d after conversion : " + d);
 
   //Convert from boolean to String.
    g = String.valueOf(t);
    System.out.println(
"Value of g after conversion : " + g);
    g = (String)(t?"1":"0");
    System.out.println("Value of g after conversion : " + g);
    int sum = (int)(b + i + l + d + f);
    System.out.println("Value of sum after conversion : " + sum);
  }
}

Download Type Casting Example

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

Hi,this casting example is good but the program is needed with in simple textpad formet

Posted by Niranjan on Friday, 07.10.09 @ 08:55am | #89214

how to casting short to int ?

Posted by rabBlue on Tuesday, 04.7.09 @ 14:48pm | #86603

how can i convert a double so that i can test to see if it is one;
for example the program calls for me to test to see if what was entered was a double;
i tried to use a boolean and set it equal to the double in an if-else statement; thanks

Posted by Amber on Monday, 09.29.08 @ 00:59am | #80753

class A
{
protected int i;
}
class B extends A
{
}
class C extends B
{
//i have to assign value to the variable i without creating instance ?
}

Posted by abdulrazak on Tuesday, 02.12.08 @ 13:39pm | #47996

this program is nice.but i am looking for string program without using the built in function.

Posted by suresh on Tuesday, 09.18.07 @ 18:14pm | #27493

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.