0

I am new to Java and I have a rather simple question, because I can't understand what it is doing and in Scala, the programming language I programmed with before, doesn't "do" or "have" such a thing.

So lets assume we have a class named "Pet" and my code snippet is:

Pet myPet = (Pet) myPet.getPetName();

What exactly is the

(Pet)

doing here in front of the "myPet.getPetName?

Please delete my question or mark it as duplicate if there exists one but I couldn't find any solution because I dont know how this is called?

bajro
  • 1,199
  • 3
  • 20
  • 33
  • 1
    This is known as "casting" – tddmonkey Apr 08 '16 at 14:01
  • Check out "Casting Objects" section [here](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) – Avantol13 Apr 08 '16 at 14:02
  • Wouldn't you use `((Pet) myPet).getPetName();` ? Because, I assume `getPetName()` would return a string. This looks, to me, like a compile-time error. – Mr. Polywhirl Apr 08 '16 at 14:07
  • `Pet myPet = (Pet) myPet.getPetName();` would probably not compile or run for several reasons: 1) `myPet` would have to be defined already thus this line might be a redefinition - it wouldn't compile if the original `myPet` was in the same scope 2) `getPetName()` sounds like it's returning a `String` which can't be cast to `Pet`. – Thomas Apr 08 '16 at 14:07
  • @Thomas Hahah, something, something... "great minds..." – Mr. Polywhirl Apr 08 '16 at 14:08

3 Answers3

4

This is called a cast (and also, that's probably not what you want to do there since I'm guessing a name is a String and not a Pet, but I may be wrong).

In Java, casting is how you tell the compiler to assume an object has a different type than declared:

public class Animal {}
public class Dog extends Animal {}

Animal pet = new Dog(); // pet is actually a Dog, but the compiler only knows that it is an Animal

Dog myBestFriend = (Dog) pet; // tell the compiler to assume that pet is really a Dog, allowing us to assign it to a Dog-typed variable.

This doesn't actually change the type of the object-- if you try to cast a variable to a type other than the contents actually are, you'll get a ClassCastException:

public class Cat extends Animal {}

Animal pet = new Cat(); // pet is a Cat, but the compiler only knows it as Animal

Dog myBestFriend = (Dog) pet; // this will throw ClassCastException, because pet is a Cat and Cat is not a subtype of Dog
Darth Android
  • 3,437
  • 18
  • 19
  • Thank you for your answer! Now my question is if this is a must or is this just optional since my Variable "myPet" is of type Pet so why do I have to cast it anyways? – bajro Apr 08 '16 at 14:15
1

It's casting. It's telling the compiler that your result should be treated as type Pet.

baarkerlounger
  • 1,217
  • 8
  • 40
  • 57
1

This code doesn't really make sense, because obviously myPet is null. And I don't see why the name of a pet should be a pet itself again. Generally something like (SomeClass) object is a cast.

For example take two classes Animal and Pet extends Animal Since every Pet is also an Animal, you might have an object like this:

Animal cat = new Pet();

If you need to call a method on cat that is in Pet but not in Animal you would cast it:

((Pet) cat).doSomething();

If you try to cast an object to a wrong type, you will get a ClassCastException, so you might want to check before you cast:

if(cat instanceof Pet)...
Bene
  • 724
  • 8
  • 20
  • I am sorry for my bad example but it was just there to clarify what the part in the parentheses does – bajro Apr 08 '16 at 14:20