1

Example--- Like for SQL we write SQL statement as String and then we execute

strsql = "Select * from international_table ";

now what we do we execute this statement similarly

I want in java or c#

like this

strjavacode = "String str = \" Masdfksdja \";System.out.Println(str); "

and after that some how to execute this

I'm eager to do something like this in which I will not have to declare variable and assign values or statement several times.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95

3 Answers3

6

Yes, there are languages where this is possible, such as Javascript and PHP, which have an eval() function that allows you to run a arbitrary string as code.

However, your example would be considered a needless abuse of the feature in pretty much any language, since instead of having separate variables String s1 up to String s10, you should use an array to represent a bunch of values accessible via an index. That's what arrays are for.

Java (like C#, C++ and many other languages) does not have this functionality. You can compile code on the fly, but then it has to be part of a class, and it's a very complex process.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
3

You think you might need eval but then in Java you could also implement the command pattern. This won't let you execute arbitary code though.

interface Command {
   void execute();
}

class StringOutputCommand implements Command {

   private final int value;

   public StringOutpuCommand(final int newValue) {
       this.value = newValue;
   }

   void execute() {
     // do something with value 
      System.out.println(value);
   }

}

// then elsewhere you could have a bunch of these command objects
 List<Command> commands = new ArrayList<Command>();    

 for (int i = 0; i < 10; i++) {
     commands.add(new StringOutputCommand(i));
 }

 // which you could execute like this ...
 for (Command command : commands) {
    command.execute();
 }
blank
  • 17,852
  • 20
  • 105
  • 159
1

Depends what you want. If after the execution you want s1 and s100 to be available for further use then this is no at all possible. Nor is it possible to write arbitrary code that can access any non-global variables.

What you can do is write a class that implements an interface and compile that class at runtime. You can then instantiate an instance of this new class using reflections. You can query this object before, during and after execution if the interface provides appropriate methods. You could also use reflections to gain access to the fields of the object as well.

This is all very hacky though, and trying to accomplish things in a way Java is not suited for. If you just want to create 100 arbitrary strings then it's probably better to use an array and create your own small DSL to accomplish simply tasks.

One last method you may wish to try is to use scripting languages that run on the JVM such as Jython or Groovy. Groovy is great for this. Since Groovy is a superset of Java you don't have to worry about valid Java not compiling. Though what might be of concern is that invalid Java might compile. eg String s = 'some string' is valid Groovy, but not valid Java.

Using Groovy you could do something like:

import groovy.lang.GroovyShell;

public class SomeClass {

    public static void main(String[] args) {
        GroovyShell shell = new GroovyShell();

        for (String arg : args) {
            Object result = shell.evaluate(arg);

            System.out.println("result is: "+result);
        }
    }

}

For this you could have input args of say String s1 = "hello";, String s2 = "world"; and String combined = s1 + " " + s2;.

Dunes
  • 37,291
  • 7
  • 81
  • 97