Skip to main content

Python print() Function

When you start learning Python, one of the first things you need to learn is how to display information on the screen. For example, you might want to display: A welcome message. Your name and age. The result of a mathematical calculation. Student marks and results. Information generated by a Python program. Python provides a built-in function called print() for displaying information. The print() function is one of the simplest and most frequently used functions in Python. It helps programmers display information while testing programs, solving problems, and developing applications. In this article, we will learn how to use print() with practical examples and their outputs. 1. What Is the print() Function in Python? The print() function is a built-in Python function that displays information in the output area, usually the terminal or console. Let's look at a simple example. print("Hello, World!") Output: Hello, World! In this example, Python displays the text Hello,...

Type Casting in Java – A Complete Guide for Beginners

When working with data in Java, you often need to convert one data type into another. This process is called Type Casting. It allows programmers to use variables in different contexts and ensures smooth execution of operations between different data types.

In this article, we’ll explore what type casting is, its types, and examples to make it easier to understand.


๐Ÿ”น What is Type Casting in Java?

Type casting in Java is the process of converting a variable of one data type into another. It is mainly used when we want to perform operations between different data types or when memory optimization is needed.

For example:

int num = 10;
double result = num;  // int is converted to double

Here, the integer num is automatically converted into a double.


๐Ÿ”น Types of Type Casting in Java

Java supports two types of type casting:

1. Widening Casting (Implicit Casting / Type Promotion)

  • Automatically performed by Java.

  • Smaller data type is converted into a larger data type.

  • No data loss occurs.

Conversion order (small → large):
byte → short → int → long → float → double

Example:

public class WideningExample {
    public static void main(String[] args) {
        int num = 100;
        double d = num;  // int to double
        System.out.println("Integer value: " + num);
        System.out.println("Double value: " + d);
    }
}

Output:

Integer value: 100
Double value: 100.0

2. Narrowing Casting (Explicit Casting)

  • Done manually by the programmer.

  • Larger data type is converted into a smaller data type.

  • May lead to data loss or precision loss.

Syntax:

dataType variableName = (dataType) value;

Example:

public class NarrowingExample {
    public static void main(String[] args) {
        double d = 9.78;
        int num = (int) d;  // double to int
        System.out.println("Double value: " + d);
        System.out.println("Integer value: " + num);
    }
}

Output:

Double value: 9.78
Integer value: 9

๐Ÿ”น Type Casting with Non-Primitive Types

Apart from primitive data types, Java also allows type casting with objects and classes.

  • Upcasting: Casting a child object to a parent class (done automatically).

  • Downcasting: Casting a parent object to a child class (needs explicit casting).

Example:

class Animal {
    void sound() { System.out.println("Animal makes a sound"); }
}

class Dog extends Animal {
    void sound() { System.out.println("Dog barks"); }
}

public class CastingExample {
    public static void main(String[] args) {
        Animal a = new Dog();  // Upcasting
        a.sound();

        Dog d = (Dog) a;       // Downcasting
        d.sound();
    }
}

Output:

Dog barks
Dog barks

๐Ÿ”น Key Points to Remember

  • Widening casting is safe and done automatically.

  • Narrowing casting must be done manually and may cause data loss.

  • Object type casting is common in inheritance and polymorphism.


✅ Conclusion

Type casting in Java is an important concept that ensures flexibility while working with different data types and classes. By mastering both primitive type casting and object type casting, you can write efficient and error-free Java programs.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? ๐Ÿ“Œ Readability – Comments make code easier to understand for you and others. ๐Ÿ“Œ Maintenance – When you revisit your code after months, comments remind you what you were doing. ๐Ÿ“Œ Collaboration – In team projects, comments explain code to fellow developers. ๐Ÿ“Œ Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . ๐Ÿ’ป Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } ๐Ÿง  Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

๐Ÿ’ก Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. ๐Ÿง‘‍๐Ÿ’ป Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...