Nov 13, 2016 07:46 PM
(This post was last modified: Nov 13, 2016 07:46 PM by SoyNuevo.)
Hey, so you can't do stuff like
Code Integer.parseInt(string.charAt(i));
because the parse method requires a string instead of a char.
However, if you do this:
Code Integer.parseInt("" + string.charAt(i));
java thinks it is perfectly acceptable and accepts the char. because java reads left -> right
I'm wondering if this is good practice or if there is a specific way I should be converting my chars to strings. thanks
In order to convert a char to a string you can simply use
String myString = new String(myChar);
1NevE2JpzhMs8rzCxSHAdmjtf1Ud8KGxJC
DO NOT DEAL WITH THIS MEMBER
OP, the method you currently use to parse a char into a String is an anti-pattern and should be avoided.
A proper way would consist of calling Character#toString.
(Nov 13, 2016 07:46 PM)SoyNuevo Wrote: Code Integer.parseInt("" + string.charAt(i));
Typically, you never want to be concatenating strings this way, even if it's just for a little hack-together like this.
(Nov 13, 2016 08:07 PM)Stealth™ Wrote: A proper way would consist of calling Character#toString.
This is the best way to achieve what you want.
Do you use an IDE to develop in? I'm surprised it didn't recommend that function.
--
Programmer for Hire
{C#/.NET/Rust/Go/Java/PHP}
DO NOT DEAL WITH THIS MEMBER
(Nov 13, 2016 07:46 PM)SoyNuevo Wrote: Hey, so you can't do stuff like
Code Integer.parseInt(string.charAt(i));
because the parse method requires a string instead of a char.
However, if you do this:
Code Integer.parseInt("" + string.charAt(i));
java thinks it is perfectly acceptable and accepts the char. because java reads left -> right
I'm wondering if this is good practice or if there is a specific way I should be converting my chars to strings. thanks
Just from what you're trying to do, converting an ascii char into an int:
PHP Code
<?php int x = (int) (string.charAt(i) - '0');
This would be a faster solution to what you're trying to do.
DO NOT DEAL WITH THIS MEMBER
(Nov 14, 2016 05:10 PM)Intel Wrote: Just from what you're trying to do, converting an ascii char into an int:
PHP Code
<?php int x = (int) (string.charAt(i) - '0');
This would be a faster solution to what you're trying to do.
That's even worse than what OP has
You can even just do
Code Integer.parseInt(string.substring(i,i+1));
(Nov 15, 2016 03:46 AM)TCP Wrote: You can even just do
Code Integer.parseInt(string.substring(i,i+1));
Accessing the underlying char array via String#charAt(int) and converting via String#valueOf(char) or Character#toString(char) is faster
(Nov 15, 2016 03:34 PM)Not2EXceL Wrote: Accessing the underlying char array via String#charAt(int) and converting via String#valueOf(char) or Character#toString(char) is faster
I am getting different results with this
Code String string = "number 5 is cool";
int i = 7, x;
long time = System.nanoTime();
x = Integer.parseInt(string.substring(i,i+1));
System.out.println(System.nanoTime()-time);
time = System.nanoTime();
x = Integer.parseInt(Character.toString(string.charAt(i)));
System.out.println(System.nanoTime()-time);
The first version is running faster with Java 8+ on my pc but when I put it online it is slower
DO NOT DEAL WITH THIS MEMBER
(Nov 15, 2016 04:06 PM)TCP Wrote: I am getting different results with this
Code String string = "number 5 is cool";
int i = 7, x;
long time = System.nanoTime();
x = Integer.parseInt(string.substring(i,i+1));
System.out.println(System.nanoTime()-time);
time = System.nanoTime();
x = Integer.parseInt(Character.toString(string.charAt(i)));
System.out.println(System.nanoTime()-time);
The first version is running faster with Java 8+ on my pc but when I put it online it is slower
there's no need to micro-optimize, just keep it simple and up to date procedure.
(Nov 15, 2016 04:06 PM)TCP Wrote: I am getting different results with this
Code String string = "number 5 is cool";
int i = 7, x;
long time = System.nanoTime();
x = Integer.parseInt(string.substring(i,i+1));
System.out.println(System.nanoTime()-time);
time = System.nanoTime();
x = Integer.parseInt(Character.toString(string.charAt(i)));
System.out.println(System.nanoTime()-time);
The first version is running faster with Java 8+ on my pc but when I put it online it is slower This is literally the worst benchmark in all of existence.
- You're doing 1 iteration
- You only ran it once(?)
- The JVM will not have had time to make necessary optimisations
- You're comparing run times from your computer to a fully loaded online java compiler
For more, read this and then create a proper benchmark.
@OP: "" + char is bad practice. Why? When you do:
string1 + string2
in Java, what's actually happening (when compiled, under the hood) is:
new StringBuilder().append(string1).append(string2).toString()
So we're creating a new StringBuilder object. This takes up memory, so we're using up more memory than we wanted (yes, the GC will eventually get to it, but it's still bad practice).
Furthermore, doing:
string1 + object1 (where object1 is anything BUT a string)
in Java, this happens instead:
new StringBuilder().append(string1).append(object1.toString()).toString()
Thus we're creating a new object and invoking the #toString() method created by the superclass of object1. We're using up a lot more resources than we need.
The preferred solution would be to, for example, use Character.toString(char).
New to trading cryptocurrencies but don't know where to start? Join Binance here! Binance is my favourite exchange for cryptocurrencies w/ low fees.
(Nov 16, 2016 05:10 AM)Chickendipper Wrote: This is literally the worst benchmark in all of existence.
Was not a benchmark just a test, every time I ran it myself the first solution was faster, same with multiple iterations in a loop
My point is there is no really a faster solution as the other guy said because it depends on environment
.charAt() is better for readability in such a context
|