Core java - Advance Topics
  • Welcome
  • Schedule
  • 1) Exception Handling
    • 1) Introduction to Exception Handling
    • 2) Categories of Exceptions
    • 3) Creating a method that throws an exception
    • 4) Creating Custom Exception Classes
    • 5)What happens when an exception is thrown?
      • 5.1) Creating try-catch-finally blocks
      • 5.2) Using a method that throws a checked exception
      • 5.3) Using a method that throws a runtime exception
      • 5.4) Using a method that throws an error
      • 5.5) Will a finally block execute even if the catch block defines a return statement?
      • 5.6) What happens if both a catch and a finally block define return statement?
      • 5.7) What happens if a finally block modifies the value returned from a catch block?
      • 5.8) Can a try block be followed only by a finally block?
      • 5.9) Does the order of the exceptions caught in the catch blocks matter?
      • 5.10) Can I rethrow an exception or the error I catch?
      • 5.11) Can I declare my methods to throw a checked exception instead of handling it?
      • 5.12) I can create nested loops, so can I create nested try-catch blocks too?
      • 5.13) Should I handle errors?
    • 6) Best Practices
    • 7) Cheat Sheet
    • 8) Problems
  • 2) Wrapper Classes and Enums
    • 2.1) Creating objects of the wrapper classes
    • Enums
  • 3) Inner Classes
    • 3.1) Static nested class (also called static inner class)
    • 3.2) Inner class (also called member class)
    • 3.3) Anonymous inner class
    • 3.4) Method local inner classes
    • CheatSheet
  • 4) Generics
    • Multiple Type parameters in Generic classes
    • Inheritance using Generics
    • Generic interfaces
    • Generic Methods
    • Bounded type parameters
    • Applications
  • 5) Equals and Hashcode
    • Problems
  • CompareTo method overview
  • Basic DS
    • 1) Simple Array List
    • 2) Simple HashMap
  • 5) Collections Framework - Part 1
    • Introducing the collections framework
    • Working with the Collection interface
      • The core Collection interface
      • Methods of the Collection interface
    • Creating and using List, Set, and Deque implementations
      • List interface and its implementations
      • Iterators
      • Sorting List using custom sorting technique
      • Comparable Interface
      • Custom Sorting using comparator
      • ArrayList - Examples and practice problems
    • Stack
    • Linked List
    • LinkedList Operations
  • 6) Collections Framework - Part 2
    • Sets
      • Set Types
      • Array to Set (vice versa)
    • Maps
    • TreeMap
    • Autoboxing And Unboxing
  • Collections Framework - Part 3
    • Basics : DS , Number System
    • Internal Working
      • HashMap
      • HashSet
  • 7) Reflection API
  • 8) Annotations
  • 9) Reading Input From Various Sources
    • File Handling
    • Reading From Xml
    • Reading From JSON
  • 10) Multi-threading (Concurrency)
    • Protect shared data
    • Thread-safe access to shared data
  • 11) Design Patterns
    • Singleton
    • DI
  • 12) Internal Working of JVM
  • 13) Garbage Collection
  • 14) More on Strings (Buffer and Builder)
  • 15) Cloning and Immutable Class
    • 16) Serialization And Deserialization
    • Untitled
  • JAVA 8
    • Interface Changes
    • Lambda
    • Method Ref
    • Optional
    • Streams
    • Predicates
  • Practice Tests
    • Test - Collections
    • OOPS
    • S-OOPS
Powered by GitBook
On this page
  • 1)Class hierarchy of wrapper classes:
  • 2) Creating objects of the wrapper classes:
  • 3) Retrieving primitive values from the wrapper classes:
  • 4) Parsing a string value to a primitive type:
  • 5) Difference between using method valueOf() and constructors of wrapper classes

Was this helpful?

  1. 2) Wrapper Classes and Enums

2.1) Creating objects of the wrapper classes

Previous2) Wrapper Classes and EnumsNextEnums

Last updated 6 years ago

Was this helpful?

1)Class hierarchy of wrapper classes:

All the wrapper classes implement the interfaces java.io.Serializable and java.lang.Comparable. All these classes can be serialized to a stream, and their objects define a natural sort order.

2) Creating objects of the wrapper classes:

You can create objects of all the wrapper classes in multiple ways:

  • Assignment—By assigning a primitive to a wrapper class variable

  • Constructor—By using wrapper class constructors

  • Static methods—By calling the static method of wrapper classes, like valueOf()

You can create objects of the rest of the wrapper classes (Short, Integer, Long, and Float) in a similar manner. All the wrapper classes define constructors to create an object using a corresponding primitive value or as a String.

Another interesting point to note is that neither of these classes defines a default no-argument constructor. Because wrapper classes are immutable, it doesn’t make sense to initialize the wrapper objects with the default primitive values if they can’t be modified later.

All wrapper classes (except Character) define a constructor that accepts a String argument representing the primitive value that needs to be wrapped.

You can assign a primitive value directly to a reference variable of its wrapper class type—thanks to autoboxing. The reverse is unboxing, when an object of a primitive wrapper class is converted to its corresponding primitive value. I’ll discuss autoboxing and autounboxing in detail in the next section.

3) Retrieving primitive values from the wrapper classes:

All wrapper classes define methods of the format primitiveValue(), where primitive refers to the exact primitive data type name. Table shows a list of the classes and their methods to retrieve corresponding primitive values.

Example :

package com.gs.ilp.corejava.wrapperclasses;

public class WrapperExample1 {
	public static void main(String[] args) {

		//-----creation----
		
		//1) using primitives
		Boolean bool1 = true;
		Character char1 = 'a';
		Byte byte1 = 10;
		Double double1 = 10.98;

		//2) using constructor accepting literal
		Boolean bool2 = new Boolean(true);
		Character char2 = new Character('a');
		Byte byte2 = new Byte((byte) 10);
		Double double2 = new Double(10.98);

		//3) using constructor accepting String literal
		// Character char3 = new Character("a");
		Boolean bool3 = new Boolean("true");
		Byte byte3 = new Byte("10");
		Double double3 = new Double("10.98");

		//4) using static methods
		Boolean bool4 = Boolean.valueOf(true);
		Boolean bool5 = Boolean.valueOf(true);
		Boolean bool6 = Boolean.valueOf("TrUE");
		Double double4 = Double.valueOf(10);
		
		//--- getting primitives from wrapper
		
		boolean b1 = bool4.booleanValue();
		char c2 = char2.charValue();
		double d3= double4.doubleValue();
		byte b4 =  byte3.byteValue();
	}
}

4) Parsing a string value to a primitive type:

To get a primitive data type value corresponding to a string value, you can use the static utility method parseDataType(), where DataType refers to the type of the return value. Each wrapper class (except Character) defines a method, to parse a String to the corresponding primitive value, listed as follows:

package com.gs.ilp.corejava.wrapperclasses;

public class WrapperExample2 {
	public static void main(String[] args) {
		Long.parseLong("12.34");
		Byte.parseByte("1234");
		Boolean.parseBoolean("true");
		Boolean.parseBoolean("TrUe");
	}
}

Example 1:

package com.gs.ilp.corejava.wrapperclasses;

public class WrapperExample2 {
	public static void main(String[] args) {
		Long.parseLong(null);
		Byte.parseByte("1234");
		Boolean.parseBoolean("true");
		Boolean.parseBoolean("TrUe");
	}
}

CTE

5) Difference between using method valueOf() and constructors of wrapper classes