Wrapper Class in Java and Method and Method Overloading

5/5 - (1 vote)

Hello everyone, in the previous topic we were talking about What is Utility, the Indifference Curve, and the Elasticity of Demand. Today we are going to talk about Wrapper Class in Java and Method and Method overloading.

Wrapper Class in Java

Wrapper Class in Java
Wrapper Class in Java
  • The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.
  • Since J2SE 5.0, autoboxing and unboxing features convert primitives into objects and objects into primitives automatically. The automatic conversion of a primitive into an object is known as autoboxing and vice-versa unboxing.

Use of Wrapper classes in Java

Uses of wrapper classes in Java are as follow-

  1. They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).
  2. The classes in java util package handle only objects and hence wrapper classes help in this case also.
  3. Data structures in the Collection framework. such as ArrayList and Vector, store only objects (reference types) and not primitive types.
  4. An object is needed to support synchronization in multithreading.
  5. These are all defined in the java language package hence we don’t need to import them manually.
  6. Wrapper class objects allow null values while primitive data type doesn’t allow them.
Primitive Data Type Wrapper Class
byte   Byte
shortShort
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

Autoboxing

The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float, boolean to Boolean, double to Double, and short to Short

Example of Autoboxing
// Autoboxing example of int to Integer
public class test {
public static void main(String args[]) {
//Converting int into Integer
int a= 10;
Integer i =IntegervalueOf(a)//converting int into Integer explicitly Integer j-a//autoboxing now compiler will write IntegervalueOf(a) internally
System.out println(a+””+(+””+j);
}
}
 Output- 10 10 10

Unboxing

The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing. It is the reverse process of autoboxing. Since Java 5, we do not need to use the intValue() method of wrapper classes to convert the wrapper type into primitives.

Wrapper to Primitive
//Unboxing example of Integer to int
public class test {
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(10);
int i= a:intValue();//converting Integer to int explicitly
int j=a://unboxing, now compiler will write a.intValue() internally System.out println (a+””+i+””j};
}
}
Output- 10 10 10

Method

A method is a collection of statements that are grouped together to perform an operation. It takes some parameters, performs some computations, and then optionally returns a value (or object).

There are five components in a method-

  • Modifiers- There can be four types of modifiers – Public, Private, Protected, and Default (uncategorized) modifiers.
  • The return type- A method can return a value, which could be any one of integer, string, character, float, or nothing.
  • The method name- It’s the identifier of a method.
  • The parameter list in parenthesis-It is the list of arguments.
  • The method body is enclosed with braces.
Syntax-
return-type methodName(parameter-list)
{
//body of the method
}
Example
public String getName(String st)
{
String name=”Hello JAVA“;
name= name+st;
return name;
}

Parameter Vs. The Argument in a Method

The parameter is variably defined by a method that receives value when the method is called. Parameters are always local to the method they don’t have scope outside the method while the argument is a value that is passed to a method when it is called.

Call-by-value and Call-by-reference

There are two ways to pass an argument to a method.

  • call-by-value – In this approach copy of an argument value is passed to a method. Changes made to the argument value inside the method will have no effect on the arguments.
  • call-by-reference – In this reference of an argument is passed to a method. Any changes made inside the method will affect the argument value.

NOTE -There is only call by value in java, not call by reference.

Example of call-by-value
class test
{
 public int callByValue(int x)

{
 x=100;
 return x;
}
public static void main(String[] args)
{
int x=50;
test t = new test();
System.out.println(t.callByValue(x));
}
}

Method Overloading in Java

If two or more method in a class has the same name but different parameters, it is known as method overloading. Overloading always occurs in the same class (unlike method overriding).

Method overloading is one of the ways through which java supports polymorphism. Method overloading is an example of Static Polymorphism.

Points to Note-

  • Static Polymorphism is also known as compile-time binding or early binding
  • Static binding happens at compile time. Method overloading is an example of static binding where binding of the method call to its definition happens at Compile time.

