Sunday, December 7, 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

1 COMMENT

Most Popular

Dominic
32429 POSTS0 COMMENTS
Milvus
103 POSTS0 COMMENTS
Nango Kala
6803 POSTS0 COMMENTS
Nicole Veronica
11945 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12015 POSTS0 COMMENTS
Shaida Kate Naidoo
6934 POSTS0 COMMENTS
Ted Musemwa
7189 POSTS0 COMMENTS
Thapelo Manthata
6883 POSTS0 COMMENTS
Umr Jansen
6867 POSTS0 COMMENTS