Open In App

java.net.PasswordAuthentication Class in Java

Last Updated : 28 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads