This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting an array of chars to a String??plz help

 
Aboo Bolaky
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello...im building a SWING login interface with the JPasswordField Component. To obtain the unmasked password i use
char password[]=txtPassword.getPassword();

the function getPassword returns an array of chars...but i need the result as simply of type String.
i tried the type casting several times but im not having the hang of it...can anyone help me plz?
thanks in advance,
Drftwy
 
Victor Ho
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String class has a constructor that takes in char[]:
public String(char[] value);

simply do:

char[] pwdArrray = ...
String pwdStr = new String(pwdArrray);
 
Aboo Bolaky
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnks..
didnt think it would be that easy...was struggling with type casting!!..
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering object references, note that you cannot change the type of an object with a cast. A cast creates a reference of a different type than the reference being cast, but both references would point to the same, unchanged object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic