Thursday, October 16, 2025
HomeLanguagesJavajava.net.PasswordAuthentication Class in Java

java.net.PasswordAuthentication Class in Java

PasswordAuthentication class is provided by package java.net for implementing networking applications, and it is used in those cases when it is required to hold the data that will be used by the Authenticator. It holds the username and password.

Syntax of its constructors :

PasswordAuthentication(String userName, char[] password)

This will create new PasswordAuthentication object for the given username and password. The given user password is cloned before it is stored in the new PasswordAuthentication object.

Method  Return Type
getUserName() Returns the username.
getPassword() Returns the user password.

Method details: 

  1. getUserName() : This will give the username and is return a string value.
  2. getPassword() : This will return user password and return char array.

Methods it inherited from class java.lang.Object : 

  1. equals()
  2. toString()
  3. hashCode()
  4. clone()
  5. getClass()
  6. finalize()
  7. notify()
  8. notifyAll()

Java




// Java Program to illustrate the 
// java.net.PasswordAuthentication
// Class
import java.io.*;
import java.net.PasswordAuthentication;
  
class GFG {
  
    public static void main(String args[])
    {
        GFG acc = new GFG();
        acc.proceed();
    }
  
    private void proceed()
    {
        // Initializing the user name
        String userName = "Geek";
        
        // Initializing the password - This is a char
        // array since the PasswordAuthentication
        // supports this argument
        char[] password = { 'g', 'e', 'e', 'k', 'g', 'o',
                            'r', 'g', 'e', 'e', 'k', 's' };
  
        PasswordAuthentication passwordAuthentication
            = new PasswordAuthentication(userName,
                                         password);
        System.out.println(
            "UserName: "
            + passwordAuthentication.getUserName());
        
        // The below getPassword actually returns the
        // reference to the password as per the Java API
        // documentation.
        System.out.println(
            "Password: "
            + passwordAuthentication.getPassword());
        
        // You can get the password in normal string
        System.out.println(
            "Password: "
            + String.copyValueOf(
                passwordAuthentication.getPassword()));
    }
}


Output

UserName: Geek
Password: [C@4e50df2e
Password: geekgorgeeks
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS