Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Java - Arithmetic Operation, Conversion and Casts in java

 
 

Share on Google+Share on Google+

Java - Arithmetic Operation, Conversion and Casts in java

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 5, 2007

Related Tutorials

Discuss: Java - Arithmetic Operation, Conversion and Casts in java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:12
varaprasad
June 30, 2011
about java

information is better.but i want as many as possible simple examples
thajudeen
July 4, 2011
java

difference between final,finally and finalize() in java?
manoj
August 28, 2011
type casting in java

hello sir can we covert integer to string or string to integer casting
hi world
January 30, 2012
hello

nice
Shailendra kumar madheshia
February 1, 2012
Type costing

what is type casting? ans. Type casting is the process in which the value of small data type convert into the large data type.
nitin
June 14, 2012
java coding

i want to know about java language from basic in details please help me
Ranjan kumar gupta
August 5, 2012
java

class ab { public static void main(String args[]) { byte a=20; byte b=20; byte c=(byte)a+b; System.out.println(c); } } this code is posible or not
waqar ali
October 10, 2012
java

i want to become expert in java.how can i become expert?
aashish choudhary
January 5, 2013
friendship

what is friendship?????? define in brief........,.,..................
DMCA.com