How do I get the username from the user ID? I want to print the username knowing just the user ID. How do I achieve this?
You can use user_load or \Drupal\user\Entity\User::load to do this. Below code will provide the solution

 public function content() {
     $account = \Drupal\user\Entity\User::load('uid'); // pass your uid
     $name = $account-&gt;getUsername();
     drupal_set_message($name);
 }


$name gives the username of the user.