1

I have very basic java question but tutorials are not focusing on this aspect. If anyone can describe for what we use this kind of brackets.

First example Employee in brackets:

Employee e = null;
  try
  {
     FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
     ObjectInputStream in = new ObjectInputStream(fileIn);
     e = (Employee) in.readObject();  <=========== (Employee)
     in.close();
     fileIn.close();
  }

Second example:

InputStream fileIs = null;
    ObjectInputStream objIs = null;
    try {
        fileIs = new FileInputStream("MyEmpFile.txt");
        objIs = new ObjectInputStream(fileIs);
        Employee emp = (Employee) objIs.readObject(); <========== (Employee)
        System.out.println(emp);
    }

I understand what:

Employee emp = new Employee();

does but with this brackets (Employee) inside I don't get it. What is this?

I asked this question here becouse google searches with combined queries like "java brackets before constructor", "java brackets creating new objects" etc didn't find results from which I could gather information about my questions. I've read java tutorials too, all were without pointing this example.

(For example www.tutorialspoint.com/java/index.htm )

Thank you for your time! I hope I will be able to help other members in the future.

Wont Provide
  • 47
  • 1
  • 3
  • 8

2 Answers2

0

That is called casting. Basically it is "transforming" an object of one type into another. If you don't find the "possible duplicate" link on the comments helpful (which you should upvote), check these

JonyD
  • 1,237
  • 3
  • 21
  • 34
0

When you see the signature of objIs.readObject(); it return the object of Object class, so we need to type cast to employee.

ankush yadav
  • 422
  • 3
  • 13