-2

I'm new to learn java. I see this syntax in a Java class called 'Point', and I'm confused what it means, and what it does:

Point otherPoint = (Point) otherObject;

Thx !

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
ajavabean
  • 1
  • 1

1 Answers1

0

I see this syntax in a Java class called 'Point', and I'm confused what it means, and what it does:

Alright, let's break it down step by step:

this is the type of the object to reference:

Point 

this is the identifier which refers to the object on the right side of the assignment operator:

otherPoint  

this is known as casting:

(Point) otherObject; 

Casting is the process of taking an Object of one particular type and “turning it into” another Object type.

if the cast succeeds the identifier otherPoint will refer to the object if not a ClassCastException exception will be thrown.

Point otherPoint = (Point) otherObject;

further reading:

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126