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:
- getUserName() : This will give the username and is return a string value.
- getPassword() : This will return user password and return char array.
Methods it inherited from class java.lang.Object :
- equals()
- toString()
- hashCode()
- clone()
- getClass()
- finalize()
- notify()
- 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())); } } |
UserName: Geek Password: [C@4e50df2e Password: geekgorgeeks