Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Casting
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Operators
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays
Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance
Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Polymorphism
Introduction
Interfaces
Packages
Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions


Casting

We have already come across casting quite a number of types. In order to help you, we will now recollect all that we have learnt about casting till now and then learn something in addition to what you have already learnt.

Casting refers to the conversion of one data type to another. When we cast a particular variable, the data type of the variable itself is not altered. For example, in the following code, when we cast, num1 to a double; the variable num1 does not change its data type from int to double. Instead, a new copy of num1 is made and its data type is changed to double.

int num1=3, num2=4;
double result = (double) num1 / num2;


Casting is of two types- up casting and down casting. When we cast a variable to a higher data type it is known as up casting. A higher data type simply indicates that the memory allocation for that data type is higher. For example, int is a higher data type than short as the memory allocated for int is 32 bits which is higher than the memory allocated for a short which is 16 bits. On the other hand, when we cast a variable to a lower data type, it is known as down casting. When we up cast a variable, no information is lost but while down casting a variable, information loss might occur. Consider the following example:

short a = 347;
int b= 347347;
short c = (short)a;
int d = (short) b;
System.out.println(c);
System.out.println(d);

This code would give the output:

347
19667

As you can see, when an int was cast to a short, data loss had taken place as the number 347347 is too large for a short variable.

Following conversions fall under down casting

byte -> char
short -> byte, char
char -> byte, short
int -> byte, short, char
long -> byte, short, char, int
float -> byte, short, char, int, long
double -> byte, short, char, int, long, float

And the following conversions fall under up casting

byte -> short, int, long, float, double
short -> int, long, float, double
char -> int, long, float, double
int -> long, float, double
long -> float, double
float -> double

Implicit and explicit casting

When casting is performed by the programmer using the cast operator (a pair of parentheses containing the data tope), it is known as explicit casting. When such casting is performed by the compiler, it is known as implicit casting. Implicit casting is performed when operators and operands come into the scene. In order to operate on two operands with an operator, both of them should be of the same type. If they are not, implicit up casting to higher of the two types is performed. Consider an int 'a' and a long 'b'. When 'a' and 'b' are added, 'a' is first implicitly casted to a long and then added to 'b'.

int a=3;
long b=4;
long c=a+b; // a is implicitly cast to a long

Look at another example below:

char c='A';
int s=1;

If we perform the addition of c and s (c+s), we get an int as a result. This is because c is implicitly casted to an int. if we try to store the result in a short or a byte variable; we get a compilation error saying 'possible loss of precision'. Precision here refers to the number of digits. Therefore the following statement is invalid.

short ans= c+s;

We will have to cast the result of c+s into a short explicitly in the following way.

short ans= (short) (c+s);

Note that we require a parentheses around c+s. this is because the cast operator has a higher operator precedence than +. To force the addition to occur first, parentheses are used as shown above.

It is also possible to cast objects from one class type to another class type. We shall look at it when we deal with inheritance. For now, this is all you need to know about casting.

Privacy Policy