Java Fundamentals

Type Casting & Command Line Arguments

Prepared by Srikanth M

1. Java Type Casting (Type Conversion)

Type casting is a technique used either by the compiler or a programmer to convert one data type into another, such as int to double, double to int, or short to int. Type casting is also known as type conversion.

There are two types of casting allowed in Java programming:

  • Widening type casting
  • Narrowing type casting

Widening

Smaller type → larger type. Usually performed automatically by the compiler.

Narrowing

Larger type → smaller type. Performed explicitly by the programmer using a cast.

Java type casting overview from PDF

2. Widening Type Casting

Widening type casting is also known as implicit type casting. A smaller data type is converted into a larger data type automatically by the compiler.

Widening Hierarchy

byte → short → char → int → long → float → double

The source explains that the compiler performs the conversion at compile time and the conversion occurs from a smaller data type to a larger compatible data type.

Why is widening generally safe? The source describes widening as safe because there is no expected loss of the original numeric information when moving to a larger compatible type.

Conditions

  • Both data types must be compatible with each other.
  • The target type must be larger than the source type.

3. Widening Type Casting Example

public class WideningTypeCastingExample
{
    public static void main(String[] args)
    {
        int x = 7;

        // automatically converts the integer type into long type
        long y = x;

        // automatically converts the long type into float type
        float z = y;

        System.out.println("Before conversion, int value " + x);
        System.out.println("After conversion, long value " + y);
        System.out.println("After conversion, float value " + z);
    }
}

Output

Before conversion, int value 7
After conversion, long value 7
After conversion, float value 7.0

The PDF also shows an example involving int and double. In Java, an expression containing an int and a double produces a double, so the receiving variable should be compatible with double.

Widening and narrowing type casting page from PDF

4. Narrowing Type Casting

Narrowing type casting is also known as explicit type casting or explicit type conversion. It converts a larger data type into a smaller data type and is performed manually by the programmer.

For example, a programmer can explicitly convert double to int by using the cast operator.

Narrowing Hierarchy

double → float → long → int → char → short → byte
Important: Narrowing can result in loss of information, such as loss of a fractional part when converting a floating-point value to an integer.

If the required explicit conversion is not performed, the compiler can report a compile-time error for an incompatible assignment.

5. Syntax for Narrowing Type Casting

The syntax for manually converting a value is:

double doubleNum = (double) num;

The type placed in parentheses is the type to which the expression is explicitly converted.

Example

public class Tester {
    public static void main(String[] args) {

        int num = 5004;

        // Type casting int to double
        double doubleNum = (double) num;

        System.out.println(
            "The value of " + num +
            " after converting to the double is " + doubleNum
        );

        // Type casting double to int
        int convertedInt = (int) doubleNum;

        System.out.println(
            "The value of " + doubleNum +
            " after converting to the int again is " + convertedInt
        );
    }
}

Output

The value of 5004 after converting to the double is 5004.0
The value of 5004.0 after converting to the int again is 5004
Narrowing type casting syntax and example from PDF

6. Narrowing Type Casting Example

public class NarrowingTypeCastingExample
{
    public static void main(String args[])
    {
        double d = 166.66;

        // converting double data type into long data type
        long l = (long)d;

        // converting long data type into int data type
        int i = (int)l;

        System.out.println("Before conversion: " + d);

        // fractional part lost
        System.out.println("After conversion into long type: " + l);

        // fractional part lost
        System.out.println("After conversion into int type: " + i);
    }
}

Output

Before conversion: 166.66
After conversion into long type: 166
After conversion into int type: 166
Observe: The fractional part .66 is lost when the double value is converted to long.

7. Upcasting and Downcasting in Java

Downcasting

Downcasting refers to the procedure in which a subclass type refers to an object through a parent-class reference. The source explains that direct downcasting can result in a ClassCastException at runtime when the actual object is not of the expected subclass.

Downcasting is performed using an explicit cast and should be checked appropriately, commonly with the instanceof operator.

Upcasting

Upcasting is casting a subtype to a supertype in the upward direction of an inheritance tree. It is automatic: a subclass object can be referred to by a superclass reference variable.

class Parent {
}

class Child extends Parent {
}

Parent p = new Child();          // Upcasting

Child c = (Child) p;             // Downcasting

Implicit Casting

Class typecasting performed by the compiler without explicit cast syntax.

Explicit Casting

Class typecasting performed by the programmer using cast syntax.

Upcasting and downcasting diagram from PDF

8. Command Line Arguments in Java

A Java command-line argument is an argument passed when a Java program is run. These values can be received by the Java program and used as input through the main() method.

Command-line arguments are received through the String[] args parameter of the main method.

Syntax:
public static void main(String[] args)

9. Working of Command Line Arguments

The arguments are supplied as space-separated values. The source explains that strings and values representing primitive data types such as int, double, float and char can be supplied, but they arrive in the program through the String[] args array.

How the Arguments Reach main()

  1. Arguments are supplied when the Java program is executed.
  2. The JVM receives the supplied command-line values.
  3. The JVM provides them to the args[] array.
  4. The first argument is stored at args[0], the second at args[1], the third at args[2], and so on.
  5. args.length can be used to determine the number of supplied arguments.

Example

public class CommandLineExample {
    public static void main(String[] args)
    {
        // Printing the first argument
        System.out.println(args[0]);
    }
}

Example Execution

java CommandLineExample Hello

Output

Hello
Remember: Command-line arguments are received as strings. If you need an integer or another primitive value, convert the string using methods such as Integer.parseInt(), Double.parseDouble(), etc.
Command line arguments in Java from PDF

10. Quick Revision

ConceptMeaningKey Syntax / Example
Type Casting Converting one data type into another. int → double
Widening Smaller compatible type converted to a larger type automatically. int x = 7; long y = x;
Narrowing Larger type converted to a smaller type explicitly. int x = (int) doubleValue;
Upcasting Subclass object referenced by a superclass type. Parent p = new Child();
Downcasting Superclass reference explicitly converted back to a subclass type when the actual object is that subclass. Child c = (Child) p;
Command Line Arguments Values supplied when starting the Java program and received through args[]. args[0]

Type Conversion Summary

Widening:
byte → short → char → int → long → float → double

Narrowing:
double → float → long → int → char → short → byte

Key Points to Remember

  • Widening is generally automatic and implicit.
  • Narrowing is explicit and uses the cast operator.
  • Narrowing can lose information, especially fractional values.
  • Upcasting is automatic in an inheritance hierarchy.
  • Downcasting requires an explicit cast and should be used only when the object is actually an instance of the target subclass.
  • Command-line arguments are received as a String[].
  • The first command-line argument is available as args[0].