The above answer by rienzi is impressive, but it does not explain how Hamlet could have accumulated so much learning by the age of only sixteen or eighteen. Take languages. Hamlet knows Danish, of course, and probably has a speaking knowledge of Norwegian and Swedish. He must know English very well if he is being sent to England as an ambassador. Furthermore, he is speaking pretty good English in the play (although we are supposed to assume everybody is speaking Danish). In order to go to Wittenberg he would have to know German because it is a German university. To get into any good university he would be expected to know the Latin language and its extensive literature thoroughly, and he might even be expected to know ancient Greek. He would know French, of course, because that was the language of culture. And Shakespeare takes pains to show that the scholarly Prince has a thorough knowledge of Italian when Hamlet explains, evidently to Ophelia in Act 3, Scene 2, "The story is extant, and written in very choice Italian," demonstrating that he is not merely acquainted with the language but is a connoisseur of the language and the literature. Then, of course, Hamlet would have had to learn, not just the languages, but what the important books in all these languages contained
Shakespeare probably had a good reason for establishing Hamlet's age as thirty in Act 5, Scene 1. The Clown says he has been sexton here for thirty years and he came to it the very day that Hamlet was born. Richard Burbage probably starred as Hamlet when it was first produced, and Burbage would have been several years past thirty at the time. It is true that the part in the play seems to call for a young man, but there was no way Burbage could have passed for a youth. The article on Burbage in Wikipedia includes a portrait of the actor apparently painted around the same time he played Hamlet. Burbage looks well weathered--and life expectancies were, of course, much shorter in those days. Burbage was born in 1567 and would have been around thirty-five years old when Shakespeare's Hamlet first appeared.
It would have been difficult for an actor of only eighteen or so to handle a complex role like that of Hamlet. An eighteen-year-old would have been more likely to play Ophelia.