Posts

Showing posts with the label Java Programming

Java Basics: Main Method and println() Method

Main Method in Java The main method is the entry point of a Java program. During program execution, the JVM (Java Virtual Machine) searches for the main method to start the execution. The JVM uses an inbuilt mechanism to locate the main method, which must be written in lowercase exactly as main. Java is case-sensitive, so any change in the method name will cause a runtime error. If the main method is present, the JVM: Locates the bytecode of the main method Converts the bytecode into machine-specific code Passes the machine code to the operating system Produces the output of the Java program If the main method is not found, the JVM generates a runtime error. Syntax of Main Method: public static void main(String[] args) {     // program execution starts here } println Method in Java The println() method is used to display data on the console output. Key points: It prints the given data on the console After printing, it moves the cursor to the next line The println() method bel...

How to Create a Java Class: Step-by-Step Guide

Creation of Java Class A Java class is a blueprint or template used to create Java objects. It defines the properties (variables) and behaviors (methods) that the objects of the class will have. The content defined inside a Java class is represented inside the Java object created from that class. How to Create a Java Class A Java class is created using the class keyword, followed by a valid identifier (class name). Java Class Syntax: class ClassName {     // variables     // methods     // constructors } Important Points About Java Class A Java class can contain: Variables Methods Constructors Blocks and nested classes A Java class must be saved with the .java file extension. The file name should match the class name when the class is declared as public. Main Method in Java For execution, a Java program must contain the main method. The main method acts as the entry point of a Java program. If the main method is not present in the class, the program will c...

Java Programming Features Every Beginner Should Know

Features of Java Java is a powerful programming language that provides several important features, making it popular for developing secure and reliable applications. 1. Platform Independent Java is called a platform-independent language because a Java program does not depend on a specific operating system. When a Java program is compiled, it generates a .class file. This file contains intermediate bytecode, not machine-specific code. The .class file can run on any operating system as long as the system has JVM (Java Virtual Machine) installed. This concept is known as “Write Once, Run Anywhere.” 2. Java Compiler The Java compiler (javac) is a program that converts the source code written by a developer into bytecode. The compiler: Takes a .java file as input Converts it into intermediate machine code Stores the output in a .class file This bytecode is platform independent and can be executed on any system with JVM support. 3. Java Virtual Machine (JVM) The JVM (Java Virtual Machine) ...

Scanner Class in Java: Complete Guide to User Input with Examples

Scanner Class. Scanner Class is present inside the java.util package. It is used for the take the input from user. Scanner Class method. nextByte();read byte data. nextShort();read short data. nextInt();read integer data. nextLong();read long data. nextFloat();read float data. nextDouble();read double data. nextBoolean();read boolean data. next();read string and character data. nextLine();read string data.

File Reading in Java Using FileReader: Step-by-Step Guide with Examples

📘 File Reading in Java (Using FileReader Class) What is File Reading in Java? File Reading in Java is the process of reading data from a file stored on the system. Java provides the FileReader class (not “File Reading”) inside the java.io package to read character-based data from files. 📦 FileReader Class The FileReader class is used to read text data from a file character by character. 📌 Package java.io 🧱 Constructor of FileReader FileReader(String filePath) throws FileNotFoundException Opens a file located at the given file path. Internally creates an input stream to read data from the file. Throws FileNotFoundException if the file does not exist. 🛠️ Methods of FileReader Method.               Description int read().    Reads one character at a time and returns its ASCII (Unicode) value void close().   Closes the input stream and releases system resources 🔄 How File Reading Works (Step by Step) 1. Create a FileReader ob...

Packages in Java: Definition, Types, and Examples

Package in Java A package in Java is a collection of related classes, interfaces, and abstract classes that are grouped together as part of an application. Packages help in organizing code, avoiding name conflicts, and improving maintainability. Types of Packages Packages in Java are classified into two types: 1. Inbuilt Packages 2. User-Defined (Programmer-Defined) Packages 1. Inbuilt Packages Inbuilt packages are predefined packages that are available in the JDK software. These packages usually start with the java keyword. Examples of Inbuilt Packages java.lang java.sql java.io java.util java.net java.time  📌 Note: The java.lang package is imported automatically by the JVM. 2. User-Defined (Programmer-Defined) Packages User-defined packages are created by programmers according to the application requirements. Package Naming Convention com.companyname.applicationname.modulename org.companyname.applicationname.modulename Example package com.example.billing.invoice; Importing a Pac...

Polymorphism in Java: Definition, Types, and Examples

Polymorphism in Java Polymorphism is an object-oriented programming concept in which a single reference variable can exhibit different behaviors depending on the object it refers to. Types of Polymorphism Polymorphism in Java is classified into two types: Compile-Time Polymorphism Run-Time Polymorphism 1. Compile-Time Polymorphism In compile-time polymorphism, the method resolution is performed by the compiler. Key Points The compiler resolves the method call based on the parameter list. Once method resolution is done, the compiler performs static binding. In static binding:               ✓ The method declaration is bound to the method definition at compile time. Compile-time polymorphism is achieved using method overloading. Example class Calculator {     int add(int a, int b) {         return a + b;     }     double add(double a, double b) {         return a + b;     } } ...

Down Casting in Java: Definition, Rules, and Examples

Down Casting in Java Down Casting is the process of converting a superclass reference (that actually refers to a subclass object) into a subclass reference. Key Points Down casting is also known as explicit casting. It must be performed by the programmer using a cast operator. The compiler does not perform down casting automatically. Up casting must occur before down casting. If down casting is done without a valid up cast, it results in a ClassCastException at runtime. Example class Parent {     void show() {         System.out.println("Parent method");     } } class Child extends Parent {     void display() {         System.out.println("Child method");     } } Parent p = new Child(); // Up casting Child c = (Child) p; // Down casting c.show(); // Allowed c.display(); // Allowed Invalid Down Casting Parent p = new Parent(); Child c = (Child) p; // ClassCastException Why Down Casting Is Risky Per...

Up Casting in Java: Definition, Rules, and Examples

Up Casting in Java Up Casting is the process of assigning a subclass object to a superclass reference or a super-interface reference. Key Points Up casting is also known as implicit casting. It is performed automatically by the compiler. In up casting, the compiler treats the subclass object as a superclass object. No explicit cast is required. Parent p = new Child(); // Up casting Accessing Members During Up Casting When up casting occurs, the programmer can access only the members of the superclass using the superclass reference. Subclass-specific members cannot be accessed directly. class Parent {     void show() {         System.out.println("Parent method");     } } class Child extends Parent {     void display() {         System.out.println("Child method");     } } Parent p = new Child(); p.show(); // Allowed // p.display(); // Not allowed Up Casting with Method Overriding If method overriding is...

Interface in Java: Definition, Rules, and Examples

Interface in Java An interface is a type definition block in Java that is completely abstract. It is used to achieve full abstraction and to define a contract that implementing classes must follow. Key Points An interface is declared using the interface keyword. An interface can contain:            ✓public static final variables (constants)           ✓public abstract methods (instance methods) An interface cannot be instantiated (object creation is not allowed). An interface can extend another interface using the extends keyword. A class can implement an interface using the implements keyword. An interface cannot extend a class. An interface supports complete abstraction because it contains only abstract methods. An interface supports multiple inheritance Example interface Animal {     int AGE = 5; // public static final by default     void sound(); // public abstract by default } class Dog implements Animal { ...

Abstract Class in Java: Definition, Rules, and Examples

Abstract Class in Java An abstract class is a class that is declared using the abstract keyword. It is used to achieve partial abstraction in Java. Key Points Any class declared with the abstract keyword is called an abstract class. An abstract class can contain:             ✓Abstract methods             ✓Concrete methods An abstract class cannot be instantiated (object creation is not allowed). If a class contains at least one abstract method, then the class must be declared abstract. An abstract class does not support multiple inheritance. An abstract class supports partial abstraction. Abstract Method An abstract method is a method that:                 ✓Does not have a method body                  ✓Ends with a semicolon (;) Abstract methods only provide method declaration, not implementation.             abstra...

Encapsulation in Java: Definition, Examples, and Advantages

Encapsulation in Java Encapsulation is an object-oriented programming (OOP) concept in which an object supports data binding and data hiding to protect its internal state. Key Concepts Data Binding Data binding is the process of associating the data with an object in memory. It ensures that the object's data is properly managed and accessed through the object. Data Hiding Data hiding is the process of restricting direct access to the object's data from other objects. Typically achieved by making variables private and providing getter and setter methods to access or modify the data. Encapsulation in Practice If a Java object supports both data binding and data hiding, it is considered an encapsulated object. Encapsulation protects the integrity of the data and allows controlled access. Example class Student {     // Private data (data hiding)     private String name;     private int age;     // Getter method     public String getName() { ...

Method Overriding in Java: Definition, Rules, and Examples

Method Overriding Method Overriding is a process in Java where a subclass provides a specific implementation for a method that is already defined in its superclass. Key Points 1. Method Overriding requires inheritance. 2. To override a method, the subclass method must have the same name, return    type, and parameter list as the superclass method. 3. During method overriding: The subclass method definition is used (exposed). The superclass method definition is hidden. This concept is sometimes referred to as method hiding (though strictly         speaking, method hiding is for static methods). 4. Only instance methods (non-static methods) can be overridden. 5. Method overriding cannot be achieved within the same class; it only works between subclass and superclass. Example class Animal {     void sound() {         System.out.println("Animal makes a sound");     } } class Dog extends Animal {     @Overri...