on Leave a Comment

Type Casting in Java

When we assign a value of one data type to another data type, it is called type casting. The result of any expression is depending on the type of values. We can cast a value of one type to the value of another type by type casting.

Widening, Automatic or Implicit Type Conversion

In widening casting, small data type widens its value for storing in large data type. For example, if we assign a int variable to the float variable, then int variable widens itself for storing in float variable and no error will generated.

Java automatically widens small data type values for conversion, hence it is also called automatic type casting.

Permitted Widening Type Conversion

- byte value can be converted into short, int, long, float or double value

For Example

public class typecasting
{
        public static void main(String[] args)
        {
                byte b = 10;
                short s = b;
                int i = b;
                long l = b;
                float f = b;
                double d = b;
               
                System.out.println("s = "+s);
                System.out.println("i = "+i);
                System.out.println("l = "+l);
                System.out.println("f = "+f);
                System.out.println("d = "+d);
        }
}

OUTPUT:

s = 10
i = 10
l = 10
f = 10.0
d = 10.0

- short value can be converted into int, long, float or double value

For Example:

public class typecasting
{
        public static void main(String[] args)
        {
                short s = 20;
                int i = s;
                long l = s;
                float f = s;
                double d = s;
               
                System.out.println("i = "+i);
                System.out.println("l = "+l);
                System.out.println("f = "+f);
                System.out.println("d = "+d);
        }
}

OUTPUT:

i = 20
l = 20
f = 20.0
d = 20.0

- char value can be converted into int, long, float or double value

For Example:

public class typecasting
{
        public static void main(String[] args)
        {
                char c = '3';
                int i = c;
                long l = c;
                float f = c;
                double d = c;
               
                System.out.println("i = "+i);
                System.out.println("l = "+l);
                System.out.println("f = "+f);
                System.out.println("d = "+d);
        }
}

OUTPUT:

i = 51
l = 51
f = 51.0
d = 51.0

This is showing ASCII value of '3'.

- int value can be converted into long, float or double value

For Example:

public class typecasting
{
        public static void main(String[] args)
        {
                int i = 30;
                long l = i;
                float f = i;
                double d = i;
               
                System.out.println("l = "+l);
                System.out.println("f = "+f);
                System.out.println("d = "+d);
        }
}

OUTPUT:

l = 30
f = 30.0
d = 30.0

- long value can be converted into float or double value

For Example:

public class typecasting
{
        public static void main(String[] args)
        {
                long l = 40L;
                float f = l;
                double d = l;
               
                System.out.println("f = "+f);
                System.out.println("d = "+d);
        }
}

OUTPUT:

f = 40.0
d = 40.0

- float value can be converted into double value

public class typecasting
{
        public static void main(String[] args)
        {
                float f = 50f;
                double d = f;
               
                System.out.println("d = "+d);
        }
}

OUTPUT:

d = 50.0

Narrowing or Explicit Type Conversion

In narrowing conversion, we assign a large data type value to small data type value.

For Example:

public class typecasting
{
        public static void main(String[] args)
        {
                double d = 60.0d;
                short s = (short)d;
                System.out.println("s = "+s);
        }
}

OUTPUT:

s = 60

0 comments:

Post a Comment