Posts

Introduction to Java Programming

  🔰 Introduction to Java Programming Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle Corporation ). It is widely used for building applications across platforms — from desktop to mobile to enterprise systems. ✨ Key Features of Java: Platform Independent : "Write once, run anywhere" — Java code runs on any device with the Java Virtual Machine (JVM). Object-Oriented : Everything in Java is an object; this promotes reusability and modularity. Simple and Familiar : Syntax is similar to C/C++, making it easier for students with prior programming exposure. Secure : Java includes built-in security features like bytecode verification. Robust : Strong memory management and exception handling. Multithreaded : Supports concurrent execution of two or more threads. ✅ Structure of a Java Program public class HelloWorld { public static void main (String[] args) { System.out.println( ...

Java Command line and IDE

  Java Command Line 📌 What is it? The command line (or terminal) is a text-based interface where users can type commands to compile and run Java programs using tools provided by the Java Development Kit (JDK) . ⚙️ Common Java Commands: Command Description javac Compiles Java source code to bytecode .class files java Runs the compiled Java program using the JVM javap Disassembles .class files (shows bytecode info) java -version Shows the installed Java version ✅ Example (on terminal or command prompt): javac HelloWorld.java # Compiles the file java HelloWorld # Runs the program 🔍 Pros: Lightweight, fast Helps understand the compilation and execution process Good for beginners and scripting 🚫 Cons: No auto-completion, debugging, or GUI support Can be slow for large projects IDE (Integrated Development Environment) 📌 What is an IDE? An IDE is a software application that provides tools to help programmers write, test, and debug their co...

Java Compiler and Java Virtual Machine

  ⚙️ Java Compiler and Java Virtual Machine (JVM) Java uses a two-step process to run programs: Step 1: Java Compiler ( javac ) The Java compiler is a program that: Converts Java source code ( .java file) into bytecode ( .class file). Bytecode is an intermediate, platform-independent code. 📌 Example : When you write: public class HelloWorld { public static void main (String[] args) { System.out.println( "Hello, World!" ); } } Save it as HelloWorld.java . Now compile it: javac HelloWorld.java This creates a file called HelloWorld.class — which contains bytecode . Step 2: Java Virtual Machine (JVM) The JVM is a virtual engine that: Reads and executes the bytecode ( .class file). Converts bytecode to machine-specific code using Just-In-Time (JIT) compilation . Ensures that the same .class file can run on any device with a JVM (Windows, Mac, Linux, etc.). 📌 To run the program: java HelloWorld The JVM runs ...

Type casting in Java

  ✅ What is Type Casting? Type Casting in Java is the process of converting a value from one data type to another. Java has two types of type casting: Widening (Implicit) Casting – small → large type Narrowing (Explicit) Casting – large → small type 🔹 1. Widening Type Casting (Automatic) Definition : Converting a smaller data type to a larger one. 👉 Safe conversion – done automatically by Java. 🔸 Example: public class WideningExample { public static void main (String[] args) { int a = 10 ; double b = a; // int → double System.out.println( "int value: " + a); System.out.println( "double value: " + b); } } Output : int value: 10 double value: 10.0 ✅ Widening Order: byte → short → int → long → float → double 🔹 2. Narrowing Type Casting (Manual) Definition : Converting a larger data type to a smaller one. 👉 Not safe automatically, so explicit casting is required. 🔸 ...

Primitive Data Types and Wrapper Classes

  1. Primitive Data Types in Java Java has 8 primitive data types , which are the most basic types of data built into the language. They are not objects and are stored directly in memory for performance. Type Size Default Value Description Example byte 1 byte (8-bit) 0 Small integers (-128 to 127) byte a = 10; short 2 bytes 0 Larger than byte (-32,768 to 32,767) short b = 1000; int 4 bytes 0 Common for integers int c = 12345; long 8 bytes 0L Very large integers long d = 100000L; float 4 bytes 0.0f Single-precision decimal float e = 3.14f; double 8 bytes 0.0d Double-precision decimal (default) double f = 3.14159; char 2 bytes '\u0000' Single 16-bit Unicode character char g = 'A'; boolean 1 bit (virtual) false Logical true/false boolean h = true; 📦 2. Wrapper Classes in Java Java provides wrapper classes in the java.lang package for each primitive data type. These allow primitives to be treated as objects . Primitive Wrapper Class byte Byte short Short int Intege...

Operators in Java

  ✅ Java Operators – Complete Guide In Java, operators are special symbols used to perform operations on variables and values. They are grouped into several categories: 🔹 1. Arithmetic Operators Used for basic mathematical operations. Operator Description Example ( a = 10 , b = 5 ) Result + Addition a + b 15 - Subtraction a - b 5 * Multiplication a * b 50 / Division a / b 2 % Modulus (Remainder) a % b 0 🔹 2. Relational (Comparison) Operators Used to compare two values, returning a boolean ( true or false ). Operator Description Example Result == Equal to a == b false != Not equal to a != b true > Greater than a > b true < Less than a < b false >= Greater than or equal a >= b true <= Less than or equal a <= b false 🔹 3. Logical Operators (used with boolean values) Operator Name Description Example && Logical AND      True if both conditions are true (a > 0 && b > 0) → true || Logical OR  True if ...

Operator Precedence in Java

  ✅ Operator Precedence in Java Operator precedence determines the order in which operators are evaluated in expressions. Operators with higher precedence are evaluated before operators with lower precedence. If operators have the same precedence , their associativity (left-to-right or right-to-left) determines the order. 🔹 Table of Java Operator Precedence and Associativity Precedence Level Operators Description Associativity 1 (Highest) () Parentheses (grouping) Left to right [] , . , obj.method() , a[i] , x.y Array/member access, method call Left to right 2 ++ , -- , + (unary), - (unary), ~ , ! Unary operators Right to left 3 * , / , % Multiplicative Left to right 4 + , - Additive Left to right 5 << , >> , >>> Bitwise shift Left to right 6 < , <= , > , >= , instanceof Relational Left to right 7 == , != Equality Left to right 8 & Bitwise AND Left to right 9 ^ Bitwise XOR Left to right 10 ` ` Bitwise OR 11 && Logical AND Lef...