Java

1) Class Declaration: The code starts with the declaration of a Java class named PrimitiveAndWrapper.
2) Main Method: Inside the class, there's a main method which serves as the entry point for the Java application.
3) String s = "25";: This line initializes a String variable s with the value "25".
4) Integer i = new Integer(s);: Here, the String "25" is converted into an Integer object using the constructor of the Integer class. This process is known as wrapping, where a primitive data type (int in this case) is converted into its corresponding wrapper class (Integer).
5) System.out.println("String :" + s);: This line prints the original String value stored in s.
6) System.out.println("Wrapper :" + i);: It prints the Integer value after wrapping s. Since i is an Integer object, it automatically invokes the toString() method to convert itself into a String representation for printing.
7) int ip = Integer.parseInt(s);: This line converts the String "25" into an integer primitive using the parseInt() method of the Integer class. This process is known as parsing.
8) System.out.println("Primitive :" + ip);: Finally, this line prints the value of the integer primitive ip.

public class PrimitiveAndWrapper {
    public static void main(String[] args) {
       
        String s="25";

        // Wrapper
        Integer i= new Integer(s);

        System.out.println("String :"+s);
        System.out.println("Wrapper :"+i);

        int ip=Integer.parseInt(s);

        System.out.println("Primitive :"+ip);


    }
}

Conclusion:
The code illustrates the conversion between String, primitive data types, and their corresponding wrapper classes in Java. It demonstrates how to wrap a String into an Integer object and parse a String into an integer primitive. Understanding these conversions is crucial for handling data interchangeably between different data types in Java programming.

Related Posts

Table Of Contents

;