Why is char preferred over a string

0 votes
Why this is so. I am not able to find an appropriate solution. Please help!
Jun 13, 2018 in Java by Akrati
• 960 points
446 views

1 answer to this question.

0 votes

Strings are immutable. That means once you've created the String, if another process can dump memory, there's no way you can get rid of the data before garbage collection kicks in.

With an array, you can explicitly wipe the data after you're done with it. You can overwrite the array with anything you like, and the password won't be present anywhere in the system, even before garbage collection.

So yes, this is a security concern - but even using char[] only reduces the window of opportunity for an attacker, and it's only for this specific type of attack.

answered Jun 14, 2018 by scarlett
• 1,290 points

Related Questions In Java

0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,120 points
3,357 views
0 votes
2 answers

what is the best way to convert an ArrayList to a String

You could probably use the Joiner class ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
931 views
0 votes
3 answers

How to check whether a string is empty or not? Is there a function for this?

str != null && str.length() != 0 alternatively str ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,910 points
988 views
0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
2,517 views
0 votes
4 answers

How to convert a char to a String?

There are two ways you can do ...READ MORE

answered Dec 29, 2020 in Java by Roshni
• 10,520 points
815 views
0 votes
0 answers

String to char array

How to convert a string to a ...READ MORE

Nov 3, 2023 in Java by Arya
• 990 points
172 views
0 votes
1 answer

How can two strings be concatenated in java?

You can concatenate Strings using the + operator: System.out.println("Your number ...READ MORE

answered Jun 6, 2018 in Java by Daisy
• 8,120 points
627 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,910 points
4,720 views
0 votes
2 answers

What is the easiest way to iterate through the characters of a string in Java?

There are two approaches to this: for(int i ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
1,475 views
0 votes
1 answer

Why there is the need of getters and setters in java

In Java getters and setters are completely ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,769 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP