1

I am learning Java (slowly) from the ground up, but every now and again I peak at some "real" code and I found something that confused me.

The following line and ones like it:

JPanel panel = (JPanel) container.getContentPane();

My question is what is happening between (JPanel) and container.getContentPane()? Its not like they are being multiplied right?

I know this is a basic part of the language and as I continue learning I'll get to this part, but I got really curious and wanted to know what it was right away. I didn't really know what to google to get the answer.

3
  • 2
    I suggest to just work yourself through the entire basic Java tutorial/book. Here's Oracle's own one on the subject you're currently asking about: docs.oracle.com/javase/tutorial/java/IandI/subclasses.html Commented Jan 13, 2012 at 19:56
  • So is what's returned by container.getContentPane() being cast as a JPanel object? Commented Jan 13, 2012 at 19:57
  • Its a way for the programmer to tell Java Compiler to be convinced that whatever 'container.getContentPane()' returns can be safely treated as a JPanel. Without this , by itself Java Compiler thinks otherwise. Commented Jan 13, 2012 at 20:12

4 Answers 4

5

Its not like they are being multiplied right?

No. It means "get this thing and treat it as a JPanel." It's called type casting and that syntax is used in C++, C# and many other languages.

You have to make sure that the way that the classes are related to each other allows for casting. For example, this wouldn't work:

JPanel p = new JPanel();
JComponent c = (JComponent)p;
JButton b = (JButton)c;

JPanel is a JComponent and so is JButton, but JButton does not descend from JPanel thus you cannot cast between these objects. You can also cast from a child back to a parent, such as from JSpinner.DefaultEditor back to JPanel, but not from JPanel to JSpinner.DefaultEditor.

Sign up to request clarification or add additional context in comments.

2 Comments

Neato, that makes perfect sense to me now. I didn't get to casting yet in my java book, but I remember it in some other langauages I've played with. I guess the syntax should have be more familiar to me.
Note that a check is done at runtime to check if the cast is valid (i.e. if the thing that's being cast really can be treated as the type you're casting it to). If it is not, you will get a ClassCastException on the line where you do the cast. Also, casting should be avoided as much as possible, because it takes away type safety.
1

This is called a type cast. The method getContentPane() normally return a Container, but we want to get it in a JPanel so we'll be able to use JPanel methods. Of course the two types have to be compatible (JPanel is a specific implementation of Container (through JComponent) for example)

Comments

1

It's a cast expression (see JLS 15.16, Cast Expressions).

It means "treat the results of the getContentPane() call as a JPanel".

Casts can fail, causing a ClassCastException (see JLS 5.5, Casting Conversion).

Comments

0

Yes this is how type casting is made in java. As you know java works with references. In your case you have a JPanel type reference and container.getContentPane() returns (correct me if I am wrong my Swing is a bit rusty) container, so since those two are incompatible you need to type cast to make them compatible. Hope that helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.