Skip to main content

Autoboxing / Unboxing




What it is?
Even though the name includes boxing, this is all about wrapping and unwrapping primitive types with its corresponding wrapper class.
No Idea…
Ok, Following example will explain all.
// Java program to illustrate the concept 
// of Autoboxing and Unboxing 
import java.io.*; 
  
class Independor 
{ 
    public static void main (String[] args) 
    { 
        // creating an Integer Object (wrapper class of int)
        // with value 10. 
        Integer i = new Integer(10); 
  
        // unboxing the Object 
        int i1 = i; 
  
        System.out.println("Value of i: " + i); 
        System.out.println("Value of i1: " + i1); 
  
        //Autoboxing of char 
        Character gfg = 'a'; 
  
        // Auto-unboxing of Character (wrapper class of char)
        char ch = gfg; 
        System.out.println("Value of ch: " + ch); 
        System.out.println("Value of gfg: " + gfg); 
  
    } 
}
Following are the primitive types and its corresponding wrapper classes.

Why Autoboxing/Unboxing?
  • Autoboxing and unboxing lets developers write cleaner code, making it easier to read.
  • The technique let us use primitive types and Wrapper class objects interchangeably and we do not need to perform any typecasting explicitly.
That’s it… You got the idea right? It’s not a complicate subject to understand, but if you have any questions let me know below. Cheers!

Popular posts from this blog

Autofill Dynamic Dropdowns Using Spring Jquery Ajax

This explains on how to populate values in a drop down based on a value selected on another drop down. But you can configure it load values dynamically when click on link or any other action performed (based on your requirment) Include JSTL tag library in your JSP Page 1 <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> Use JSTL to call the required URL path through Spring bindings 1 < c : url var = "findProductForCat" value = "/products_ajax.do" /> HTML and EL code to enter in JSP page. 1 2 3 4 5 6 7 8 9 10 <select class= "select-width" id= "category_list_d" > <option value= "" > Select Category </option> <c:forEach var= "catList" items= "${category_lst}" > <option value= "${catList.categoryId}" > ${catList.categoryName } </option> </c:forEach> ...

Java Version and New Features 8 - 13

Introduction Introduction Java is widely used staticly typed programming language which is founded over 25 years back. It's almost everywhere from home appliances, mobile devices to industry scale systems. Java is evolving over the time and from Java 8 onwards there were major changes introduced to the language. Below are the list of all the changes on each version. Imperative vs Declarative Programming Imperative approach is defining step by step on HOW we are going to achieve the end result. Declarative approach is describing WHAT we want as the output. Simple example would be a classical for loop in Java which we can call as imperative programming and a select query in sql as declarative programming. ...

Annotations

What are annotations? Annotations are meta data in Java. What is meta data? A metadata is data about data. Metadata adds some additional flags on your actual data, and in runtime either you or JVM who understand these flags, can utilize this metadata information to make appropriate decisions based on context. How it looks like? Basic annotation is look like following. @ MyAnnotation It starts with @ sign followed by the Annotation name. Annotations are defined as following. @interface MyAnnotation { } I didn’t get it… Ok, Annotations do basically three things in Java. It provides…. Embed instructions to Java Compiler Embed instructions to Source Code Processing Tools such as IDEs Embed metadata which can be read at runtime by your Java Application or Third Party Tools Lets look at some simple example Below example shows how @Override annotation checks that the annotated method is overridden method. public class DemoClass { //some code @Override public Str...