I am trying to convert some Java to C# and I have a line as follows:
int[][] variableName = get();
What my question is is what does "get();" actually do? There is no function or method in the Java code I am converting called "get()" so I am assuming "get();" simply creates an empty object of the required type, in this case, an empty int[][]. Would I be correct in this assumption or does "get()" in Java have some other meaning?
I have searched for "get()" within stackoverflow but the () are ignored and as a result I get masses of information about HTTP GET which is not what I'm after so excuse me if this is duplicated anywhere else.
All help appreciated.
There is no function or method in the Java code I am converting called "get()"
There must be, either in that class or one of its superclasses, or as a static import although that's not very likely. (Nice one, Jesper!) My guess is that you haven't checked all of the superclasses.
...so I am assuming "get();" simply creates an empty object of the required type, in this case, an empty int[][]. Would I be correct in this assumption or does "get()" in Java have some other meaning?
No, unlike C#, get is not a keyword and has no special meaning in Java. That line of code calls a method called get (it could just as easily be called foo) which is declared in the class or one of its superclasses. It may be a static or instance method, but it will be defined by the class or one of its superclasses, or as a static import.
Related
I have a question about java's method reference in the following format;
acollection.foreach(System.out::println);
I would expect the code to be something similar to;
acollection.foreach(System.PrintStream::println);
Would you please explain the idea behind relating implementation(code) to an object?
PrintStream.println() is an instance method and thus has to be executed on an instance (object).
acollection.foreach(System.out::println); does just that. It will execute println on System.out for each element in the collection.
acollection.foreach(System.PrintStream::println); can't work because no instance of PrintStream is given - and also because System.PrintStream is not a valid statement.
This question already has answers here:
Reflection: get invocation object in static method
(4 answers)
Closed 7 years ago.
We all know that in Java you can call a static method as an instance method like this:
Foo foo = new Foo();
FooBar fooBar = foo.bar(); // bar is a static method on class Foo
What I want to know is:
Is there any way to determine inside bar whether bar was called statically (Foo.bar()) or called via a class instance as above?
If so, is there any way for bar to get a reference to the object which called it (in this case foo)?
Reason:
I am developing a kind of semantic syntax. I want my consumers to be able to put things like:
With.attribute("blah").and().attribute("blahblah"); // both "attribute" and "and" methods return an object of type "With"
Here you can see that attribute is being called both as a static and an instance method. However, you can't define a static and an instance method with the same name in Java, for the same reason as above - the static method could be called as an instance method and so to create an instance method with the same name would create ambiguity. Therefore I want to create a single method attribute which can be called both statically and non-statically, and inside the method body I want to try to determine if it was invoked statically or non-statically. The above questions will help me to assess the feasibility of doing this.
No, there is no way for the method to know whether it was called on a class or on an instance (at the JVM level there is no difference), and there is no way to get the instance that the method was called on.
The term for this kind of "semantic syntax" is domain-specific language (DSL).
Possible solution: name the static method withAttribute, then you could make it look like this:
withAttribute("blah").and().attribute("blahblah");
Java isn't made to do this, because it is not a very good thing to do.
If you want to be able to do something like this, why not do something like
new foo().withAttribute("blah").withAttribute("blahblah");
It really isn't a very good idea to do this at all.
Plus, why are you making your consumers use java code to give instructions? why not do something like
make a foo //Check for and chop off "make a" to get the object to create
add attribute blah //Check for and chop off "add attribute" to get the attribute to add
add attribute blahblah //Or, if you can add other things too, check for and chop off "add", then check for and chop off "attribute"
It seems like that would be much simpler.
Sometimes I see methods that have a parameter like public method(ObjectName variable)
Does that mean this method only accepts ClassName's objects? For example from another class?
it might be a simple question but I am used to seeing only int's, Strings etc. as parameters. I guess some methods can accept instances of other classes? How does compiler know then that the class in the parameter is valid? Does it know it from imports?
Basically, yes to all your questions. Some methods accept instances of classes, both others or this class. Compiler knows by imports and the other classes in the package. If you have a more specific question put some code up.
The formal rules can be found at http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2
Basically if a method accepts class A then it can also accept any subclass of A. A can be any class, BigInteger InputStream URL etc. If a method can accepts interface B then it will also accept any class that implements B.
I know this is simple but I don't really understand the question...
Assume the signature of the method xMethod is as follows.
Explain two different ways to invoke xMethod:
public static void xMethod(double[] a)
I thought to invoke a method you just do:
xMethod(myarray);
What could it mean by asking two different ways? Maybe I'm just looking into the question too much.
For kicks, show your professor this:
XClass.class.getMethod("xMethod", a.getClass()).invoke(null, a);
Then tell them the two answers are
XClass.xMethod(a);
//and
xObject.xMethod(a); //this is bad practice
If this is for a first time java class, my guess is he is looking for these 2 cases:
//the one you have, using a precreated array
double[] myArray = {1.1, 2.2, 3.3}
xMethod(myarray);
//and putting it all together
xMethod(new double[]{1.1, 2.2, 3.3});
Basically illustrating you can make an array to pass, or simply create one in the call.
Just a guess though
You could invoke it either by calling it on a class, or via an instance of that class.
Foo.xMethod(a);
or:
Foo foo = new Foo();
foo.xMethod(a);
The first approach is prefered, but the second one will compile and run. But be aware that it is often considered a design flaw in the language that the second approach is allowed.
static methods are not bound to the construction of the class.
The method above can be called either by constructing the class or just by using the namespace:
Classname myclass = new Classname();
myclass.xMethod(myarray);
or you could just do:
Classname.xMethod(myarray);
as you see, you don't have to construct the class in order to use the method. On the other hands, the static method can't access non-static members of the class.
I guess that's what the question meant by 2 different ways...
There is only one valid way to do this:
YourClass.xMethod(myDoubleArray);
But, you can write non-totally-correct Java:
YourClass instance = new YourClass();
instance.xMethod(myDoubleArray);
This will work, but is considered as wrong. The Java compiler will even complain about it. Because a there is no need of invoking a static method by creating an instance of the class. Static means that the method is instance independent. Invoking it through an instance is pointless and confusing.
Later on, you will see that there is a second correct way of doing it, ie "reflection". But that is an advanced topic, so I assume you are not supposed to know this already.
So, I'm looking through a java library (JScience) after someone here thoughfully pointed me towards it for getting Vectors (mathematical ones, that is) in java.
Unfortunately, I've never seen anything in my life before like:
public static <F extends Field<F>> DenseVector<F> valueOf(F... elements)
as a method you can call in the DenseVector class. What...does that even mean. Is it returning a "<F extends Field<F>>" (and if so, why does Eclipse think it's an input?)
http://jscience.org/api/org/jscience/mathematics/vector/DenseVector.html#valueOf(F...)
It really confuses me. I can't make a new DenseVector() because only the super class has that, and it's protected, and trying to do DenseVector.valueOf() apparently only works if I give it...that...weird thing as an input.
I've seen people having to instantiate methods when trying to instantiate objects (or something like that)...is that like that (or IS it that?)) What is the API trying to get me to do?
I'm kind of confused that I've learned java in school (and used it a bit at work, though we use a lot of differnet stuff besides just java), and never came across anything like this. What's it for? What's it trying to get me to do? Is it new? Old? Obscure?
-Jenny
You should be able to invoke this method to create a vector, like this:
Real r1 = Real.ONE, r2 = Real.valueOf(2D), r3 = Real.ZERO;
DenseVector<Real> v = valueOf(r1, r2, r3);
In this example, the type argument F is Real. Real obeys the constraint "extends Field<F>" because it implements Field<Real>.
For different applications, different fields are likely to be used. For example, security applications might use the ModuloInteger field. It's a little confusing because this is a mathematical field, not a "vector field" like one talks about in physics.
By using type variables, this library helps to make sure you perform all operations within a given field. For example, given v declared as a DenseVector<Real> like above, the compiler will complain if you try to multiply it by a Complex number.
It's a generic return type. See here for a tutorial on Java Generics.
These are called Generic types. They've been added in Java 5 and are similar to C++ templates.
The idea is that you define a collection of items of a particular type rather than something general.
This helps you avoid frequent downcasting. In older Java code, suppose that you knew your vector would contain only X's. Once you retrieved items out of that collection, you would just get Object, and you had to explicitly downcast it.
It is also safer because you can't put Ys into a vector of Xs, and clearer to read for the same reasons.
The story behinds the "extends" in these brackets is that you can define collections of "Xs and all their subtypes" that would still accept subtypes of X but reject Y.
public static <F extends Field<F>> DenseVector<F> valueOf(F... elements)
Lets break this down:
public static
Its a public static method.
<F extends Field<F>>
Its a generic method for any class F where F is an extention of Field
DenseVector<F>
It returns a (generic) DenseVector for F
valueOf(F... elements)
A method named valueOf where parameters are zero or more Fs.