Type Casting in Java

By: Saravanan Emailed: 1730 times Printed: 2337 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

In this tutorial we are going to see how to typecast in java. Now we are going to see the syntax and sample code to convert each and every data type into other type.

//Integer code1
public class CastExample
{
public static void main(String arg[])
{
String s=”27”;
int i=Integer.parseInt(s);
System.out.println(i);
Float f=99.7f;
int i1=Integer.parseInt(f);
}
}

//Integer code2
public class CastExample
{
public static void main(String arg[])
{
String s=”27”;
int i=(int)s;
System.out.println(i);
}
}


//Integer to String
int a=97;
String s=Integer.toString(a);

(or)
String s=””+a;


//Double to String
String s=Double.toString(doublevalue);

//Long to String
String s=Long.toString(longvalue);

//Float to String
String s=Float.toString(floatvalue);

//String to Integer
String s=”7”;
int i=Integer.valueOf(s).intValue();

(or)
int i = Integer.parseInt(s);

//String to Double
double a=Double.valueOf(s).doubleValue();

//String to Long
long lng=Long.valueOf(s).longValue();

(or)
long lng=Long.parseLong(s);

//String to Float
float f=Float.valueOf(s).floatValue();

//Character to Integer
char c=’9’;
int i=(char)c;

//String to Character
String s=”welcome”;
char c=(char)s;

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(7)


1. View Comment

your second last example in above code (Character to string ),the second statement is wrong it should= int i= (int)c;

View Tutorial          By: jyoti at 2011-11-06 17:01:19
2. View Comment

program is very long

View Tutorial          By: ajit kumar at 2012-04-21 03:57:13
3. View Comment

Float f=99.7f;
int i1=Integer.parseInt(f);
above code will not work,bcz parseint method takes only string as a argument


View Tutorial          By: Ashwin at 2012-12-23 05:14:04
4. View Comment

Looks good. This is going to be a good resource.

View Tutorial          By: Brainpower at 2013-03-02 02:45:11
5. View Comment

Class TypeCasting
{
public static void main(String k[])
{
float sum=0.0f;
int a=10;

sum=sum+(float)a;
System.out.print("sum="+sum);
System.out.println("a="+a);
}
}


View Tutorial          By: kishoree koli at 2013-03-14 03:00:31
6. View Comment

your second last example in above code (Character to string )typecast not required int i= c;

View Tutorial          By: rupam shaw at 2013-04-24 08:36:05
7. View Comment

Design program with given algorithm---

step1:Declare a variable x of type integer and assign it to the value 10
Step 2: Declare a variable y of type double and assign it the value of x.
Step 3: Print the value of y it will show no error itâs called implicit casting.
Step 4: Now, Set the value of y to 13.5
Step 5: Assign value of y to the x. (It raises compilation error because implicit casting doesnât work as explained.)
Step 6: Use explicit casting (narrowing conversion) to assign y to x.
Step 7: print value of x (it gives you round off value y).
Step 8: Define a class named vehicle as a parent class, define a subclass of vehicle as car.
Step 9: create an object x of class vehicle.
Step 10: create an object y of class car.
Step 11: assign y to x (Its upcasting so no compilation error and no explicit casting is required)
Step 12: assign x to y (Its downcasting so it will give u compilation error. So use explicit casting)


View Tutorial          By: siddhant at 2014-10-11 13:19:25

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Saravanan
Converting properties using PropertyEditors and Other Spring features worth mentioning
How to create an array and method in JSP
inheritance in Java
Use List, Set, SortedSet, LinkedHashSet, Map, LinkedHashMap in java
cloneable in Java
Java program for Associate keys with values
Java code for Enumeration and using java.lang.reflect.Array
Insert an element in Array, Search and Sort Array by using java program
Type Casting in Java
How to initialize an Array and how to copy the array
Simple java program to add an element to specified index of Java ArrayList
How to use set, get basic and nested properties for Spring framework
How to access instance from an inner class and accessing outer class variables in java
How to use and access the inner class in java
Java program for Map

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
How to use ArrayList in Java
Method Overloading (function overloading) in Java
Method Overriding in Java
wait(), notify() and notifyAll() in Java - A tutorial
What is Java?
XML and Java - Parsing XML using Java Tutorial
FileReader and FileWriter example program in Java
Count number of vowels, consonants and digits in a String in Java
The Basic Structure of a Simple Java program
arraycopy example in Java
Recursion in java
Stack example in Java - push(), pop(), empty(), search()
Calendar - sample program in Java
Execute system commands in a Java Program
Most Emailed Articles (in Java)
Vector in Java - Sample Program
instanceof operator example in Java
3-D Secure Payment or 3DS - (3-Domain Secure Payment)
What is a Java Package and how to use it?
Use of return statement in Java
How to use Iterator in Java
J2EE Components
The java Buzzwords
The Basic Structure of a Simple Java program
Java Chat Client Sample Applet
MultiLevel Inheritance sample in Java
What is JasperReports?
java.lang.reflect package
Applet Notinited : Applet xxxxxxxxxxxx notinited
The if-then Statement in Java