Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am developing a Java program and I'm meeting cases where I get undecided whether to use the casting a string to integer method, or to use the integer.parseInt method. Is there any clear benefit for either of the two methods?
With 'casting to string method', I mean:
String.valueOf(integer);
As far as I know, it's not possible to cast from a String to an int, so using Integer.parseInt seems like the best option here.
Looking at your edits about using valueOf, perhaps this link may help: Integer.valueOf() vs. Integer.parseInt()
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I started learning java and always had this bugging my mind.
When to use concat() and when to use append() operations.
Do they perform the same operation and what of their return types?
concat():
String has a concat method, string is immutable.
adds a string to another string.
It will create the new object after concatenation is done, since it is a immutable.
append():
StringBuilder and StringBuffer has append method, these two are mutable.
appends a char or char sequence to a string.
It will not create a new object, since it is a mutable one.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
So, I have a SparseArray which stores Roboto typefaces.
I have a FontHelper Class which helps me to organize Typefaces and store in a cache.
I have a method to get the typeface I want to:
public static Typeface getRobotoFont(int fontType, Context context) {}
The fontType parameter will be an Integer constant which will access the SparseArray. For instance, you want Roboto-Regular, you'd pass the integer ROBOTO_REGULAR. My question is, instead of integer constants, would it be better if I just used enums, as in:
enum bla {
ROBOTOREGULAR(0),
...
}
I think the most important difference is the type checking. Enums are checked, Integer constants are not.
You can use an enum, or for a more Android friendly practice, you can use the IntDef pattern.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Why doesn't java.lang.Short (or Float or Double) class have a reverse method similar to java.lang.Integer ?
They both do have reverseBytes method though.
Why isn't the API list consistent ?
short can hold 2 bytes and it would have made sense to have a reverse method as well.
Wouldn't it ?
Thanks
While I agree on the API criticism, it's pretty simple to emulate:
short input = ...;
short reversed = (short)(Integer.reverse(input) >> 16);
So maybe the answer is:
Not enough people felt it was necessary
It's easy enough to simulate
Someone wanted to show off with the implementation of Integer.reverse()
Every line of code needs to maintained. Less code == less bugs, lower cost, easier maintenance.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have tried to return null as third value in my boolean function, but it won't compile. I need to return three values from my class method - true, false and null (for example). Is there any standard way how can I do it?
Please use an enumeration with three values defined. Hacking things together is no solution.
Similar question has been asked, it should help.
You can make your own POJO object with this logic in getXX() method. From your method return this POJO with value and test it in code.
Generaly, don't use null values as state indicators.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I know i can get the attribute names, types, and set their values from an Object.
I wanna know what's the best way to get their values via reflections.
EDIT:
For meaning. Best is like, less code, less memory and faster execution.
Like:
Is it better if I try to invoke the methods to get their values, or if I use something like this:
Object obj;
Class cls = obj.getClass();
cls.getField("atribute1").get(obj).toString();
The best way I know of to do that is to use Apache BeanUtils