<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pravanjan Amanta</title>
    <description>The latest articles on DEV Community by Pravanjan Amanta (@pravanjan17p).</description>
    <link>https://dev.to/pravanjan17p</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2294538%2Fd4c6288c-cd5f-4e79-bded-95ac98060787.jpg</url>
      <title>DEV Community: Pravanjan Amanta</title>
      <link>https://dev.to/pravanjan17p</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pravanjan17p"/>
    <language>en</language>
    <item>
      <title>🚀 Mastering JDBC: Bridging Java and Databases Seamlessly 📊</title>
      <dc:creator>Pravanjan Amanta</dc:creator>
      <pubDate>Fri, 03 Jan 2025 18:32:53 +0000</pubDate>
      <link>https://dev.to/pravanjan17p/mastering-jdbc-bridging-java-and-databases-seamlessly-f63</link>
      <guid>https://dev.to/pravanjan17p/mastering-jdbc-bridging-java-and-databases-seamlessly-f63</guid>
      <description>&lt;h2&gt;
  
  
  JDBC IN JAVA
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;JDBC (Java Database Connectivity) is a key API in Advanced Java that allows Java applications to interact with databases. It provides a standard way to connect to a database, execute SQL queries, and retrieve results. Below is a comprehensive overview:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Key Components of JDBC
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;DriverManager: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Manages a list of database drivers and establishes a connection between the application and the database.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Connection: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Represents the session between the Java application and the database.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Statement: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Executes static SQL queries against the database.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;PreparedStatement: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Used to execute parameterized SQL queries, improving security and performance.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;CallableStatement: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Used for executing stored procedures in the database.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;ResultSet: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Represents the results of a query, allowing navigation through rows of data.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Steps to Use JDBC
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Load the Driver:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Class.forName("com.mysql.cj.jdbc.Driver"); // Load MySQL Driver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Establish a Connection:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Connection con = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/yourDatabaseName", "username", "password");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a Statement:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Statement stmt = con.createStatement();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Execute a Query:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ResultSet rs = stmt.executeQuery("SELECT * FROM yourTableName");
while (rs.next()) {
    System.out.println(rs.getString("columnName"));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Close the Connection:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rs.close();
stmt.close();
con.close();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Advantages of JDBC
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Platform Independence: Works on any platform with a supported Java environment.&lt;/li&gt;
&lt;li&gt;Supports Multiple Databases: Compatible with various databases like MySQL, Oracle, PostgreSQL, etc.&lt;/li&gt;
&lt;li&gt;Dynamic SQL Execution: Provides flexibility to execute both static and dynamic SQL.&lt;/li&gt;
&lt;li&gt;Database Metadata: Allows retrieval of database and table structure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Types of JDBC Drivers
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Type 1: JDBC-ODBC Bridge Driver (Deprecated)&lt;/li&gt;
&lt;li&gt;Type 2: Native-API Driver&lt;/li&gt;
&lt;li&gt;Type 3: Network Protocol Driver&lt;/li&gt;
&lt;li&gt;Type 4: Thin Driver (Pure Java driver, widely used)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Common JDBC Operations
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Insert Data:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String query = "INSERT INTO employees (id, name) VALUES (?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setInt(1, 101);
pstmt.setString(2, "John Doe");
pstmt.executeUpdate();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Update Data:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String query = "UPDATE employees SET name = ? WHERE id = ?";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "Jane Doe");
pstmt.setInt(2, 101);
pstmt.executeUpdate();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Delete Data:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String query = "DELETE FROM employees WHERE id = ?";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setInt(1, 101);
pstmt.executeUpdate();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example Program
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.sql.*;

public class JdbcExample {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/yourDatabase";
        String user = "root";
        String password = "password";

