Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this code casting a different object?  RSS feed

 
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have this code and I was wondering if the statement: Graphics2D g2 = (Graphics2D) g; is casting a different object it just looks like when you cast an int to a double. Here is my code:




Thanks!

Bill
 
Author and ninkuma
Marshal
Posts: 66541
160
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, no new object is created. It just says to treat the existing object as the subclass rather than the superclass.

Casting objects is not the same as casting primitives.
 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

In the book I'm working on it says that since we want to use the methods of the Graphics2D methods and to accomplish this to cast.
The book I'm working on is pretty old copyright 2005 Java Concepts by Cay Horstmann. So this is not a cast.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66541
160
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bill foster wrote:The book I'm working on is pretty old copyright 2005 Java Concepts by Cay Horstmann. So this is not a cast.


Huh? Of course it's a cast.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!