+ 2
Questions on multiple values of a variable
Say int x=5 x=10 System.out.println(x) What will be printed as there are two values of x i am so confused my school is not helpful so i downloaded this app to learn java Please help 🙏.
4 ответов
+ 4
Write the code in a Java code bit on playground.
Run the code.
Observe the output.
Remember that statements in Java need to finish with ";"
+ 2
Simply follow the steps:
1. Go to Create section.
2. Tap '+' and then select Java to create a new java code bit.
3. In the playground, paste this code.
4. Run the code
5. Watch the output (it's an error).
6. Because every statement on Java ends with ";".
7. Fix it and run it again.
8. It'll be 10.
Reason:
For many programming languages like java, machines interpretes codes (or 'run' it). That means it that the mechines does everything step by step. Which means ir considers every statement as a step.
So 1st line defines an integer variable 'x' with value = 5.
2nd line, when you're mosy probably having problem, it just redefines the value.
Which means the variable is not having multiple values, it has a new value, the computer replaced the previous value with the new.
And it's being printed in the 3rd line, since it's value was set to 5, it'll print that.
-> if you don't know, variables are a pieces (temporary) data stored in the memory while running. So using = sign means it's storing the value in memory.
+ 1
the last assigned value applies. So 10 overwrites the 5 previously assigned
+ 1
Thank you so much guys for help appreciate it 🙏 and yeah i am a new student so i forgot the semicolon i will be used to soon