        try (Connection con = DriverManager.getConnection(url, user, password);
             PreparedStatement pstmt = con.prepareStatement("SELECT * FROM yourTable")) {

            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✨ More update to connect:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/Prabhanjan-17p" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/pravanjan-17p/" rel="noopener noreferrer"&gt;Linkedin&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>jdbc</category>
      <category>programming</category>
      <category>sql</category>
    </item>
    <item>
      <title>🚀 Mastering Generics in Java: The Ultimate Guide to Type-Safe and Reusable Code 💻</title>
      <dc:creator>Pravanjan Amanta</dc:creator>
      <pubDate>Thu, 02 Jan 2025 19:28:14 +0000</pubDate>
      <link>https://dev.to/pravanjan17p/mastering-generics-in-java-the-ultimate-guide-to-type-safe-and-reusable-code-293</link>
      <guid>https://dev.to/pravanjan17p/mastering-generics-in-java-the-ultimate-guide-to-type-safe-and-reusable-code-293</guid>
      <description>&lt;h2&gt;
  
  
  Generics in Java: A Comprehensive Guide
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Generics in Java are a powerful feature introduced in Java 5 that allow developers to write reusable and type-safe code. While primarily used in collections, their applications extend far beyond. Generics enable you to specify types at compile-time, reducing runtime errors and enhancing code readability.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Key Concepts of Generics
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Type Safety&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generics ensure that only the specified type of data can be added to a collection or used in a method, preventing runtime ClassCastException.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Code Reusability&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A single generic class, method, or interface can work with various types of data, reducing redundancy and promoting code reuse.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How Generics Work
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Generic Classes
&amp;gt; Generic classes allow you to define a class that works with any data type. Here's an example:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Box&amp;lt;T&amp;gt; {
    private T item;

    public void setItem(T item) {
        this.item = item;
    }

    public T getItem() {
        return item;
    }
}

public class Main {
    public static void main(String[] args) {
        Box&amp;lt;String&amp;gt; stringBox = new Box&amp;lt;&amp;gt;();
        stringBox.setItem("Hello");
        System.out.println(stringBox.getItem());

        Box&amp;lt;Integer&amp;gt; intBox = new Box&amp;lt;&amp;gt;();
        intBox.setItem(123);
        System.out.println(intBox.getItem());
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Generic Methods
&amp;gt; Generic methods allow type parameters to be used in method definitions.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Util {
    public static &amp;lt;T&amp;gt; void printArray(T[] array) {
        for (T element : array) {
            System.out.println(element);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Integer[] intArray = {1, 2, 3};
        String[] strArray = {"A", "B", "C"};
        Util.printArray(intArray);
        Util.printArray(strArray);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Bounded Type Parameters
&amp;gt; Type parameters can be constrained using extends or super.&lt;/li&gt;
&lt;li&gt;Upper Bound (extends): Ensures the type is a subclass of the specified type.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MathUtils {
    public static &amp;lt;T extends Number&amp;gt; double square(T number) {
        return number.doubleValue() * number.doubleValue();
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(MathUtils.square(5));    // Integer
        System.out.println(MathUtils.square(5.5)); // Double
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Lower Bound (super): Ensures the type is a superclass of the specified type.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Wildcards
&amp;gt; Wildcards (?) represent an unknown type and offer flexibility when working with generics.&lt;/li&gt;
&lt;li&gt;Unbounded Wildcard:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void printList(List&amp;lt;?&amp;gt; list) {
    for (Object item : list) {
        System.out.println(item);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Upper Bounded Wildcard:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void printNumbers(List&amp;lt;? extends Number&amp;gt; list) {
    for (Number num : list) {
        System.out.println(num);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Lower Bounded Wildcard:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void addNumbers(List&amp;lt;? super Integer&amp;gt; list) {
    list.add(10); // Only Integer or its subclass can be added
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Generics and Collections
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The Java Collections Framework heavily relies on generics to provide type safety.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Example: Using Generics with Lists
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList&amp;lt;String&amp;gt; names = new ArrayList&amp;lt;&amp;gt;();
        names.add("Alice");
        names.add("Bob");

        for (String name : names) {
            System.out.println(name);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Example: Using Generics with Maps
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        HashMap&amp;lt;Integer, String&amp;gt; map = new HashMap&amp;lt;&amp;gt;();
        map.put(1, "One");
        map.put(2, "Two");

        for (Integer key : map.keySet()) {
            System.out.println("Key: " + key + ", Value: " + map.get(key));
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Advantages of Generics
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Type Safety: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Prevents runtime errors by catching type mismatches at compile time.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Code Reusability: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Allows writing flexible, reusable code for any type.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Performance:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reduces runtime overhead by eliminating the need for explicit typecasting.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Limitations of Generics
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Type Erasure:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generics are implemented using type erasure, so the type information is not available at runtime.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Primitive Types Not Allowed:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generics work only with objects. Use wrapper classes like Integer for int.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;No Static Members:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generic classes cannot define static fields or methods using the type parameter.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Generics in Java are essential for writing robust, maintainable, and efficient code. By leveraging type safety and code reusability, developers can handle data more effectively. Whether you're working with collections or creating custom classes, mastering generics is crucial for advanced Java development.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>java</category>
      <category>webdev</category>
      <category>100daysofcode</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Thread Concurrency In Java</title>
      <dc:creator>Pravanjan Amanta</dc:creator>
      <pubDate>Thu, 31 Oct 2024 21:09:56 +0000</pubDate>
      <link>https://dev.to/pravanjan17p/thread-concurrency-in-java-idb</link>
      <guid>https://dev.to/pravanjan17p/thread-concurrency-in-java-idb</guid>
      <description>&lt;p&gt;Thread Concurrency or Multithreading in advanced Java allows multiple threads to execute concurrently, enhancing performance and responsiveness in complex applications. Here’s a concise breakdown of its key concepts and utilities.&lt;/p&gt;

&lt;p&gt;Key Features of Multithreading in Java:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creating Threads.&lt;/li&gt;
&lt;li&gt;Thread Management with Executors&lt;/li&gt;
&lt;li&gt;Concurrency Utilities&lt;/li&gt;
&lt;li&gt;Fork/Join Framework&lt;/li&gt;
&lt;li&gt;Asynchronous Programming with Completable Future&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;1️⃣ Creating Threads.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Extending Thread: Create a new thread by overriding the run() method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implementing Runnable: Pass a Runnable instance to a Thread object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implementing Callable: Unlike Runnable, Callable allows threads to return a result and handle checked exceptions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2️⃣ Thread Management with Executors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Java’s Executor Framework (java.util.concurrent.ExecutorService) manages thread pools, allowing efficient handling of tasks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Executors like FixedThreadPool and CachedThreadPool create a pool of reusable threads, managing them efficiently to reduce the overhead of creating new threads.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3️⃣ Concurrency Utilities&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Locks: Advanced locking mechanisms like ReentrantLock provide more flexibility than synchronized methods, allowing timed and interruptible locks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Atomic Variables: The java.util.concurrent.atomic package includes atomic classes (AtomicInteger, AtomicLong) that offer lock-free thread-&lt;br&gt;
safe operations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Synchronizers:include utilities like:&lt;br&gt;
CountDownLatch: Allows a thread to wait until other threads complete &lt;br&gt;
tasks.&lt;br&gt;
CyclicBarrier: Synchronizes a fixed number of threads at a common &lt;br&gt;
barrier point.&lt;br&gt;
Semaphore: Controls access to resources by allowing a specific number &lt;br&gt;
of concurrent threads.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4️⃣ Fork/Join Framework&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. For divide-and-conquer tasks, ForkJoinPool splits a task into smaller subtasks that are processed in parallel, particularly useful in recursive algorithms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5️⃣ Asynchronous Programming with Completable Future&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CompletableFuture enables asynchronous and non-blocking programming, allowing chaining and combining tasks for complex workflows.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Using Thread Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Main class call 2 different thread&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class ThreadConcurrence {
    public static void main(String[] args) {
        // There is 2 type you have to call thread method
                //1- Extend Thread class
                //1- Implements Runnable class
        // why Implement concept is introduce here
                // because in java multiple thread dose not support that's so why implement class will introduce
                // ex- when u extend (inherit) base call, then at that time this call can not extend another Thread class.
        int n = 10;
        for (int i = 0; i &amp;lt; n; i++) {

            // in case of extend(inherit) Thread class
            Thread1 t1 = new Thread1();
            t1.start();

            // in case of implement Runnable class
            Thread t2 =new Thread(new Thread2());
            t2.start();
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thread1--(extends Thread)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Thread1 extends Thread{
    //If you are extend Thread class then you  most be used run()
    // Because when you start a thread then run() automatically call and run
    public void run(){
        try {
            System.out.println("Thread1 is running now....");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thread2--(implements Runnable)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Thread2 implements Runnable {
    //IF you are implement thread Then run() will be executed.
    // Because when you start a thread then run() automatically call and run
    public void run(){
        try {
            System.out.println("Thread2 is running.......");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conclusion :&lt;/p&gt;

&lt;p&gt;By leveraging these tools and frameworks, advanced Java multithreading enables building scalable, high-performance applications that can handle concurrent tasks seamlessly.&lt;/p&gt;

&lt;p&gt;For more insights, feel free to mention your Linkedin and GitHub for in-depth examples and code samples! Let me know if you'd like any specific adjustments.&lt;/p&gt;

&lt;p&gt;Linkedin : &lt;a href="https://www.linkedin.com/in/pravanjan-17p/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/pravanjan-17p/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GitHub : &lt;a href="https://github.com/Prabhanjan-17p" rel="noopener noreferrer"&gt;https://github.com/Prabhanjan-17p&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>coding</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Serialization and Deserialization In Java</title>
      <dc:creator>Pravanjan Amanta</dc:creator>
      <pubDate>Mon, 28 Oct 2024 15:45:21 +0000</pubDate>
      <link>https://dev.to/pravanjan17p/serialization-and-deserialization-in-java-5572</link>
      <guid>https://dev.to/pravanjan17p/serialization-and-deserialization-in-java-5572</guid>
      <description>&lt;p&gt;In advanced Java, serialization and deserialization are processes to save and restore the state of an object, making it possible to store objects in files or databases, or transfer them over a network. Here’s a breakdown of these concepts and their application&lt;/p&gt;

&lt;p&gt;1️⃣ Serialization&lt;/p&gt;

&lt;p&gt;Serialization is the process of converting an object into a stream of bytes. This byte stream can be saved to a file, sent over a network, or stored in a database. In Java, the Serializable interface is used to indicate that a class can be serialized.&lt;/p&gt;

&lt;p&gt;✍ Steps to serialize an object:&lt;/p&gt;

&lt;p&gt;▶️ Implement the Serializable interface.&lt;/p&gt;

&lt;p&gt;▶️ Use ObjectOutputStream and FileOutputStream to write the object to a file or output stream.&lt;/p&gt;

&lt;p&gt;▶️ Call the writeObject() method on ObjectOutputStream.&lt;/p&gt;

&lt;p&gt;👉Code Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Employee implements Serializable {
    private static final long serialVersionUID = 1L;
    String name;
    int id;

    public Employee(String name, int id) {
        this.name = name;
        this.id = id;
    }
}

public class SerializeDemo {
    public static void main(String[] args) {
        Employee emp = new Employee("John Doe", 101);

        try (FileOutputStream fileOut = new FileOutputStream("employee.ser");
             ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
            out.writeObject(emp);
            System.out.println("Serialized data is saved in employee.ser");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, employee.ser is a serialized file that stores the object’s byte stream.&lt;/p&gt;

&lt;p&gt;2️⃣ Deserialization&lt;br&gt;
Deserialization is the reverse process, where the byte stream is converted back into a copy of the original object. This enables you to recreate the object’s state after it’s been stored or transferred.&lt;/p&gt;

&lt;p&gt;✍ Steps to deserialize an object:&lt;/p&gt;

&lt;p&gt;▶️ Use ObjectInputStream and FileInputStream to read the object from the file or input stream.&lt;/p&gt;

&lt;p&gt;▶️ Call the readObject() method on ObjectInputStrea&lt;/p&gt;

&lt;p&gt;👉Code Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class DeserializeDemo {
    public static void main(String[] args) {
        Employee emp = null;

        try (FileInputStream fileIn = new FileInputStream("employee.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn)) {
            emp = (Employee) in.readObject();
            System.out.println("Deserialized Employee...");
            System.out.println("Name: " + emp.name);
            System.out.println("ID: " + emp.id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will retrieve the object’s original state, allowing access to its fields as they were before serialization.&lt;/p&gt;

&lt;p&gt;3️⃣ Advanced Serialization Topics&lt;/p&gt;

&lt;p&gt;▶️ Custom Serialization: Override writeObject() and readObject() for customized serialization.&lt;/p&gt;

&lt;p&gt;▶️ Externalizable Interface: Provides full control over serialization, requiring the implementation of writeExternal() and readExternal() methods.&lt;/p&gt;

&lt;p&gt;▶️ Transient Fields: Use the transient keyword to avoid serializing specific fields (e.g., sensitive data like passwords).&lt;/p&gt;

&lt;p&gt;✍ Advantages of Serialization:&lt;/p&gt;

&lt;p&gt;▶️ Allows saving the state of an object for future use.&lt;/p&gt;

&lt;p&gt;▶️ Facilitates the transfer of complex data objects over networks.&lt;/p&gt;

&lt;p&gt;📝 Briefly Explain Code Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.*;

class Student implements Serializable {
    private  static   final long serialVersionUId = 1l;
    private String name ;
    private int  age;
    private String Address;

    public Student(String name,int age,String Address){
        this.name = name;
        this.age = age;
        this.Address = Address;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public void setAddress(String Address){
        this.Address = Address;
    }

    public String getName(){
        return name;
    }
    public String getAddress(){
        return Address;
    }
    public int getAge(){
        return age;
    }


    public String toString(){
        return ("Student name is "+this.getName()+", age is "+this.getAge()+", and address is "+this.getAddress());
    }
}

public class  JAVA3_Serialization {
    // when you implement Serializable then you must be write a serialVersionUId because when it serialise and deserialize it uniquely identify in the network
    // when u update ur object or anything then you have to update the serialVersionUId increment .
    // private  static   final long serialVersionUId = 1l;

    // transient  int x ;
    // If you do not want a particular value to serialise and Deserialize.
    // the value x, when you don't serialise and Deserialize Then transient you used.

    public static void main(String[] args) {

        Student student = new Student("kanha",21,"angul odisha");

        String filename = "D:\\Advance JAVA\\CLS3 JAVA\\Test.txt"; // store the data in this location
        FileOutputStream fileOut = null; // write file
        ObjectOutputStream objOut = null; // create object
        //Serialization
        try {
            fileOut = new FileOutputStream(filename);
            objOut = new ObjectOutputStream(fileOut);
            objOut.writeObject(student);

            objOut.close();
            fileOut.close();

            System.out.println("Object has been serialise =\n"+student);
        } catch (IOException ex){
            System.out.println("error will occure");
        }

        //Deserialization
        FileInputStream fileIn = null;
        ObjectInputStream objIn = null;
        try {
            fileIn = new FileInputStream(filename);
            objIn = new ObjectInputStream(fileIn);
            Student object =(Student) objIn.readObject();
            System.out.println("object has been Deserialization =\n"+object);

            objIn.close();
            fileIn.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✍ Key Notes:&lt;/p&gt;

&lt;p&gt;▶️ Only non-static and non-transient data members are serialized by default.&lt;/p&gt;

&lt;p&gt;▶️ Serializable objects must ensure compatibility between versions if the class is modified after serialization.&lt;/p&gt;

&lt;p&gt;For more insights, feel free to mention your GitHub for in-depth examples and code samples! Let me know if you'd like any specific adjustments.&lt;/p&gt;

&lt;p&gt;GitHub - &lt;a href="https://github.com/Prabhanjan-17p" rel="noopener noreferrer"&gt;https://github.com/Prabhanjan-17p&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>learning</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
