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,...

Understanding Data Types in Java: A Complete Guide for Beginners

When learning Java programming, one of the most important concepts to understand is data types. Every variable you use in Java must have a specific type that tells the compiler what kind of value it can hold. Without data types, it would be difficult for a program to know how to store, process, or perform operations on data.

In this article, we’ll explore what data types are, the types available in Java, and why they matter.


What are Data Types in Java?

A data type in Java defines the kind of values a variable can store and the operations that can be performed on it. For example, some variables store numbers, others store characters, and some store true/false values.

Java is a statically typed language, which means the data type of every variable must be declared at the time of creation.


Categories of Data Types in Java

Java data types are mainly divided into two categories:

  1. Primitive Data Types

  2. Non-Primitive (Reference) Data Types


1. Primitive Data Types

Primitive data types are the basic building blocks of Java. There are 8 types, each serving a specific purpose.

Data Type Size (in bytes) Description Example
byte 1 byte Stores small integers (-128 to 127) byte age = 20;
short 2 bytes Stores larger integers (-32,768 to 32,767) short year = 2025;
int 4 bytes Default integer type, widely used int salary = 50000;
long 8 bytes Stores very large integers long population = 7000000000L;
float 4 bytes Stores decimal values with single precision float price = 99.99f;
double 8 bytes Stores decimal values with double precision double pi = 3.14159;
char 2 bytes Stores a single character (Unicode) char grade = 'A';
boolean 1 bit (logical) Stores true/false values boolean isJavaFun = true;

๐Ÿ‘‰ Tip: Use int for whole numbers, double for decimal numbers, and boolean for true/false conditions.


2. Non-Primitive (Reference) Data Types

Non-primitive data types are more complex and are used to store objects. Unlike primitive types, they don’t store the actual value but rather a reference (memory address).

Common non-primitive data types:

  • String → Represents sequences of characters (String name = "Java";)

  • Arrays → Collection of multiple values of the same type (int[] marks = {90, 80, 70};)

  • Classes, Objects, Interfaces → User-defined data types.


Why are Data Types Important?

  • They ensure memory efficiency by reserving the right amount of space.

  • They prevent errors by restricting operations to compatible types.

  • They make code readable and maintainable.


Example Program

public class DataTypesExample {
    public static void main(String[] args) {
        int age = 21;
        double pi = 3.14159;
        char grade = 'A';
        boolean isJavaFun = true;
        String name = "Shivam";

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Pi Value: " + pi);
        System.out.println("Grade: " + grade);
        System.out.println("Is Java Fun? " + isJavaFun);
    }
}

Output:

Name: Shivam
Age: 21
Pi Value: 3.14159
Grade: A
Is Java Fun? true

Final Thoughts

Data types are the foundation of Java programming. By understanding and using them properly, you can write more efficient, error-free, and maintainable code. As you move forward, you’ll see how data types interact with operators, methods, and objects in Java.

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...