Day 21: Strings in Java Today’s learning was all about understanding how Strings work internally in Java from creation to comparison and concatenation. 🔹 What is a String? A String is a sequence of characters enclosed in double quotes (" " ), whereas a character uses single quotes (' ' ). ➡️ Multiple characters cannot be stored in single quotes. 🔹 Types of Strings Strings are classified into two types: Mutable Strings – values can be changed Immutable Strings – values cannot be changed (default in Java) 🔹 Ways to Create a String in Java Using new keyword Using string literals Using character arrays 🔹 String Comparison Methods Java provides multiple ways to compare strings: == → compares reference (memory address) equals() → compares values/content equalsIgnoreCase() → compares values ignoring case compareTo() → compares character by character (lexicographically) 🔹 Where Are Strings Stored in Memory? Strings are stored in the Heap Segment of the JRE Heap is divided into: Constant Pool – no duplicates allowed Non-Constant Pool – duplicates allowed 🔹 String Pool Concept Strings created without new keyword → Constant Pool Strings created using new keyword → Non-Constant Pool 🔹 String Concatenation String concatenation means combining multiple strings to form a new string. Can be done using: + operator concat() method 📌 Understanding Strings is crucial for memory management and performance in Java. #Day21 #Java #StringsInJava #CoreJava #Programming #LearningJourney #TapAcademy #JavaDeveloper
Understanding Java Strings: Creation, Comparison, and Concatenation
More Relevant Posts
-
🚀 *Understanding Mutable Strings in Java* 🚀 In Java, strings are immutable by default, meaning once created, their values can't be changed. But what if you need to modify strings frequently? That's where *mutable strings* come in! 🔹 *Why Mutable Strings?* Mutable strings allow you to change their content without creating a new object, improving performance and memory efficiency when you need to modify strings often. 🔹 *StringBuilder vs. StringBuffer* Java provides two classes for creating mutable strings: - *StringBuilder*: Not thread-safe, but faster. Ideal for single-threaded environments. - *StringBuffer*: Thread-safe (synchronized), making it suitable for multi-threaded environments, though slightly slower than StringBuilder. 🔹 *Key Differences* - *Synchronization*: StringBuffer is synchronized, StringBuilder isn't. - *Performance*: StringBuilder is generally faster due to lack of synchronization overhead. - *Use Cases*: Choose StringBuilder for single-threaded contexts, StringBuffer for multi-threaded ones. 🔹 *Example Usage* // Using StringBuilder StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); System.out.println(sb.toString()); // Outputs: Hello World // Using StringBuffer StringBuffer sbf = new StringBuffer("Hello"); sbf.append(" World"); System.out.println(sbf.toString()); // Outputs: Hello World 💡 *Learning from Tap Academy* I'm grateful to Tap Academy for helping me deepen my understanding of Java concepts like mutable strings. Their teaching approach makes complex topics easy to grasp! #Java #StringBuilder #StringBuffer #MutableStrings #TapAcademy #Programming #SoftwareDevelopment TAP Academy
To view or add a comment, sign in
-
-
Day 22 – Reference Variables in Java Today I explored an important concept in Object-Oriented Programming — Reference Variables. Unlike primitive variables, reference variables store the address of an object, not the actual value. 🔹 What is a Reference Variable? A reference variable is a non-primitive variable that refers to an object created from a class. It is declared using the class name. Syntax: ClassName referenceVariable; Initialization- referenceVariable = new ClassName(); Or both together: ClassName referenceVariable = new ClassName(); Here: ClassName → Name of the class referenceVariable → Object reference new → Keyword used to create an object ClassName() → Constructor 🔹 Example class Demo5 { int x = 100; int y = 200; } class MainClass3 { public static void main(String[] args) { Demo5 d1 = new Demo5(); System.out.println("x = " + d1.x); System.out.println("y = " + d1.y); System.out.println("modifying x & y"); d1.x = 300; d1.y = 400; System.out.println("x = " + d1.x); System.out.println("y = " + d1.y); } } Output: x = 100 y = 200 modifying x & y x = 300 y = 400 🔹 Important Observation When we write: Demo5 d1 = new Demo5(); Java performs three things: 1️⃣ Creates a reference variable (d1) 2️⃣ Creates a new object in memory 3️⃣ Stores the object reference in the variable #Java #CoreJava #JavaFullStack #OOP #Programming #BackendDevelopment #LearningInPublic #SoftwareDevelopment
To view or add a comment, sign in
-
-
Day 22: Built-in Methods in Strings & compareTo() in Java 🔤 Today’s learning focused on powerful String built-in methods and how Java performs string comparison using compareTo(). 🔹 Built-in Methods in String Class Java provides many useful methods to manipulate strings: length() → Returns the length of the string charAt(index) → Returns character at specified index substring(start, end) → Extracts part of a string indexOf() → Returns first occurrence index lastIndexOf() → Returns last occurrence index replace() → Replaces characters replaceAll() → Replaces using regex toLowerCase() → Converts to lowercase toUpperCase() → Converts to uppercase trim() → Removes leading & trailing spaces split() → Splits string into array 📌 These methods make string manipulation easy and efficient in Java. 🔹 String Comparison using compareTo() The compareTo() method compares two strings lexicographically (alphabetical order) based on ASCII/Unicode values. It performs a three-way comparison: ✅ Returns 0 → if both strings are equal ➕ Returns positive value → if first string is greater ➖ Returns negative value → if first string is smaller ✨ Unlike equals() (two-way comparison), compareTo() helps in sorting strings in ascending or descending order. #Day22 #Java #StringsInJava #CompareTo #CoreJava #Programming #LearningJourney #TAPAcademy
To view or add a comment, sign in
-
-
Day 37 - 🚀 Rules of Method Overriding in Java Method Overriding is a key concept in Object-Oriented Programming (OOP) that allows a subclass to provide a specific implementation of a method already defined in its superclass. It helps achieve Runtime Polymorphism in Java. 📌 Important Rules of Method Overriding: 🔹 1. Same Method Name The method in the subclass must have the same name as in the superclass. 🔹 2. Same Method Parameters The number, type, and order of parameters must be exactly the same. 🔹 3. Return Type The return type must be the same or a covariant type (subtype) of the parent method. 🔹 4. Access Modifier Rule The subclass method cannot reduce visibility. Example: ✔ protected → public ✔ default → protected ❌ public → private 🔹 5. Final Methods Cannot Be Overridden If a method is declared final, it cannot be overridden. 🔹 6. Static Methods Cannot Be Overridden Static methods belong to the class and are method hidden, not overridden. 🔹 7. Private Methods Cannot Be Overridden Private methods are not inherited, so they cannot be overridden. 🔹 8. Exception Handling Rule The child class method cannot throw broader checked exceptions than the parent method. 🔹 9. Use @Override Annotation Using @Override helps the compiler check whether the method is correctly overridden. 💡 Conclusion: Method Overriding enables runtime polymorphism, making Java programs more flexible, maintainable, and scalable. #Java #OOP #MethodOverriding #JavaProgramming #ProgrammingConcepts #SoftwareDevelopment
To view or add a comment, sign in
-
-
🌟 Day 7 of 10 – Core Java Recap: Encapsulation, Inheritance & Access Modifiers 🌟 Continuing my 10-day Core Java revision journey 🚀 Today I revised very important OOP concepts used in real-world applications. 🔐 1️⃣ Encapsulation in Java Encapsulation is the process of wrapping data (variables) and code (methods) into a single unit (class). It is mainly used for data hiding and security. In encapsulation: Variables are declared as private Access is provided using public getter and setter methods Key Benefits: ✔ Data hiding ✔ Controlled access to data ✔ Better code security ✔ Improved maintainability Example concept: Private variables + Public getters/setters = Encapsulation ⚙ 2️⃣ Implementation of Encapsulation Key points: Use private data members Provide public getter() and setter() methods Prevent direct access from outside the class Example: private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } 🧬 3️⃣ Inheritance in Java Inheritance is a mechanism in which one class acquires the properties and behaviors of another class. Real-time relation: Parent Class → Child Class Superclass → Subclass Advantages: ✔ Code reusability ✔ Readability ✔ Maintainability 📚 4️⃣ Types of Inheritance Single Inheritance Multilevel Inheritance Hierarchical Inheritance Hybrid Inheritance (supported using interfaces in Java) Note: Java does not support multiple inheritance using classes to avoid ambiguity (Diamond Problem). 🔓 5️⃣ Access Modifiers in Java Access modifiers define the accessibility (scope) of classes, variables, and methods. Types of Access Modifiers: Public Private Protected Default (No modifier) 📊 6️⃣ Scope of Access Modifiers 🔹 Private Accessible only within the same class Provides maximum data security 🔹 Default Accessible within the same package No keyword is used 🔹 Protected Accessible within the same package Also accessible in subclasses (even in different packages) 🔹 Public Accessible from anywhere in the program Access Level Order: Private < Default < Protected < Public 💡 Key Learnings Today: Understood encapsulation and data hiding Learned how getters and setters control data access Revised inheritance and its types Clearly understood access modifiers and their scope Strengthening my OOP concepts step by step for interviews and real-world development 💻🔥 #Java #CoreJava #OOP #Encapsulation #Inheritance #AccessModifiers #JavaLearning #CodingJourney
To view or add a comment, sign in
-
🚨throw vs throws in Java - One Letter, Completely Different Meaning When I first started learning Java, two keywords confused me a lot: throw & throws They look almost identical... but they do very different things. Let's break it down simply👇 💠throw - Used to actually throw an exception -> Used within methods to explicitly raise an exception instance, allowing one checked or unchecked exception at a time. 🧩Example: if(age < 18){ throw new IllegalArgumentException("Age must be 18 or above"); } Here, the program immediately throws an exception. 💠throws - Used to declare possible exceptions -> Used in method signatures to declare one or more potential checked exceptions, signaling to the caller that the exception must be handled. 🧩Example: public void readFile() throws IOException { FileReader file = new FileReader("data.txt"); } This method itself does not handle the exception - it passes responsibilty to the caller. 🧠Simple way to remember throw->used inside a method (creates) throws->used in method declaration (warns) 💡Understanding this difference helps to: ✅Write cleaner APIs ✅Handle errors properly ✅Make the code easier for others to use. 💬 Quick question for Java developers here: Which exception confused you the most when you were starting out? NullPointerException still haunts many developers 😅 #Java #ExceptionHandling #JavaDeveloper #BackendDevelopment #Programming #SoftwareEngineering #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Learning Java OOP — Understanding Object Class in Java Today I explored one of the most important concepts in Java: **Object Class**, the root of the entire class hierarchy. 🔹 Every class in Java directly or indirectly inherits from `Object` class 🔹 It provides common methods available to all objects 🔹 This is why every object in Java gets default behaviors automatically ✅ Important methods in Object Class: • `toString()` → Converts object data into readable text • `equals()` → Compares two objects • `hashCode()` → Generates unique hash value • `getClass()` → Returns runtime class information • `clone()` → Creates duplicate object • `wait()`, `notify()`, `notifyAll()` → Used in multithreading • `finalize()` → Deprecated method 💡 Key Insight: When we print an object reference, Java internally calls `toString()`. That is why overriding `toString()` helps display object data in a meaningful way. 📌 Object class contains **12 methods + 1 constructor**, and it is called the **parent of all Java classes**. #Java #OOP #ObjectClass #Programming #LearningJourney #JavaDeveloper #SoftwareDevelopment
To view or add a comment, sign in
-
-
🔹 What Does static Mean in Java? In Java, the static keyword means the member belongs to the class, not to the objects of the class. 👉 Static members are loaded into memory only once when the class is loaded. 👉 They are shared among all objects of that class. 🔹 Static Members of a Class A class can contain: ✔ Static Variables ✔ Static Methods ✔ Static Blocks These belong to the class memory (Method Area). Whereas: ❌ Instance variables ❌ Instance methods Belong to the object (heap memory). 🔹 Why Static is Important? 1️⃣ Memory Efficiency Since static members are created only once, they save memory when multiple objects are created. 2️⃣ No Object Required Static methods can be called directly using the class name: 🔹 Rules of Static (Very Important) ✅ Static methods CAN access: Static variables Static methods ❌ Static methods CANNOT directly access: Instance variables Instance methods ❌ Static methods CANNOT use: this keyword super keyword Why? Because static methods belong to the class, and this refers to an object. 🔹 Static Block A static block: Executes only once Runs when the class is loaded Executes before the main method 🔹 Flow of Execution in Java (Important for Interviews) Static variables Static block Main method Object creation Instance block Constructor Instance method I sincerely appreciate the structured learning approach at Tap Academy, which helps in building strong technical fundamentals. A special thanks to Sharth Sir for explaining the concept with exceptional clarity and depth. Your guidance has helped me strengthen my foundation in Core Java and understand concepts beyond just theory. #Java #CoreJava #Programming #OOP #SoftwareDevelopment #LearningJourney #TapAcadem
To view or add a comment, sign in
-
-
Day8 🚀: Variables & Memory Structure in Java Today’s learning was about what is variable and Types of variables and how Java uses RAM when a program executes. and how program execution works in java This helped me understand what happens behind the scenes when we run a Java program. A variable is a container that stores data values. In Java, every variable must be declared with a specific data type. 🔹 Syntax: data Type variable Name = value; 🔹 Example: int age = 21; double salary = 25000.50; char grade = 'A'; String name = "Theja"; 🔹 Types of Variables Covered 1️⃣ Local Variables *Declared inside methods *Stored in Stack memory *Created when method starts and destroyed when method ends 2️⃣ Instance Variables *Declared inside a class but outside methods *Stored in Heap memory *Each object has its own separate copy 🔹 How Java Uses RAM During Execution When we run a Java program, memory is divided into different segments: 🧠 1. Code Segment °Stores the compiled bytecode °Contains the program instructions 🧠 2. Static Segment °Stores static variables °Memory is allocated only once 🧠 3. Stack Segment °Stores local variables °Stores method calls °Works in LIFO (Last In First Out) order 🧠 4. Heap Segment °Stores objects and instance variables °Managed by Garbage Collector 🔹 How Program Execution Works in Java 1️⃣ Code is written and compiled into bytecode 2️⃣ JVM loads the class into memory 3️⃣ main() method is pushed into Stack 4️⃣ Objects are created in Heap 5️⃣ Variables are allocated memory 6️⃣ After execution, unused objects are removed by Garbage Collector 🔹 What I Understood Today Understanding variables is not enough. Knowing where they are stored in memory and how Java manages RAM gives deeper clarity about performance and program behavior. Learning how Java works internally is making my foundation stronger 💻🔥 #Java #MemoryManagement #JVM #Programming #LearningJourney #Day8
To view or add a comment, sign in
-
-
🚀 Learning Core Java – Immutable Strings & String Comparison Today, I learned more about Immutable Strings in Java and the different ways to compare them. In Java, the String class is immutable, which means once a string object is created, its value cannot be changed. Any modification results in a new object being created in memory. Because strings are objects, Java provides multiple built-in methods to compare them in different ways. ⸻ 🔹 1️⃣ == (Reference Comparison) The == operator compares references (memory addresses), not actual content. If two string variables point to the same object, it returns true. Otherwise, false — even if the content is the same. ⸻ 🔹 2️⃣ equals() (Value Comparison) The equals() method compares actual string values (content). It checks whether the characters inside both strings are the same. ⸻ 🔹 3️⃣ compareTo() (Character-by-Character Comparison) The compareTo() method compares strings lexicographically (character by character). • Returns 0 → if both strings are equal • Returns positive value → if first string is greater • Returns negative value → if first string is smaller ⸻ 🔹 4️⃣ equalsIgnoreCase() This method compares string values while ignoring uppercase and lowercase differences. ⸻ 🔹 5️⃣ compareToIgnoreCase() This compares strings character by character, ignoring case differences. ⸻ 🔎 Key Takeaway: • Use == for reference comparison • Use equals() for content comparison • Use compareTo() for sorting or lexicographical comparison • Use ignore-case methods when case sensitivity doesn’t matter Understanding these differences helps avoid common bugs and write more predictable Java programs. Excited to keep strengthening my Java fundamentals! 🚀 #CoreJava #JavaProgramming #ImmutableString #JavaDeveloper #StringComparison #ProgrammingFundamentals #LearningJourney #StudentDeveloper
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