📘 Java Learning | transient vs @Transient (Real-Time Example) Today I learned a very important Java concept that often creates confusion in real projects and interviews 👇 🔹 transient (Core Java) transient is a Java keyword Used to exclude a variable from serialization When an object is converted into a byte stream, transient fields are NOT saved 👉 After deserialization, these fields get default values: null → Objects 0 → Numbers false → Boolean class User implements Serializable { String username; transient String password; } 📌 Real-time use case: Passwords, OTPs, session data should never travel over the network or be stored in files. 🔹 @Transient (JPA / Hibernate) @Transient is a JPA annotation It tells Hibernate NOT to persist the field in the database The field exists only in memory, not in DB tables 🖼️ Image Explanation: The attached image shows: How sensitive fields are skipped during serialization How transient values become default after deserialization How @Transient fields are never stored in DB 📌 Key Takeaway: ✔ transient → controls serialization ✔ @Transient → controls database persistence ✔ Knowing this difference is crucial for real-time projects & interviews #Java #CoreJava #SpringBoot #Hibernate #JPA #BackendDevelopment #JavaDeveloper #Serialization #ProgrammingConcepts #DailyLearning #SoftwareEngineering
Java transient vs @Transient: Serialization and Persistence
More Relevant Posts
-
📌 Understanding the this Keyword in Java (with a simple example) In Java, the this keyword is a reference to the current object of a class. It’s commonly used to differentiate instance variables from method or constructor parameters when they share the same name. Let’s look at a simple example 👇 class Employee { String name; int age; // Constructor Employee(String name, int age) { this.name = name; this.age = age; } void display() { System.out.println("Name: " + this.name); System.out.println("Age: " + this.age); } public static void main(String[] args) { Employee emp = new Employee("Vishnu", 28); emp.display(); } } 🔍 What’s happening here? this.name refers to the instance variable of the current object. name (without this) refers to the constructor parameter. Without this, Java would get confused between the two variables. Using this makes the code clear, readable, and bug-free. ✅ Why this is important? Avoids variable shadowing Improves code clarity Helps in constructor chaining and method calls Essential for writing clean object-oriented code Mastering small concepts like this builds a strong Java foundation 💪 #Java #OOP #Programming #Backend #Learning #CleanCode
To view or add a comment, sign in
-
**Post 7 📘 Effective Java – Item 7** “Eliminate obsolete object references” One of the most **silent bugs in Java** is not a crash… It’s a **memory leak**. Java has Garbage Collection, but **GC is not magic**. If *you* keep references, GC can’t help you. 🔍 **Common mistake** We assume that once an object is “logically unused”, Java will clean it up. But if a reference still exists → **memory leak**. 💡 **Classic example** Implementing your own stack, cache, or listener list. If you pop an element from a stack but don’t null out the reference: ➡️ Object stays in memory ➡️ GC cannot reclaim it ✅ **Best practices** * Set references to `null` once they are no longer needed * Be extra careful with: * Custom data structures * Static fields * Caches * Listeners & callbacks * Prefer **weak references** (`WeakHashMap`) for caches when applicable 🧠 **Key takeaway** > Garbage Collection works on *reachability*, not *usefulness*. Writing clean Java isn’t just about syntax or performance — it’s also about **memory hygiene**. This one habit can save you from **production memory leaks** that are extremely hard to debug. source - Effective Java ~ Josch bloch #EffectiveJava #Java #MemoryManagement #GarbageCollection #CleanCode #SoftwareEngineering #BackendDevelopment
To view or add a comment, sign in
-
☕ Java | Day-76 – TreeSet (Collections Framework) Today I learned about TreeSet in Java and how it stores data in a sorted and unique way. 🌳 What is TreeSet? TreeSet is a class in the Java Collections Framework that implements the Set interface and stores elements in sorted order. ✔️ No duplicate elements ✔️ Elements are sorted automatically ✔️ Uses Red-Black Tree internally 🧠 How TreeSet Works TreeSet sorts elements based on: 🔹 Natural ordering (Comparable) 🔹 Custom ordering (Comparator) Because of sorting, TreeSet is slightly slower than HashSet but more powerful when order matters. ✨ Key Characteristics 🔸 Maintains ascending order 🔸 Does not allow null elements 🔸 Best for sorted & navigable data 🔸 Part of NavigableSet 🔧 Important TreeSet 📌 add(element) – inserts element in sorted position 📌 remove(element) – removes specified element 📌 contains(element) – checks presence 📌 first() / last() – returns first or last element 📌 higher(x) – smallest element greater than x 📌 lower(x) – greatest element smaller than x 📌 ceiling(x) – ≥ x 📌 floor(x) – ≤ x 📌 pollFirst() / pollLast() – removes & returns elements 📌 size() / isEmpty() – collection info 🧩 When to Use TreeSet? ✔️ When sorted output is required ✔️ When duplicates must be avoided ✔️ When range-based operations are needed 📘 TreeSet is ideal when order + uniqueness both matter. 10000 Coders Full Stack Trainer: Gurugubelli Vijaya Kumar #Java #TreeSet #CollectionsFramework #JavaLearning #BackendDevelopment #DSA #Consistency #Day76
To view or add a comment, sign in
-
📘 Day 17 – Java Concept | static vs instance Members Today I revised one of the most fundamental OOP concepts in Java that interviewers use to test understanding of memory, class loading, and object behavior. What I Learned 👇 🔹 static Members Belong to the class, not to individual objects Single copy shared by all instances Loaded into memory once when the class is loaded Accessed using the class name Example: class Demo { static int count = 0; } System.out.println(Demo.count); 🔹 Instance Members Belong to the object Each object has its own copy Created when an object is instantiated Accessed using the object reference Example:class Demo { int value = 10; } Demo d = new Demo(); System.out.println(d.value); 🔹 Golden Rule in Java Use static → For shared data and utility methods Use instance → For object-specific state and behavior Why This Matters Understanding this concept helps in building memory-efficient, scalable Java applications and answering system-level interview questions confidently. #Day17 #Java #CoreJava #OOP #JavaInterview #ProgrammingJourney #PlacementPrep
To view or add a comment, sign in
-
📘 Core Java | Regular Expressions (Regex) What is Regex in Java? - Regex is a pattern based search used to: - Validate input - email, credentials - Search text - Replacement - Extraction - fetch a data from a text - Split strings based on rules In Java, Regex is mainly used through: java.util.regex package String Methods That Use Regex - matches() → check against a pattern - replaceAll() → replaces text matching a pattern - split() → splits string using a regex Regex is widely used for: Email validation Password rules Phone number checks Data parsing Log analysis Instead of writing long conditional logic, one regex can solve complex validation.
To view or add a comment, sign in
-
In this article, we’ll explore: Java program structure & syntax Variables in Java Data types (Primitive & Non-Primitive) Type casting Java naming conventions These are fundamental concepts you must master before moving into logic and problem-solving. https://lnkd.in/gemKfKjJ
To view or add a comment, sign in
-
☕ Java | Day-78 – Map & Hashtable Today I explored Map in Java and went deeper into Hashtable, understanding how key–value storage works and where each is used. 🗂️ What is Map in Java? Map is part of the Java Collections Framework used to store data as key–value pairs. 🔹 Keys are unique 🔹 Values can be duplicated 🔹 Not part of the Collection interface 🧠 Why Use Map? ✔️ Fast data lookup using keys ✔️ Real-world representation (ID → Object, Name → Value) ✔️ Efficient searching and updating 🧩 What is Hashtable? Hashtable is a legacy Map implementation that stores key–value pairs in a thread-safe manner. 🔒 Key Features of Hashtable 🔸 Thread-safe (synchronized) 🔸 No null key or value allowed 🔸 Slower than HashMap due to synchronization 🔸 Part of legacy classes ⚖️ Map vs Hashtable (Conceptual) 🔹 Map → Interface 🔹 Hashtable → Concrete class 🔹 Map allows modern implementations (HashMap, TreeMap) 🔹 Hashtable focuses on thread safety 🔧 Important Hashtable Methods 📌 put(key, value) – insert data 📌 get(key) – retrieve value 📌 remove(key) – delete entry 📌 containsKey(key) – check key existence 📌 containsValue(value) – check value existence 📌 size() / isEmpty() – collection info 📌 keySet() / values() / entrySet() – data access 🎯 When to Use Hashtable? ✔️ In multithreaded environments ✔️ When null values must be avoided ✔️ In legacy systems 📘 Learning Map & Hashtable helped me understand data storage, synchronization, and performance trade-offs in Java. 10000 Coders Full Stack Trainer: Gurugubelli Vijaya Kumar #Java #Map #Hashtable #CollectionsFramework #JavaDeveloper #BackendLearning #DSA #Day78
To view or add a comment, sign in
-
📌 hashCode() in Java In Java, every object inherits the hashCode() method from the Object class. It returns an integer value that represents the object. But hashCode is not about uniqueness — it’s about efficiency. 1️⃣ What is hashCode()? hashCode() returns an integer used by hash-based collections such as: • HashMap • HashSet • Hashtable It helps decide where an object should be stored internally. 2️⃣ hashCode() and equals() Relationship Java contract: • If two objects are equal using equals(), they MUST have the same hashCode • If two objects have the same hashCode, they MAY or MAY NOT be equal This means: Same equals → same hashCode Same hashCode → not necessarily same object 3️⃣ Why hashCode is Important Hash-based collections work in two steps: • First: use hashCode() to find the bucket • Second: use equals() to find the exact object Without hashCode: • Lookup would be slow • Performance would degrade to linear search 4️⃣ What Happens If hashCode Is Not Implemented Properly If hashCode is inconsistent: • Objects may go into wrong buckets • Retrieval may fail • Collections behave unpredictably 5️⃣ Why String Has a Good hashCode • Based on characters • Cached after first computation • Never changes due to immutability 💡 Key Takeaways: - hashCode improves performance, not equality - equals() ensures correctness - Both must be implemented together #Java #CoreJava #hashCode #equals #JVM #BackendDevelopment
To view or add a comment, sign in
-
Until now, our Java programs knew all the values in advance. But real programs become useful only when they can listen to the user. Think about real life: 📝 A form is meaningful only when someone fills it. 🏧 An ATM works only after you enter details. A Java program also needs a way to listen 👂. That’s where user input comes in. 🛠️ 𝐔𝐬𝐞𝐫 𝐈𝐧𝐩𝐮𝐭 𝐢𝐧 𝐉𝐚𝐯𝐚 (𝐔𝐬𝐢𝐧𝐠 𝐒𝐜𝐚𝐧𝐧𝐞𝐫) Java provides a built-in class called Scanner to read input from the user while the program is running. There’s a simple flow involved: -->𝘐𝘮𝘱𝘰𝘳𝘵 𝘵𝘩𝘦 𝘚𝘤𝘢𝘯𝘯𝘦𝘳 𝘤𝘭𝘢𝘴𝘴 Before using Scanner, Java must be told where it comes from.This is done using import java.util.Scanner; -->𝘊𝘳𝘦𝘢𝘵𝘦 𝘢 𝘚𝘤𝘢𝘯𝘯𝘦𝘳 𝘰𝘣𝘫𝘦𝘤𝘵 The Scanner object is created using System.in , which means input will be read from the keyboard ⌨️. -->𝘙𝘦𝘢𝘥 𝘶𝘴𝘦𝘳 𝘪𝘯𝘱𝘶𝘵 Different methods are used based on the data type: • nextLine() → text • nextInt() → whole numbers • nextDouble() → decimal values -->𝘜𝘴𝘦 𝘵𝘩𝘦 𝘪𝘯𝘱𝘶𝘵 Once the input is stored in variables, the program can display it, compare it, or apply logic ⚙️. -->𝘊𝘭𝘰𝘴𝘦 𝘵𝘩𝘦 𝘚𝘤𝘢𝘯𝘯𝘦𝘳 Closing the Scanner releases the input resource after the program finishes ✅. This small setup is what transforms a static program into an interactive one ✨. I’ve also attached a simple Java program that takes input from the user and uses it - feel free to go through it for better understanding. #Java #CoreJava #JavaBasics #LearningJourney #Programming #BuildInPublic
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