Posts

Showing posts with the label Java Basics

Conditional Statements in Java Explained with Syntax

Conditional Statements in Java Conditional statements are used to control the flow of execution in a Java program based on certain conditions. They allow the program to make decisions and execute different blocks of code. Java provides the following conditional statements: if statement if-else statement if-else-if-else statement switch statement Real-Life Examples of Conditional Statements in Java 1. if Statement – Real-Life Example 📌 Example: Checking age for voting Real life: If a person’s age is 18 or more, they are eligible to vote. int age = 20; if (age >= 18) {     System.out.println("Eligible to vote"); } 👉 If the condition is true, the message is printed. 👉 If false, nothing happens. 2. if-else Statement – Real-Life Example 📌 Example: Pass or Fail result Real life: If marks are greater than or equal to 40, the student passes; otherwise, they fail. int marks = 35; if (marks >= 40) {     System.out.println("Pass"); } else {     System.out.pr...

Operators in Java Explained: Arithmetic, Relational, and Logical

Operators in Java Operators are special symbols used to perform operations on variables and values in Java. Java provides several types of operators to perform arithmetic, logical, relational, and other operations. 1. Arithmetic Operators       Used to perform basic mathematical operations. + Addition - Subtraction * Multiplication / Division % Modulus (remainder) 2. Relational Operators      Used to compare two values and return a boolean result (true or false). > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to 3. Compound Assignment Operators       Used to perform an operation and assign the result to the same variable. += Add and assign -= Subtract and assign *= Multiply and assign /= Divide and assign %= Modulus and assign 4. Unary Operators      Used to operate on a single operand. ++ Increment -- Decrement ! Logical negation Increment Operators Post-inc...

Final Keyword in Java Explained with Examples

  Final Keyword in Java The final keyword in Java is used to make a variable’s value constant, meaning the value cannot be changed once assigned. Final Variables When a variable is declared as final: The variable must be initialized once Reinitialization is not allowed Any attempt to change the value results in a compile-time error Example : final int x = 10; // x = 20; // Compile-time error Final with Static and Instance Variables When a static variable or instance variable is declared as final, it does not support default values Such variables must be declared and initialized together The value must be assigned only once Valid syntax: class Example {     static final int A = 100;     final int B = 50; } Purpose of Final Keyword The final keyword is used to: Create constant values Prevent accidental modification of variables Improve code readability and safety

Variables in Java Explained: Local, Static, and Instance

Variables in Java A variable is a symbolic name associated with a value. The value of a variable can change during program execution. In Java, variables are classified into three types: Local Variable Static Variable Instance Variable 1. Local Variable A local variable is declared inside a method, constructor, or block. Key points: Also called a method-level variable Accessible only within the block {} where it is declared Does not support default values Must be initialized before use If declared but not initialized and used, it causes a compile-time error If declared but not used, no error is generated Example : void display() {     int x = 10; // local variable     System.out.println(x); } 2. Static Variable A static variable is declared inside a class but outside any method, using the static keyword. Key points: Also called a class-level variable Supports default values Memory is allocated once and shared among all objects Can be accessed:     By variabl...

Primitive Data Types in Java with Size and Default Values

Primitive Data Types in Java Primitive data types are basic data types in Java that determine the type and amount of memory required to store a value. Java provides a fixed set of primitive data types for efficient memory usage. There are two main categories of primitive data types: Numeric Non-Numeric 1. Numeric Data Types Numeric data types are used to store numerical values. They are further divided into two types: a) Integer Data Types Integer data types store whole numbers (without decimal points). byte- 8 bits short- 16 bits int- 32 bits long- 64 bits b) Floating-Point Data Types Floating-point data types store decimal numbers. float- 32 bits double- 64 bits 2. Non-Numeric Data Types Non-numeric data types are used to store non-numerical values. char – Stores a single character and occupies 16 bits of memory boolean – Stores true or false (Memory size is JVM-dependent) Default Values in Java A default value is a value automatically assigned by the compiler when a data type is d...

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

What Is JDK? End Users, Components, and Configuration Guide

JDK (Java Development Kit) The JDK (Java Development Kit) is a software package required to develop, compile, and run Java applications. It contains all the tools needed by developers and users to work with Java programs. The JDK includes: Java Compiler (javac) Java Runtime Environment (JRE) Java Virtual Machine (JVM) Java libraries and development tools End Users of JDK Software 1. Programmer A programmer needs the JDK to perform the following tasks: Writing Java programs (coding) Compiling Java source code Executing Java applications For these tasks, the programmer must install JDK software, which provides: Java Compiler – used for compilation Java Libraries – used during coding and execution JVM (Java Virtual Machine) – used for program execution Without JDK, a programmer cannot develop Java applications. 2. Client A client (end user) is required to only execute Java applications, not write or compile them. For execution purposes, the client needs: Java Libraries JVM (Java Virtual M...

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

Arrays in Java: Definition, Types, Syntax, and Real-Life Examples

📌 Array in Java (Professional & Beginner-Friendly Explanation) 🔹 What is an Array? An array in Java is an object that is used to store multiple values of the same (homogeneous) data type under a single variable name. Arrays store data using index positions. The size of an array is fixed once it is created. Array indexing starts from 0 and ends at size – 1. When an array is created in Java, an internal class is generated by the JVM. This internal class contains a final variable called length, which stores the size of the array. 🔹 Key Characteristics of Arrays Stores homogeneous data only Index-based data access Fixed size (cannot grow or shrink) Faster data access compared to collections 🔹 Syntax to Create an Array in Java 1️⃣ Using new keyword dataType[] arrayName = new dataType[size]; Example: int[] numbers = new int[5]; 2️⃣ Using Array Literal dataType[] arrayName = {value1, value2, value3}; Example: int[] numbers = {10, 20, 30, 40}; 🔹 Types of Arrays in Java 1️⃣ Primitive D...

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.

Basics of Java Programming: Keywords, Literals, and Identifiers

Basics of Java Program The basics of a Java program include keywords, literals, and identifiers. These are the fundamental building blocks used while writing Java code. 1. Keywords Keywords are reserved words in Java that have predefined meanings and functionality for the Java compiler. Key points: Keywords must be written in lowercase. Java has 50 reserved keywords. Out of these, 48 keywords have predefined meaning and functionality. The remaining 2 reserved words (goto and const) are not used and have no functionality. Examples of Java keywords: class static final public void Keywords cannot be used as variable names, method names, or identifiers. 2. Literals Literals are fixed values that are directly stored in a Java program. Java supports five types of literals: Integer Literals: Eg:- 1, 25, 100 Floating-Point Literals: Eg:- 1.5, 2.75 Character Literals: Eg:- 'a', 'Z' String Literals: Eg:- "Java", "Programming" Boolean Literals: Eg:- true, false...