This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Question marks in Java generics.
I'm editing someone else's code for an assignment and I'm trying to clean it up to get rid of the dozens of warnings in it and Eclipse was giving warnings for the use of Collections as a raw type. When I took it's suggested fix it created this.
Collections<?>
Example
public static String separatedString(Collection<?> c, String separator) {
return separatedString(c, "", separator, "", new StringBuffer())
.toString();
}
I was just wondering exactly what this did and whether or not it was safe.
This ist the concept of generics.
Its all about object oriented programming
For example if i declare a variable as Collection<MyClass> ONLY and ONLY objects that are of declared Type or Subtype of MyClass may be put in it.
This is good to keep things straight and put constraints on the way this code should be used.
The question mark stands for class of your choice.
When you initialise the class you can ... whoops just seeing there is an exact duplicate here:
What does the question mark in Java generics' type parameter mean?
Adding
MyClass<?>
adds generics to the code, but doesn't really add much benefit since the naked question mark can mean any class. Google and read up on generics, and you'll learn how to create generics that do constrain what classes may be used and how this adds the benefit of compile-time type checking.
e.g.,
MyClass<? extends Comparable>
Which will constrain coders to only using Comparable types with MyClass. A basic tutorial starts here: Java Generics Tutorial
Related
This question already has answers here:
Is "public static final" redundant for a constant in a Java interface?
(6 answers)
Closed 3 years ago.
What exactly the meaning of instance fields in JAVA ?
As per am knowing in JAVA :
An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
But, When I have tried as below :
interface TempIn
{
TakeInput tv=null;
String name="";
int temp=0;
void printT();
}
and it's working. How ?
Confused...
Simple: all these fields are static and final by default.
Therefore the java language allows you to write down something that is implicitly given.
In other words: imagine the "compiler" putting down the keywords for you.
But I agree, this is a bit of confusing. And it also turns a bit into a "style" thing. In the early years of Java, a lot of people would add these redundant keywords to their interfaces. On the other hand, "clean code" tells us to avoid redundancy in our code. And nowadays, an IDE like IntelliJ will even give you warnings when using the keywords. So, my recommendation:
don't touch old, existing code
talk to your team, and decide what makes sense for you, and for new code, follow that agreement
This question already has answers here:
What does <T> (angle brackets) mean in Java?
(6 answers)
Closed 6 years ago.
I am working on a tutorial and the example code has the following line:
public class GreenAdapter
extends RecyclerView.Adapter<GreenAdapter.NumberViewHolder> {
How does <GreenAdapter.NumberViewHolder> fit in, what does the <> syntax mean?
From the code it seems you are working in Android.
<> is the syntax for widely used feature in java called Java Generics introduced in Java 5.0. They extend Java's type system to allow “a type or method to operate on objects of various types while providing compile-time type safety.”
For an example: if you have had a look at Collections Apis of java, you will find same class with different generics type passed to it. Have a look at ArrayList<> , HashMap<> and other collection apis.
With whatever type you pass , class will start working on that type only and will take care of type safety. So I'll advice you to first go through a very basic tutorial of Java generics before proceeding any further.
Here is the link for very very basic tutorial for Java generics that will clear some air:
https://www.tutorialspoint.com/java/java_generics.htm
And then if you want to get a deeper understanding of this, read some reference books over this topic. Have a good day :)
This question already has an answer here:
Passing parameterized Class instance to the constructor
(1 answer)
Closed 8 years ago.
I wanted to obtain class object of Iterator and discovered that
Iterator<String>.class
is not valid. This highlighted a question if its possible at all to use
".class"
syntax for
Object<T>
kind of objects in Java.
If yes, then how it can be done?
If not, what are the alternatives ?
I am doing Mocking for Iterator using Mockito's
Mockito.mock(Class<T>)
syntax.
You can't do this because generics in Java are implemented using erasure i.e. you don't get specialized version of your classes for each type parameter used at runtime. Take a look at this answer in case it didn't turn up in your search.
It is not possible. The way that generics are implemented in Java, they only exist at compile time, for the purpose of type checking.
This question already has answers here:
"Program to an interface". What does it mean? [duplicate]
(8 answers)
Closed 8 years ago.
I've seen many times statements like this:
List list = new ArrayList<String>();
What's the point of writing like this? I mean setting the type of object a super class or implemented Interface of it. Is this makes a difference or improves performance or things like that?
Note
My English is poor and I've probably written the question title and body confusing. Please edit that and then remove this line.
That statement means that you can use all of the features of List that are implemented using ArrayList but you could also use the features inside LinkedList. Although there is no harm if you have the object type as ArrayList, it could be useful for example, if you have a method that takes in a List parameter instead of an ArrayList, your choices have expanded and you can have other classes that implement that type, e.g LinkedList.
To put it simply, if you are using the interface List as its type, you are saying: "I want to use any class that does these things to do my own" as List is an interface. But if you just use ArrayList, you are saying: "I want to use just this specific class to to my things". In summary, it gives you more flexibility.
Other than that, there isn't much of a difference using the super class as the type or using ArrayList or other subclass as itself.
Hope that this is what you are asking about, and hope that it makes sense. If not, feel free to ask any questions.
This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 9 years ago.
Which would be better in generics? Seems same to me, but eclipse complains to just plain List, but not with List<Object>.
Parameterzation should be used. It provides the compiler with details for things such as casting and autoboxing.
With this, especially when types other than Object are used, the compiler can handle the casting and ensuring compatibility. Especially with return values and method signatures, the true type parametrized may not be exposed out of bytecode to other classes.
Do note that List<Object> is semantically and programatically almost as useless as List. Make sure that if you can, use a more specific type. If declaring a class or method, make sure to use a proper, specific wildcard.
Neither. It is better to specify the specific generic type of the list contents if possible. At the very least a common interface should be used.