Skip to main content

Posts

Understanding Programming Methodologies: A Comprehensive Guide

Understanding Programming Methodologies: A Comprehensive Guide Introduction Programming methodologies define structured approaches to writing code, improving efficiency, maintainability, and scalability. Different methodologies provide distinct ways of thinking about problem-solving, organizing logic, and structuring applications. This blog explores various programming methodologies, their advantages, drawbacks, applications, and best use cases. 1. Procedural Programming Procedural programming follows a step-by-step approach where code is structured as procedures or functions. Characteristics: Based on the concept of procedure calls. Follows a linear, top-down execution model. Uses variables, loops, and control structures. Languages: C, Pascal, Fortran Sample Code (C): #include <stdio.h> void greet() { printf("Hello, World!\n"); } int main() { greet(); return 0; } Applications: Embedded systems (e.g., firmware, microcontrollers) Operating systems (e.g., Li...
Recent posts

Understanding Constructors in Java: A Simple Guide with Examples and Analogies

  What is a Constructor in Java? In Java, a constructor is a special type of method that is used to initialize objects. When you create an object of a class, the constructor is called automatically. Its main job is to set the initial values of the object’s properties or perform any setup that the object needs before it can be used. Why Do We Need Constructors? You need constructors because: Initialization : Constructors are responsible for initializing an object when it is created. Automatic Execution : A constructor is automatically called when an object is created, so you don’t have to manually initialize every property. Simplifying Object Creation : It simplifies object creation by providing default values or custom initialization. Where Do Constructors Fit in Java? Constructors fit within a class. They are used whenever a new object of that class is created, and they allow the object to be initialized. Constructors must have the same name as the class, and they don't have a re...

Implementing Arrays and String Functions in Java

  1️⃣ Working with Arrays in Java An array in Java is a collection of elements of the same type , stored in contiguous memory locations. 🔹 Declaring and Initializing an Array: public class ArrayExample { public static void main (String[] args) { int [] numbers = { 10 , 20 , 30 , 40 , 50 }; // Array declaration and initialization System.out.println( "First element: " + numbers[ 0 ]); // Accessing array elements // Using a loop to print all elements System.out.println( "Array Elements:" ); for ( int num : numbers) { System.out.println(num); } } } ✅ Output: First element: 10 Array Elements: 10 20 30 40 50 🔹 Taking Array Input from User and Finding Sum: import java.util.Scanner; public class ArrayInput { public static void main (String[] args) { Scanner sc = new Scanner (System.in); System.out.print( "Enter number of elemen...

Why is String[] args necessary in main() method in Java?

  Why is String[] args necessary in main() method in Java? In Java, the main method serves as the entry point for the program. The correct syntax for the main method is: public static void main (String[] args) { System.out.println( "Hello, Java!" ); } 🔹 Breaking it down: public → Accessible from anywhere. static → No need to create an object of the class to run it. void → No return value. main → Special method recognized by the JVM as the starting point. String[] args → Stores command-line arguments (optional but required by JVM). Why Can't We Skip String[] args ? JVM looks for main(String[] args) When you run a Java program, the JVM searches for the main method with exactly this signature : public static void main (String[] args) If you change or remove String[] args , the JVM cannot find the correct main() method and throws a runtime error (NoSuchMethodError). Java Specification Requires It The Java Language Specification (JLS) defines that main...

Why JVM Was Needed & How It Evolved – A Story

  The Problem Before Java In the early days of programming, developers faced a big issue: portability . Every programming language required code to be rewritten for different operating systems. A program written for Windows wouldn’t run on Linux or Mac without modifications. Imagine you are a Bollywood director making a movie. If you shoot in Hindi, it won’t reach a Tamil-speaking audience unless you dub or remake it. This was the same problem in software—each platform needed a different version of the same program. The Birth of Java & JVM (1991-1995) In 1991, James Gosling and his team at Sun Microsystems wanted to solve this problem. They dreamed of a language that could run anywhere, on any device, without modification . They created Java , but Java alone wasn’t enough. They needed a translator that could understand Java and speak the language of any operating system. This is where the Java Virtual Machine (JVM) was born. Java Compiles Code to Bytecode : Instead of com...

Reading Values from Keyboard in Java

  Reading Values from Keyboard in Java In Java, we can take user input from the keyboard using different methods. The most common ways are: Using Scanner Class (Recommended) Using BufferedReader Using Console Class 1. Using Scanner Class (Most Common & Easy) The Scanner class is the easiest way to read input from the keyboard. It is part of the java.util package. Example: Read Integer, Float, and String Input import java.util.Scanner; // Import Scanner class public class ScannerExample { public static void main (String[] args) { Scanner sc = new Scanner (System.in); // Create Scanner object System.out.print( "Enter your name: " ); String name = sc.nextLine(); // Read String input System.out.print( "Enter your age: " ); int age = sc.nextInt(); // Read integer input System.out.print( "Enter your salary: " ); float salary = sc.nextFloat(); // Re...

Basic Java Program for Operators, Variables, and Control Flow Statements

  1. Variables and Operators in Java A variable is used to store data, and operators perform operations on these variables. Example: Arithmetic Operators public class BasicOperators { public static void main (String[] args) { int a = 10 , b = 5 ; System.out.println( "Sum: " + (a + b)); System.out.println( "Difference: " + (a - b)); System.out.println( "Product: " + (a * b)); System.out.println( "Quotient: " + (a / b)); System.out.println( "Remainder: " + (a % b)); } } Explanation: + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulo) are used to perform arithmetic calculations. 2. Control Flow Statements in Java Control flow statements decide the execution path of the program based on conditions. Example: If-Else Statement public class IfElseExample { public static void main (String[] args) { int num = 10 ; ...