Core Java Tutorial

Define Java

C vs C++ vs Java

Java Virtual Machine

Java First Program

Java Data Types

Java Tokens

Java Keywords

Java Operators

Operator Precedence

Java Type Casting

Java Statement

Java If statement

Java Switch statement

Java Loops

Java Jumping Statement

Java Arrays 1D

Java Arrays 2D

Java Variable Size Array

Java Vector

Java Math Methods

Java String Methods

Java String Buffer

User-Defined Method

Java Method Overloading

Java Class & Object

Java Constructor

Java Inheritance

Java Interface

Java Packages

Java Multi-threading

Exceptional Handling

File Handling

Java Applets

Java DataBase Connectivity

Page Stats

Visitor: 182

Java Type Casting

Type Casting is use to convert value of one data type into another data type. The process of assigning a smaller data type into larger data type is known as widening or promotion. For example:

byte b = 10;
int i = b;

Assigning a larger data type value into a smaller data type is known as narrowing. In this type of assigning, loss of information may be possible.

int i = 10;
byte b = (byte)i

Convert string to numeric value

int i = Integer.valueOf("22").intValue();
long l = Long.valueOf("22").longValue();
double d = Double.valueOf("22.5").doubleValue();
float f = Float.valueOf("22.5").floatValue();
int i = Integer.parseInt (str);
long l = Long.parseLong (str);
parseInt() and parseLong() methods throw a NumberFormatException if the value of str does not present an integer.

Converting Number to String

string str = Integer.toString(i);
string str = Float.toString(f);
string str = Double.toString(d);
string str = Long.toString(l);