🚀 Starting My Java Learning Journey – Day 5 🔹 Topic: Control Statements in Java Control statements are used to control the flow of execution of a program based on certain conditions. In Java, the main control statements are: 1️⃣ if Statement The if statement executes a block of code only if the condition is true. Example: public class Main { public static void main(String[] args) { int num = 10; if (num > 0) { System.out.println("Number is positive"); } } } 2️⃣ if-else Statement The if-else statement executes one block of code if the condition is true and another block if it is false. Example: public class Main { public static void main(String[] args) { int num = -5; if (num > 0) { System.out.println("Positive number"); } else { System.out.println("Negative number"); } } } 3️⃣ switch Statement The switch statement is used when we want to compare one value with multiple possible cases. Example: public class Main { public static void main(String[] args) { int day = 2; switch(day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; default: System.out.println("Other day"); } } } 💡 Key Point: Control statements help make programs dynamic and decision-based. #Java #JavaLearning #Programming #BackendDevelopment #CodingJourney #ControlStatements
Java Control Statements Explained
More Relevant Posts
-
Exception Handling in Java – My Learning Journey Today, I explored one of the most important concepts in Java – Exception Handling. 📌 What is an Exception? An exception is an unusual activity that occurs during the execution of a program due to faulty input, leading to abrupt termination. 💡 For example: When we divide a number by zero, the program stops immediately, and the remaining lines are not executed. This is called abrupt termination. ⚙️ Key Understanding - Exceptions do not occur at compile time - They occur during runtime (execution time) - When an exception happens: - An exception object is created - It is sent to the runtime system - If no handling is present → Default Exception Handler terminates the program 🛠️ How to Handle Exceptions? We use try-catch blocks to handle exceptions and ensure smooth program execution. ✔️ Risky code is placed inside "try" ✔️ Handling logic is written in "catch" This helps in avoiding abrupt termination and allows the program to continue normally. 🔁 Multiple Catch Blocks We can use multiple "catch" blocks for a single "try". 👉 Important rules: - Only one catch block executes based on the exception - Always write specific exceptions first - Keep generic exception (Exception e) at the end 📚 Types of Exceptions I Learned - Arithmetic Exception (e.g., division by zero) - Negative Array Size Exception - Input Mismatch Exception - Array Index Out of Bounds Exception - Null Pointer Exception 🎯 Key Takeaway Exception handling helps us prevent abrupt termination and ensures the program runs smoothly and safely. 💭 Learning Java step by step and building strong fundamentals in OOP and error handling! #Java #ExceptionHandling #Programming #CodingJourney #OOP #Learning #SoftwareDevelopment
To view or add a comment, sign in
-
-
🚀 Starting My Java Learning Journey – Day 6 🔹 Topic: Loops in Java Loops in Java are used to execute a block of code repeatedly until a certain condition is met. Java mainly provides three types of loops: 1️⃣ for Loop Used when the number of iterations is known. Example: public class Main { public static void main(String[] args) { for(int i = 1; i <= 5; i++) { System.out.println(i); } } } 2️⃣ while Loop Used when the number of iterations is not known beforehand. Example: public class Main { public static void main(String[] args) { int i = 1; while(i <= 5) { System.out.println(i); i++; } } } 3️⃣ do-while Loop The do-while loop executes the code at least once even if the condition is false. Example: public class Main { public static void main(String[] args) { int i = 1; do { System.out.println(i); i++; } while(i <= 5); } } 💡 Key Point: Loops help automate repetitive tasks and make programs more efficient. #Java #JavaLearning #Programming #BackendDevelopment #CodingJourney #JavaLoops
To view or add a comment, sign in
-
Day 41/200 - Java Learning Journey 🌱 ✨ Sharing what I learned today in Java. 🔆Learn a little every day, and one day you will realize how far you’ve come. 📚 Today I learned about Conditional Statements in java Today's Topic: Conditional Statements (if) 📍 Conditional Statements : 👉 These are also known as Decision Making Statements. 📍 If: 👉 The condition which is passed within the if will always check the boolean values and also the resultant will be boolean value. 👉 It is used to check only the one condition. 👉 Executes a block of code only if the condition is true. 👉 Syntax: if (condition){ //stmts } 📌 if(condition) without curly braces ,in that time the condition will be applied to the first line of the statement. Example 1: Checking if a number is positive or not. public class IfExample { public static void main(String[] args) { int number = 10; if (number > 0) { // Checks if the number is greater than 0 System.out.println("The number is positive."); } System.out.println("End of program."); } } O/p: The number is positive. End of program. Example 2: if Statement Skipping Execution. public class IfExample { public static void main(String[] args) { int number = -5; if (number > 0) { // This condition is false System.out.println("The number is positive."); } System.out.println("End of program."); } } O/p: End of program. #Java#DailyLearning#Consistency#ConditionalStatements#Meghana M#10000 Coders#
To view or add a comment, sign in
-
Day 21 of Learning Java Continuing my Java journey! On Day 21, I learned about: ✅ Immutable Classes ✅ How Immutable Objects Work ✅ Defensive Copying ✅ Why String is Immutable ✅ Common Interview Questions on Immutability 🔹 What is an Immutable Class? An immutable class is a class whose object state cannot be changed after it is created. Once the object is initialized, its data remains constant throughout its lifetime. A common example in Java is the String class. 🔹 Key Rules to Create an Immutable Class To make a class immutable: • Declare the class as final • Make all variables private and final • Initialize variables through the constructor • Do not provide setter methods • If the class contains mutable objects, use defensive copying 🔹 What is Defensive Copying? Defensive copying means returning a copy of an object instead of the original reference so that the internal state of the object cannot be modified externally. 🔹 Why is String Immutable? (Very Common Interview Question) String is immutable mainly because of: • Security (used in networking, file paths, class loading) • Thread safety • String Pool optimization • Hashcode caching for performance 1️⃣ What is an immutable object? An object whose state cannot change after creation. 2️⃣ Why are immutable objects thread-safe? Because their state cannot be modified after creation. 3️⃣ Can immutable classes contain mutable objects? Yes, but they must use defensive copying. 4️⃣ Give examples of immutable classes in Java. String, Integer, Boolean, LocalDate. 5️⃣ What is the advantage of immutability? Better security, thread safety, and predictability. special thanks to Aditya Tandon Rohit Negi sir
To view or add a comment, sign in
-
-
🚀 Day 29 | Core Java Learning Journey 📌 Topic: TreeSet in Java Today, I learned about TreeSet, an important class in the Java Collections Framework used when we need sorted and unique elements. 🔹 TreeSet in Java ✔ TreeSet is a class that implements NavigableSet ✔ It also indirectly implements SortedSet and Set ✔ Introduced in JDK 1.2 ✔ Stores unique elements only (no duplicates allowed) 🔹 Data Structure Used ✔ Based on Self-Balancing Binary Search Tree (Red-Black Tree) ❗ (important correction) ✔ Elements are stored in sorted order 🔹 Key Features ✔ Does NOT follow insertion order ✔ Follows natural sorting order (default) ✔ Allows custom sorting using Comparator ✔ Does NOT allow null elements ❌ ✔ Stores homogeneous data (same type, for proper comparison) 📌 Important Methods • add() – add element • remove() – delete element • contains() – check element • first() – returns first (smallest) element • last() – returns last (largest) element • higher() – next greater element • lower() – next smaller element 📌 Performance ✔ Operations like add, remove, search → O(log n) 📌 When to Use TreeSet? ✔ When you need: ✅ Sorted data ✅ Unique elements ✅ Range-based operations 💡 Note: Unlike HashSet, TreeSet focuses on sorting rather than speed. 🙏 Special thanks to Vaibhav Barde Sir for the guidance! 🔥 #CoreJava #JavaLearning #JavaDeveloper #TreeSet #SortedSet #NavigableSet #JavaCollections #Programming #LearningJourney
To view or add a comment, sign in
-
-
Day 42/200 - Java Learning Journey 🌱 ✨ Sharing what I learned today in Java. 🔆Success is built on discipline, consistency, and hard work. 📚 Today I learned about Conditional Statements in java. Today's Topic: Conditional Statements (if-else) 📍 If-Else: 👉 Else will be executed when all the conditions becomes false. 👉 if, if-else is going to check only one condition at a time 👉 It will execute if block (or) else block not both. 👉 if-else Statement – Executes one block if the condition is true, otherwise executes another block. 👉 Syntax: if(condition){ //Code to execute if condition is true } else{ // Code to execute if condition is false } 👉 The if-else statement is used when we want to execute one block of code if the condition is true and another block if the condition is false. Example: public class IfElseExample { public static void main(String[] args) { int number = -5; if (number > 0) { System.out.println("The number is positive."); } else { System.out.println("The number is not positive."); } System.out.println("End of program."); } } O/p: The number is not positive. End of program. #Java#DailyLearning#Consistency#If-else#ConditionalStatements#Meghana M#10000 Coders#
To view or add a comment, sign in
-
Day 42 of My Java Learning Journey – Rules of Interface Today I explored the Rules of Interfaces in Java, which play a crucial role in achieving abstraction, polymorphism, and loose coupling in object-oriented programming. An Interface acts like a contract that defines what a class must implement. 🔹 Key Rules of Interfaces in Java: 1️⃣ Interface acts as a contract When a class implements an interface, it must provide implementation for its methods. 2️⃣ Interfaces promote polymorphism An interface reference can point to objects of implementing classes, helping achieve flexibility and loose coupling. 3️⃣ Methods in an interface are automatically public and abstract Example: void fun(); → public abstract void fun(); 4️⃣ Specialized methods cannot be accessed directly using an interface reference They can be accessed by downcasting the interface reference. 5️⃣ If a class partially implements an interface, it must be declared abstract. 6️⃣ A class can implement multiple interfaces This avoids the diamond problem, since interfaces do not have constructors. 7️⃣ An interface cannot implement another interface Because interfaces only declare methods, not implementations. 8️⃣ An interface can extend another interface It can also extend multiple interfaces, enabling multiple inheritance in Java. 9️⃣ A class can extend a class and implement an interface simultaneously Order must be: extends → implements 🔟 Variables inside interfaces are automatically: public static final (constants) 1️⃣1️⃣ Marker Interface An empty interface used to mark a class (e.g., Serializable). 1️⃣2️⃣ Objects of interfaces cannot be created But interface references can point to implementing class objects, enabling polymorphism. #Java #JavaLearning #Interfaces #ObjectOrientedProgramming #ProgrammingJourney #DeveloperLearning
To view or add a comment, sign in
-
-
🚀 Starting My Java Learning Journey – Day 9 🔹 Topic: Method Overloading in Java Method Overloading is a feature in Java that allows a class to have multiple methods with the same name but different parameters. It helps improve code readability and flexibility. 📌 Ways to Achieve Method Overloading 1️⃣ Different number of parameters 2️⃣ Different data types of parameters 📌 Example Program public class Main { // Method with two int parameters static int add(int a, int b) { return a + b; } // Method with three int parameters static int add(int a, int b, int c) { return a + b + c; } public static void main(String[] args) { System.out.println(add(5, 10)); System.out.println(add(5, 10, 15)); } } Output: 15 30 💡 Key Points: ✔ Method overloading allows multiple methods with the same name ✔ Methods must differ in number or type of parameters ✔ Helps make programs more flexible and readable #Java #JavaLearning #Programming #BackendDevelopment #CodingJourney #MethodOverloading
To view or add a comment, sign in
-
🚀 Learning Update: Understanding How a Java Program Actually Executes Today’s session helped me gain a deeper understanding of how Java programs execute internally beyond just writing code. 🔹 Java Compilation Process A Java program written in high-level language is first compiled using the Java Compiler (javac) which converts it into bytecode (.class files). If a file contains multiple classes, the compiler generates separate class files for each class. 🔹 Program Execution in JVM When we run a Java program: 1️⃣ The JVM loads the class containing the main method. 2️⃣ The bytecode is converted into machine-level instructions. 3️⃣ The program is executed inside the Java Runtime Environment (JRE). 🔹 Memory Structure in Java (JRE) During execution, the program uses different memory areas: • Code Segment – Stores compiled bytecode • Stack Segment – Stores method stack frames • Heap Segment – Stores objects created using new • Static Segment – Stores static variables 🔹 Role of Class Loader The Class Loader dynamically loads classes into memory when they are required during program execution. 🔹 Static vs Instance Concept • Static elements → belong to the class • Instance elements → belong to objects This concept explains why static methods can be accessed without creating an object, while instance methods require object creation. 💡 Key Insight: Understanding how JVM, class loading, memory segments, and object creation work internally helps in writing better and more optimized Java programs. Excited to keep exploring deeper concepts in Core Java and Object-Oriented Programming. #Java #JVM #Programming #OOP #SoftwareDevelopment #LearningJourney #JavaDeveloper TAP Academy
To view or add a comment, sign in
-
-
📘 **Day 17 – Java Learning Journey** Today I explored one of the most important concepts in Java: **Strings**. Here are some key points I learned while practicing and analyzing my notes: 🔹 **What is a String?** A String is a sequence (collection) of characters enclosed within double quotes. Example: `"Java"` 🔹 **Strings are Objects in Java** Unlike primitive data types, Strings are objects created from the `String` class. 🔹 **Immutable Nature of Strings** Strings in Java are **immutable**, meaning once a String object is created, its value cannot be changed. 🔹 **Different Ways to Create Strings** 1️⃣ `String s = "Java";` → Stored in the **String Constant Pool** 2️⃣ `String s = new String("Java");` → Stored in **Heap Memory** 3️⃣ Using character arrays 🔹 **String Comparison Methods** ✔ `==` → Compares **references (memory location)** ✔ `.equals()` → Compares **values/content** ✔ `.compareTo()` → Compares **character by character** ✔ `.equalsIgnoreCase()` → Compares **ignoring case differences** 🔹 **String Concatenation** Strings can be combined using: • `+` operator • `concat()` method Understanding concepts like **String Constant Pool, Heap Memory, and Reference Comparison** helped me clearly see how Java manages memory. Every day I’m learning something new and strengthening my Java fundamentals step by step. 🚀 #Java #Programming #LearningJourney #JavaDeveloper #ComputerScience #Coding TAP Academy
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development