I am trying to learn a java-based program, but I am pretty new to java. I am quite confusing on the following two lines of java code. I think my confusion comes from the concepts including “class” and “cast”, but just do not know how to analyze.
For this one
XValidatingObjectCorpus<Classified<CharSequence>> corpus
= new XValidatingObjectCorpus<Classified<CharSequence>>(numFolds);
What is <Classified<CharSequence>> used for in terms of Java programming? How to understand its relationships with XValidatingObjectCorpusand corpus
For the second one
LogisticRegressionClassifier<CharSequence> classifier
= LogisticRegressionClassifier.<CharSequence>train(para1, para2, para3)
How to understand the right side of LogisticRegressionClassifier.<CharSequence>train? What is the difference between LogisticRegressionClassifier.<CharSequence>train and LogisticRegressionClassifier<CharSequence> classifier?
These are called generics. They tell Java to make an instance of the outer class – either
XValidatingObjectCorpusorLogisticRegressionClassifier– using the type of the inner object.Normally, these are used for lists and arrays, such as ArrayList or HashMap.
What is the relationship between
XValidatingObjectCorpusandcorpus?corpusis just a name given to the newXValidatingObjectCorpusobject that you make with that statement (hence the= new...part).What does
LogisticRegressionClassifier.<CharSequence>trainmean?I have no idea, really. I suggest looking at the API for that (I think this is the right class).
What is the difference between
LogisticRegressionClassifier.<CharSequence>trainandLogisticRegressionClassifier<CharSequence> classifier?You can’t really compare these two. The one on the left of the
=is the object identifier, and the one on the right is the allocator (probably the wrong word, but it is what it does, kind of).Together, the two define an instance of
LogisticRegressionClassifier, saying to create that type of object, call itclassifier, and then give it the value returned by thetrain()method. Again, look at the API to understand it more.By the way, these look like wretched examples to be learning Java with. Start with something simple, or at least an easier part of the code. It looks like someone had way too much fun with long names (the API has even longer names). Seriously though, I only just got to fully understanding this, and Java was my main language for quite a while (It gets really confusing when you try and do simple things). Anyways, good luck!