Different ways of Method overloading

There are two ways to overload the method in java

  • By changing the number of arguments
  • By changing the data type
Changing no. of arguments
class Area
{
 void find(int I, int b)
{
System.out.println(“Area is”+(l*b));
}
 void find(int l, int b, int h)
{
System.out printin(“Area is”+(l*b*h);
}
public static void main (String[]) args)
{
Area ar = new Area();
ar find(8,5); //find(int l, int b) is method is called
ar find(4,6,2); /find(int I, int b,int h) is called
}
}
Output
Area is 40
Area is 48
The sequence of the data type of arguments
class test
{
public void disp(char c, int num) {
(System.out.printin(“I’m the first definition of method disp”); }
public void disp(int num, char c)
{
System.out.printin(“l’m the second definition of method disp” ); }
}
class demo
{
public static void main(String args[])
{
test obj = new test();
obj.disp(‘x’ 51);
obj.disp(52 ‘y’);
}
Output-
I’m the first definition of method disp
I’m the second definition of method disp

Invalid case of method overloading

In java, method overloading is not possible by changing the return type of the method only because of ambiguity.

If two methods have the same name, same parameters, and have a different return type, then this is not a valid method overloading example. This will throw a compilation error.

int add(int, int)
float add(int, int)
Example
 class demo {
 static int add(int a,int b)(return a+b;}
static double add(int a ,int b){return a+b;}
}
 class test{
 public static void main(String[]) args)
{
System.out.printin(demo.add(11,11)); //ambiguity
}
}
\\Output- Compile Time Error

Method Overloading and Type Promotion One type is promoted to another implicitly if no matching datatype is found.

Example of Method Overloading with Type Promotion
class test{
void sum(int a,long b){System.out.println(a+b);}
void sum(int a,int b,int c) {System.out.println(a+b+c);}
public static void main(String args[]){
test obj=new test();
obj sum(20,20);//now second int literal will be promoted to long obj.sum(20,20,20);
}
}
Output- 40
Output- 60

Example of Method Overloading with Type Promotion of matching found

If there are matching type arguments in the method, type promotion is not performed.

class test{
void sum(int a, int b){System.out.println(“int arg method invoked”);} void sum(long a, long b){System.out.println(“long arg method invoked”);}
public static void main(String args[]){
test obj=new test();
obj.sum(20,20);//now int arg sum() method gets invoked
}
}
Output- int arg method invoked

Example of Method Overloading with Type Promotion in case of ambiguity

If there are no matching type arguments in the method, and each method promotes similar number of arguments, there will be ambiguity.

class test{
 void sum(int a, long b){System.out println(“a method invoked”)}
 void sum(long a, int b){System.out.printin(“b method invoked”)}
 public static void main(String args[]) {
 test obj=new test[];
 obj sum(20,20)//now ambiguity
}
}
Output- Compile Time Error

So, that is all for today guys see you in our next blog. If you like our article please don’t forget to share with others & follow our Instagram page for your daily dose of Motivation.

Thank You,

Regards

Grooming Urban

General FAQ

What is Wrapper Class in Java?

The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.

What is Autoboxing?

The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing.

What is Unboxing?

The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing.

What is Method?

A method is a collection of statements that are grouped together to perform an operation. It takes some parameters, performs some computations, and then optionally returns a value (or object).

What are the 5 components in a method?

There are five components in a method-
1. Modifiers
2. The return type
3. The method name
4. The parameter list in parenthesis
5. The method body is enclosed with braces.

What is Method Overloading in Java?

If two or more method in a class has the same name but different parameters, it is known as method overloading.

Sharing Is Caring:
Kumar Shanu Sinha

An aspiring MBA student formed an obsession with Management Related Concept, Digital Marketing, Leadership, and Personality Development now helping others to improve in their studies and personality as well.

1 thought on “Wrapper Class in Java and Method and Method Overloading”

Leave a Comment