Java Tutorial
IntroductionEnvironment SetupIDEBuild ManagementLanguage SpecificationBasic ProgramVariablesData TypesPackagesModifiersConditionalsLoopsObject OrientedClassesSuperInterfacesEnumStatic importInheritanceAbstractionEncapsulationPolymorphismBoxing & UnboxingConversion Formatting numbers Arrays Command line arguments in java Variable Number of arguments in Java Exception handling in Java String handling in Java StringBuffer and StringBuilder in Java Mathematical Operations in Java Date and Time in Java Regular expressions in Java Input output programming in Java File Handling Nested Classes Collections Generics Serialization Socket programming Multi-Threading Annotations Lambda Expressions Reflections in Java Singleton class in Java Runtime Class in JavaHow to load resource in JavaHow to load properties file in JavaAdvanced
Log4j – Logging framework in JavaInterview Questions in JavaCollections in Java does not support primitives. We can not store primitives in collection objects. Only objects can be stored in collections. Below statement is invalid.
//ArrayList<int> list = new ArrayList<>();
But still we can store integers in ArrayList using a concept called as Boxing. Process by which primitives like int are converted into objects of Wrapper classes like Integer, Double etc is called as Boxing. The reverse process is called as Unboxing. Below example illustrates Boxing and Unboxing in Java.
package boxing;
import java.util.ArrayList;
/**
* Created by ssalunke on 19/04/2016.
*/
public class Boxing {
public static void main(String args []){
//Below statement is invalid
//ArrayList<int> list = new ArrayList<>();
int c = 3;
ArrayList<Integer> list = new ArrayList<>();
list.add(2); //boxing to Integer wrapper
list.add(c); //boxing to Integer wrapper
//Unboxing occurs when storing integer object to primitive type
int a = list.get(0);
System.out.println("First value in list is " + a);
}
}
Here is the output of above example.
First value in list is 2
Web development and Automation testing
solutions delivered!!