Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how the program works?

 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all!! I am beginner in Java language and i want your help.
Can anyone explain me exacthly how the program runs?
Which value have the variables every time?


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a homework assignment to me. How do you think that it works?
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot understand the difference between the local variables and which variables cosidered local and the global variables. And why the x has the value 1?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a parameter is passed to a method, the variable referring to it is local to that method. This might shadow a variable of the same name.

For example, variables A, X, and Y are all declared at the class level. However, there are also variables A, X, and Y that are local to the method P1. These local variables shadow the class variables.

To help understand how this code works, start with the first line in main...

Without going any further, answer 2 questions:
1) What are the values passed to P1?
2) After that method executes, what are the values of class variables A, X, and Y?
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help!
1) I believe the values are A=10, Y=4, X=1
2)The class variables are the same bacause are declared static, isnt'it??
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi teo,

welcome to Ranch
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
teo kokos wrote:... 1) I believe the values are A=10, Y=4, X=1...

You need to consider the difference between pre and post incrementing. Basically, if the increment operator is before the variable (for example, --X), then the value is incremented before being used. If the increment operator is after the variable (for example, Y++), then the value is incremented after being used.

So when you call P1(A, Y++, --X), the value for Y is passed before being incremented, and the value for X is passed after being incremented. So the values passed into the method are 10, 3, and 1.
teo kokos wrote:... 2)The class variables are the same bacause are declared static, isnt'it??

No, "static" is not the same as "final." A static variable "stays" with the class, rather than being associated with a particular instance of that class. But unless the variable is also final, its value can change.
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot understan why the Y has the value 4 when P1 is executed and why not the value (TEMP=10*2=20 Y=20*2=40) 40?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
teo kokos wrote:I cannot understan why the Y has the value 4 when P1 is executed and why not the value (TEMP=10*2=20 Y=20*2=40) 40?

This is the shadowing I talked about above...

When a parameter is passed to a method, the variable referring to it is local to that method. For example, variables A, X, and Y are all declared at the class level. However, there are also variables A, X, and Y that are local to the method P1. These local variables "shadow" the class variables.

In other words, there are 2 separate variables called Y:
  • There is a static Y associated with the class, TestApp1, and...
  • There is also a local Y within the method body of P1.


  • Add the following println statements to your code to see the difference...

    Outside of P1, 'Y' simply refers to the static variable. So the println in main uses the static Y (not the Y that's local to P1).

    Also note the order of the values passed to P1, because they are "switching" X and Y. The method is defined as P1(int A, int X, int Y); and it's called using P1(A, Y++, --X); So inside P1...
  • 10 (the value of static A) is assigned to the local variable A
  • 3 (the value of static Y before incrementing) is assigned to the local variable X (not Y)
  • 1 (the value of static X after decrementing) is assigned to the local variable Y (not X)
  •  
    teo kokos
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    WoW!!! Thank you very much marc weber!!! I am grateful!!


     
    teo kokos
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am checking again this program and there is something where i get stuck...
    I made the program like this:




    My problem is in P2 method.
    a) Why the local X and Y have the value 5?? :O
    b) Why the static Y has value 350? With this logic in P1 method the static Y should have value 160.
    Please help me because in 2 days i give exams.
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    teo kokos wrote:...My problem is in P2 method.
    a) Why the local X and Y have the value 5?? :O
    b) Why the static Y has value 350? With this logic in P1 method the static Y should have value 160...

    I'm glad to see you're working on this! It's a great way to learn.

    The method P2 has a local variable X because X is declared as a parameter: static void P2(int X) {...}. But the method P2 does not have local A or Y variables. So in the P2 method body, X refers to a local variable, but A and Y both refer to the static class variables. That means inside P2, A is the same as TestApp1.A, and Y is the same as TestApp1Y.

    Also, note that the value passed to P2 is referred to as X (the local variable). However, when P2 is called in main, it's called by passing the incremented value of the static variable Y.

    Before that method is called, Y is 4. So calling P2(++Y) increments Y to 5, and passes that value to P2, where it's assigned to the local variable X. So inside P2, X refers to the local variable X, which is 5, and Y refers to the static variable Y, which is also 5.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic