0% found this document useful (0 votes)
131 views

Sy Java

The document discusses the difference between an object and a reference in Java. An object is an instance of a class that contains methods and variables defined by the class blueprint. An object is mutable and has a real world meaning. A reference is a memory location that points to the object and contains the object's address. It is immutable and has no real meaning on its own.

Uploaded by

Varad Vinherkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Sy Java

The document discusses the difference between an object and a reference in Java. An object is an instance of a class that contains methods and variables defined by the class blueprint. An object is mutable and has a real world meaning. A reference is a memory location that points to the object and contains the object's address. It is immutable and has no real meaning on its own.

Uploaded by

Varad Vinherkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

11.What is difference between object and reference of a class?

Parameter of
Object Reference
Comparison

Basic definition It is the instance of a class and all the elements it A simply memory reference that points to where the
contains are based on the blueprint of the class. object is stored in memory slot.

Format for There is a simple format for creation of an object: The memory reference is created alongside object. It is
creation ClassName reference_variable = new used in the format for object creation and is given a
ClassName(with parameter); variable name

Elements It contains methods and variables based on the It contains a sequence of bits that store the address of
class. the object.

Mutable Objects have states and behaviors that can be The reference variable value cannot be changed. It can
changed, i.e, the state of the object can be only remain as the data type that it was declared as.
changed.

Virtual meaning It is a real world entity that holds some form of It is nothing but a variable name, which has no real
memory or data. meaning. It is like the name of a person, that references
that person.

12. Explain with help of examples concept of autoboxing and unboxing.

Autoboxing: Automatic conversion of primitive types to the object of their corresponding


wrapper classes is known as autoboxing. For example – conversion of int to Integer, long to
Long, double to Double etc.

Unboxing: It is just the reverse process of autoboxing. Automatically converting an object of


a wrapper class to its corresponding primitive type is known as unboxing. For example –
conversion of Integer to int, Long to long, Double to double etc.

Primitive type Wrapper class


boolean Boolean
byte Byte
char Character
float Float
int Integer
long Long
short Short
double Double

When does the autoboxing and unboxing happens in Java


Autoboxing
: Lets see few cases with examples, where autoboxing happens.
Case 1: When a method is expecting a wrapper class object but the value that is passed as
parameter is a primitive type. For example in the below code, the method myMethod() is
expecting an object of Integer wrapper class, however we passed a primitive int type. The
program ran fine as compiler does the autoboxing (conversion of int to Integer)

class AutoboxingExample1
{
public static void myMethod(Integer num){
System.out.println(num);
}
public static void main(String[] args) {
/* passed int (primitive type), it would be
* converted to Integer object at Runtime
*/
myMethod(2);
}
}
Output:

2
Case 2: When at some point of time, you are assigning a primitive type value to an object of
its wrapper class. For example: The below statements are valid because compiler does the
autoboxing at runtime.

Integer inum = 3; //Assigning int to Integer: Autoboxing


Long lnum = 32L; //Assigning long to Long: Autoboxing
Case 3: When dealing with collection framework classes:

ArrayList<Integer> arrayList = new ArrayList<Integer>();


arrayList.add(11); //Autoboxing - int primitive to Integer
arrayList.add(22); //Autoboxing
Here ArrayList class is expecting an Integer wrapper class object but we are providing int
primitive.
Unboxing
Case 1: Method is expecting Integer object (parameter) but we have supplied int. Auotmatic
conversion(unboxing) happened that converted Integer to int.

class UnboxingExample1
{
public static void myMethod(int num){
System.out.println(num);
}
public static void main(String[] args) {

Integer inum = new Integer(100);

/* passed Integer wrapper class object, it


* would be converted to int primitive type
* at Runtime
*/
myMethod(inum);
}
}
Output:

100
Case 2: Assignments

Integer inum = new Integer(5);


int num = inum; //unboxing object to primitive conversion
Case 3: While dealing with collection classes:

ArrayList arrayList = new ArrayList()


int num = arrayList.get(0); // unboxing because get method returns an Integer object

13.
List and explain overloaded constructors of String class.

14.

You might also like