Close
Close

Doubt

   sriram

code:

 

class Ans{
  public static void main(String[] args){
    System.out.println(2+4+5);
  }
}

 

Doubt: The answer given in this code is to add the sum of these numbers. Can we add these numbers directly as in the code. I thought we have to assign like this  (first giving the value (2, 4, 5) respectively to  some alphabet (terminology -variable i think) and give a formula to add. Because I thought while reading previous codes, I found like assining some variable a value – (first we have to allocate some memory for these variables and then giving value. Correct me if i am wrong. I a beginner. Just I want to learn from the basics. I want to be strong in basics like these. That is why I am asking even smallest doubts.


Answers

  •   

    Yes, you can do this. It is the ‘+’ operator which will give the desired value. In the function ‘println’, the ‘+’ operator will be evaluated first and then will be equivalent to 

    System.out.println(11);
    

    And this you can obviously do. 

    No memory will be allocated for any special variable here.



  •    Larry Martin

    While the code adds numbers directly, your idea of assigning variables and using formulas is valid. Understanding both methods is crucial for building a strong programming foundation. Keep questioning and learning!
    affordable airport shuttle services in Lihue HI 



Ask Yours
Post Yours
Write your answer