I have been taught to always use getters and setters. However, I don't know the pros and cons of these methods, as by implementing them we are exposing the data and also hiding it.
I am a little confused about this. Can anybody give some proper advice on why we use a getter/setter and what the advantages are?
The basic "private field with public getter and setter that do nothing but return or set the field" pattern is indeed completely pointless when it comes to encapsulation, except that it gives you a chance to change it later without changing the API.
So don't use that pattern unthinkingly. Carefully consider what operations you actually need.
The real point of getters and setters is that you should only use them where they are appropriate, and that they can do more than just get and set fields.
IllegalArgumentException
All of these things are implementation details that are hidden behind the simple "getter and setter" interface. That's what encapsulation is about.