A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/new-operator-java/ below:

new operator in Java - GeeksforGeeks

new operator in Java

Last Updated : 30 May, 2018

When you are declaring a class in java, you are just creating a new data type. A class provides the blueprint for objects. You can create an object from a class. However obtaining objects of a class is a two-step process :

Hence declaration of a class variable, instantiation of a class and initialization of an object of class can be together illustrated as follows :

Important points :
  1. The above two statements can be rewritten as one statement.
    Box mybox = new Box();
    
  2. The reference returned by the new operator does not have to be assigned to a class variable. It can also be used directly in an expression. For example:
    double height = new Box().height;
    
  3. Since arrays are object in java, hence while instantiating arrays, we use new operator. For example:
    int arr[] = new int[5];
    
  4. At this point, you might be wondering why you do not need to use new operator for primitives data types. The answer is that Java’s primitive types are not implemented as objects. Rather, they are implemented as "normal" variables. This is done in the interest of efficiency. For object versions of the primitive data types, refer Wrapper Classes.
  5. The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.

Assigning object reference variables

When you assign one object reference variable to another object reference variable, you are not creating a copy of the object, you are only making a copy of the reference. Let us understand this with an example.

Java
// Java program to demonstrate assigning 
// of object reference variables

// Box class
class Box
{
    double width;
    double height;
    double depth;
}

// Driver class
public class Test
{
    // Driver method
    public static void main(String[] args) 
    {
        // creating box object
        Box b1 = new Box();
        
        // assigning b2 to b1
        Box b2 = b1;
        
        // height via b1 and b2
        System.out.println(b1.height);
        System.out.println(b2.height);
        
        // changing height via b2
        b2.height = 20;
        
        // height via b1 and b2
        // after modification through b2
        System.out.println(b1.height);
        System.out.println(b2.height);
    }
    
}

Output :

0.0
0.0
20.0
20.0
Explanation :

First let us understand what the following fragment does in above program.

Box b1 = new Box();
Box b2 = b1;

You might think that

b2

is being assigned a reference to a copy of the object referred to by

b1

. That is, you might think that

b1

and

b2

refer to separate and distinct objects. However, this would be wrong. Instead, after this fragment executes,

b1

and

b2

will both refer to the same object. The assignment of

b1

to

b2

did not allocate any memory or copy any part of the original object. It simply makes

b2

refer to the same object as does

b1

. Thus, any changes made to the object through

b2

will affect the object to which

b1

is referring, since they are the same object. Same can be verified by output when we change

height

of box via

b2

. This situation can be illustrated as follows :

Note :

Although

b1

and

b2

both refer to the same object, they are not linked in any other way. For example, a subsequent assignment to

b1

will simply

unhook b1

from the original object without affecting the object or affecting

b2

.For example :

Box b1 = new Box();
Box b2 = b1;
// ...
b1 = null;

Here,

b1

has been set to

null

, but

b2

still points to the original object.

Passing object references variables to methods

When we pass object reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument. To know more with examples, refer

Passing and Returning Objects in Java.

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4