Error Casting in Java [duplicate] - java

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.

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.

Java: Constructing a class within an arraylist initialiser causes an error [duplicate]

This question already has answers here:
Initialization of an ArrayList in one line
(34 answers)
Closed 3 years ago.
I've got a constructor that takes a parameter ArrayList<Setting> settings.
Now the problem is that I wrote the following call in a subclass:
super(new ArrayList<Setting>(){new Setting("", this, 0)});
This causes a lot of errors, the main one being Invalid method declaration; return type required, as well as '{' or ';' expected, Parameter expected, Unexpected token and Constructor Setting() is never used.
I tried switching to using regular arrays and it worked fine:
super(new Setting[]{new Setting("Exp Only", this, false)});
For now, I'm happy just using regular arrays, however I come across this error rather frequently, is there something I'm doing wrong or is this just the way it is, and if so, why?
Java has introduced of method since java 9. You can use it as below.
super(List.of(new Setting("Exp Only", this, false)));

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

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.

Interview homework: Which is better Java ArrayList creation [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Type List vs type ArrayList in Java
Hi I have been asked the following question in an interview:
Which of the following would you choose recommended when and where?
ArrayList<SomeType> a = new ArrayList<SomeType>(); or
List<SomeType> a = new ArrayList<SomeType>();
I dont know much difference if anyone knows kindly please help. Thanks in advance.
Second is better cause you can simply change underlying implementation from ArrayList to LinkedList (or to any proper implementation) by one line code change.

Generic syntax symbol <~> [duplicate]

This question already has answers here:
What does a tilde in angle brackets mean when creating a Java generic class?
(4 answers)
Closed 9 years ago.
Working with IDE like NetBeans or IDEA, i've seen that they are converting generic types into this symbol:
private final List<String> ar = new ArrayList<~>();
But using this in a simple editor results in throwing an error. BTW Eclipse also doesn't like it. Is it somehow connected with type erasure mechanism?
You will see that in IntelliJ its a different colour!
This is because it has used code folding to hide the code, but you can't write it this way in Java 6.
In Java 7 you can write
private final List<String> ar = new ArrayList<>();

Categories