What does the symbol <?> mean? [duplicate] - java

This question already has answers here:
What does the question mark in Java generics' type parameter mean? [duplicate]
(6 answers)
Java syntax <?> explanation
(1 answer)
Closed 7 years ago.
While doing some android app development, I encounter the parameter AdapterView< ? > test. My question is what exactly does < ? > mean or do because I also see it in many other places such as a Map where it is Map< String ,? >.

In generic code, the question mark (?), called the wildcard, represents an unknown type.
The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type.
So in order to answer the question: it is a Wildcard-> Official Doc so you can handle classes event when you dont know the type.

Related

Java: can one name lambdas? [duplicate]

This question already has answers here:
How to make a lambda expression define toString in Java 8?
(5 answers)
Naming(toString) Lambda-Expressions for Debugging purpose
(2 answers)
Closed 1 year ago.
In our application we have a couple of utility methods that take lambdas as argument to execute misc. activities and among them they then call the passed lambdas.
During development we occasionally had errors in the lambdas, so we emit the lambdas as part of the error message. The default toString() of a lambda at least emits the classname (which is already a BIG help! Otherwise we would never have found some of the issues) but only with a cryptic suffix e.g. "classname_here$$Lambda$2497/0x0000000800e75c40#463f3a95".
For better error logging it would be very helpful if one could give a lambda a more telling name. Is that somehow possible? Can one assign lambdas a name or some "human readable" identification? For classes that contain LOTS of lambdas it can be pretty hard to find out, WHICH lambda caused the issue, so that would be helpful.

The maximum number of arguments in a Java construcor [duplicate]

This question already has answers here:
Maximum number of parameters in Java method declaration
(3 answers)
Closed 4 years ago.
What is the maximum number of arguments supported in a java constructor.
I am using android studio.
I am getting a too many parameters error when I use above 300 parameters.
According to the JVM docs:
The number of method parameters is limited to 255 by the definition of a method descriptor (ยง4.3.3), where the limit includes one unit for this in the case of instance or interface method invocations.
I guess it's the same with constructors. Either way, your code needs some refactoring to do if it really has a method with over 10 arguments.
method( YourCustomObjectContaining301params o301p ) is all you need

Why I must have Map<Long,... and not Map<long, [duplicate]

This question already has answers here:
Why don't Java Generics support primitive types?
(5 answers)
Closed 7 years ago.
That's all. I understand that Long is an object, and long is a primitve type, so why can't I map an object to a number?
First of all... Long is not an object, it is a Class.
Now about your question...
In Generics,unlike C#, Java doesn't support primitive type yet.

Error Casting in Java [duplicate]

This question already has answers here:
Converting a generic argument to an int in java, provided that it is a number
(4 answers)
Closed 8 years ago.
I have the next problem:
I am making a simple cast:
TargetBot2Params params = (TargetBot2Params)bot.getParams();
But I get the next error:
Inconvertible types
Requiered: TargetBot2Params
Found: UT2004BotParameters
And I dont know why occurs this, because I think that it doesnt do the cast.
Anyone knows why occurs this?
Thanks for your time.
The error message says it all.
For the casting to be successful, UT2004BotParameters has to either be a TargetBot2Params (i.e., extends TargetBot2Params) or, if TargetBot2Params is an interface, implement it.

How to get all possible values of an enum in java? (not knowing the specific Enum) [duplicate]

This question already has answers here:
How to get all enum values in Java?
(8 answers)
Closed 5 years ago.
I'd like to create a JComboBox that handles the selection of any Enum given to it. For that I need a method to retrieve all the available values of the Enum passed to the JComboBox. As I don't know the specific Enum I can't call EnumType.values().
I could think of some complicated solutions where supported Enums would have to implement some interface I define, but I guess I am missing a simpler, more general solution. What is the way I should go?
Class.getEnumConstants()

Categories