In C# you can define delegates anonymously (even though they are nothing more than syntactic sugar). For example, I can do this:
public string DoSomething(Func<string, string> someDelegate) { // Do something involving someDelegate(string s) } DoSomething(delegate(string s){ return s += "asd"; }); DoSomething(delegate(string s){ return s.Reverse(); });
Is it possible to pass code like this in Java? I'm using the processing framework, which has a quite old version of Java (it doesn't have generics).
There is nothing equivalent to classes . Its a totally different paradigm. You can use structures in C. Have to code accordingly to make structures do the job.
There's no new / delete expression in C. The closest equivalent are the malloc and free functions, if you ignore the constructors/destructors and type safety.
C++ is a superset of C, so both languages have similar syntax, code structure, and compilation. Almost all of C's keywords and operators are used in C++ and do the same thing. C and C++ both use the top-down execution flow and allow procedural and functional programming.
delete keyword in C++ Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. Delete can be used by either using Delete operator or Delete [ ] operator. New operator is used for dynamic memory allocation which puts variables on heap memory.
Pre Java 8:
The closest Java has to delegates are single method interfaces. You could use an anonymous inner class.
interface StringFunc { String func(String s); } void doSomething(StringFunc funk) { System.out.println(funk.func("whatever")); } doSomething(new StringFunc() { public String func(String s) { return s + "asd"; } }); doSomething(new StringFunc() { public String func(String s) { return new StringBuffer(s).reverse().toString(); } });
Java 8 and above:
Java 8 adds lambda expressions to the language.
doSomething((t) -> t + "asd"); doSomething((t) -> new StringBuilder(t).reverse().toString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With