-1

In Java, let's say I have...

let hello = 10;

Is it possible to retrieve hello's value (10) using the string reference 'hello'? Something like valueOf(....'hello'....) = 10

3
  • 2
    It is very unlikely that you have a class called let (which does not conform to the Java naming conventions) that can be assigned an integer. Is your question about javascript? Commented Jan 11, 2023 at 6:33
  • This may not be a duplicate. It may be a misunderstanding as pointed out by @JohannesKuhn. Commented Jan 11, 2023 at 6:36
  • sorry for the misunderstanding, it is Javascript / Google Script. What I need to do is use the string 'hello' to return the variable of the same name, surely this possible? Commented Jan 11, 2023 at 22:34

1 Answer 1

-2

This is how you can retrive in Java. Also let is used in JavaScript not in Java.

public class Test {

    public static void main(String[] args) {
        String hello = "10";
        int helloNum = Integer.valueOf(hello); //converting String "10" into int 10
        
        
        int num = 10;
        String numString = String.valueOf(num);//converting int 10 to string "10"
        
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

From my understanding this answer does not match the question. It's about dynamically accessing some variable by a given string, not about parsing the string.
@berse2212 is spot on
I've just found the answer, by using eval('hello') to get to the hello variable.
@user1488934 eval does not exist in Java. Was your question about javascript all along?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.