Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Interoperability with dynamic languages=== This example illustrates how Java and C# can be used to create and invoke an instance of class that is implemented in another programming language. The "Deepthought" class is implemented using the [[Ruby (programming language)|Ruby programming language]] and represents a simple calculator that will multiply two input values ({{mono|a}} and {{mono|b}}) when the {{mono|Calculate}} method is invoked. In addition to the conventional way, Java has [[GraalVM]], a virtual machine capable to run any implemented programming language. {| style="width:100%; border:none;" class="wikitable" |- !width=50%|Java !width=50%|C# |- |valign=top| ====Using GraalVM==== <syntaxhighlight lang=Java style="font-size:90%"> Context polyglot = Context.newBuilder().allowAllAccess(true).build(); //Ruby Value rubyArray = polyglot.eval("ruby", "[1,2,42,4]"); int rubyResult = rubyArray.getArrayElement(2).asInt(); //Python Value pythonArray = polyglot.eval("python", "[1,2,42,4]"); int pythonResult = pythonArray.getArrayElement(2).asInt(); //JavaScript Value jsArray = polyglot.eval("js", "[1,2,42,4]"); int jsResult = jsArray.getArrayElement(2).asInt(); //R Value rArray = polyglot.eval("R", "c(1,2,42,4)"); int rResult = rArray.getArrayElement(2).asInt(); //LLVM (in this case C, but could be C++, Go, Basic, etc...) Source source = Source.newBuilder("llvm", new File("C_Program.bc")).build(); Value cpart = polyglot.eval(source); cpart.getMember("main").execute(); </syntaxhighlight> ====Traditional way==== <syntaxhighlight lang="java" style="font-size:90%"> // Initialize the engine var invocable = new ScriptEngineManager().getEngineByName("jruby"); var rubyFile = new FileReader("Deepthought.rb"); engine.eval(fr); </syntaxhighlight> <syntaxhighlight lang="java" style="font-size:90%"> // create a new instance of "Deepthought" calculator var calcClass = engine.eval("Deepthought"); var calc = invocable.invokeMethod(calcClass, "new"); // set calculator input values invocable.invokeMethod(calc, "a=", 6); invocable.invokeMethod(calc, "b=", 7); // calculate the result var answer = invocable.invokeMethod(calc, "Calculate"); </syntaxhighlight> |valign=top|<syntaxhighlight lang="csharp" style="font-size:90%"> // Initialize the engine var runtime = ScriptRuntime.CreateFromConfiguration(); dynamic globals = runtime.Globals; runtime.ExecuteFile("Deepthought.rb"); </syntaxhighlight> <syntaxhighlight lang="csharp" style="font-size:90%"> // create a new instance of "Deepthought" calculator var calc = globals.Deepthought.@new(); // set calculator input values calc.a = 6; calc.b = 7; // calculate the result var answer = calc.Calculate(); </syntaxhighlight> |- valign=top | Notes for the Java implementation: * Ruby accessors names are generated from the attribute name with a {{code|{{=}}}} suffix. When assigning values, Java developers must use the Ruby accessor method name. * Dynamic objects from a foreign language are not first-class objects in that they must be manipulated through an API. | Notes for the C# implementation: * Objects returned from properties or methods of {{mono|dynamic}} objects are themselves of {{mono|dynamic}} type. When type inference (the {{mono|var}} keyword) is used, the variables calc and answer are inferred dynamic/late-bound. * Dynamic, late-bounds objects are first-class citizens that can be manipulated using C# syntax even though they have been created by an external language. *{{mono|new}} is a reserved word. The {{code|@}} prefix allows keywords to be used as identifiers. |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)