Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

doubt in some programs and question in java

 
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

what is the output of the program

2)sorting array without third variable in below code i didnt understand logic anyone tell me
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What is the output of the first program: Did you try running it? It shows that in Java, parameters are passed by value. See Pass-by-Value Please.

The second program shows a clever trick for swapping two values without using a temporary variable. Note that it's just a clever trick, and it has no practical advantage - you should never use this trick in any really useful program.
 
Marshal
Posts: 80254
428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
That is a party trick whose performance is slower than a temporary variable; a temporary variable requires three assignments, each of which takes 1 clock cycle. That trick requires three assignments and three additive operations (+ and - probably use the same part of the chip), so it will be slower in execution. If you do that in real code, it will of course it will have remarkably fast performance in terminating your contract
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jesper de Jong wrote:What is the output of the first program: Did you try running it? It shows that in Java, parameters are passed by value. See Pass-by-Value Please.


i didnt got any output,whats the output will come?i understand abt pass by value but litle bit confusion

The second program shows a clever trick for swapping two values without using a temporary variable. Note that it's just a clever trick, and it has no practical advantage - you should never use this trick in any really useful program.


but iam sorting the program but i didnt get the logic ie if i=0 in first loop after it goes to second loop k=j(that means k=0)after that a[j]>a[k] means(a[0]>a[0])then how the program will run i give the elements 5,2,4,6 both a[0] indication 5 only
 
Campbell Ritchie
Marshal
Posts: 80254
428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Even without the spelling errors, your last post is very difficult to understand. Please write in sentences with punctuation and capitalisation and spaces between all the workds.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Will Zelan wrote:i didnt got any output,whats the output will come?i understand abt pass by value but litle bit confusion


Ok, first let's make it a complete program, otherwise it's hard to know exactly what we are talking about.

You say you understand what pass by value means, so you should be able to tell what the output of this program will be. What do you think will this print and why?

For the sorting program, please provide a complete working program yourself, and if it doesn't do what you expect, explain in detail what you expected and how that's different from what the program outputs.
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
what kind of sorting program it was?
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jesper de Jong wrote:

Will Zelan wrote:i didnt got any output,whats the output will come?i understand abt pass by value but litle bit confusion


Ok, first let's make it a complete program, otherwise it's hard to know exactly what we are talking about.

You say you understand what pass by value means, so you should be able to tell what the output of this program will be. What do you think will this print and why?


iam still confused abt passby value,i dnt know whats output will come,it was interview question,so is that program corect?whats the output will come i think pqrxyz is the output,bt now iam totally confused?

For the sorting program, please provide a complete working program yourself, and if it doesn't do what you expect, explain in detail what you expected and how that's different from what the program outputs.



i give input is 5,8,2,3,7.in this program my doubt is how the loop executes in j and k ie let be explain if j=0;0<5 then it goes to next loop ie k=j,it means k=0;0<5 after that it goes to if condition a[0]>a[0] that means 5>5 so if condition is not satisfied,so where the loop goes again now,how the excution occurs and sort the order in 2,3,5,8,7
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
> That is a party trick whose performance is slower than a temporary variable;

... and in contrary to the temporary variable solution, is prone to over- or underflow with some values.
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
previously i posted doubt related to prgrm?please see that
 
Campbell Ritchie
Marshal
Posts: 80254
428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
And what do you think the output would be?
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
i dont know about first program thats why i asked here?
in second program i had doubt in looping how it executes the sorting order little bit confused any one can describe it
 
Ranch Hand
Posts: 91
IntelliJ IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Will,

Read about String Immutability and try this link might help you understand what pass by reference and pass by value is. If you do not understand, the same topic has been discussed in the Ranch before, try this link as well
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Vineeth Menon wrote:try this link



I don't recommend that article. It's not well written, and it erroneously states that objects are passed by value in Java. They are not. References are passed by value, and objects are not passed at all.

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Will Zelan wrote:

Jesper de Jong wrote:

Will Zelan wrote:i didnt got any output,whats the output will come?i understand abt pass by value but litle bit confusion


Ok, first let's make it a complete program, otherwise it's hard to know exactly what we are talking about.

You say you understand what pass by value means, so you should be able to tell what the output of this program will be. What do you think will this print and why?


iam still confused abt passby value,i dnt know whats output will come,it was interview question,so is that program corect?whats the output will come i think pqrxyz is the output,bt now iam totally confused?



So you think the output will be: pqrxyz

Why do you think that?

See: Pass-by-Value Please

When you pass a variable to a method in Java, you are not really passing the variable itself, but just the value of the variable. (A variable is a container for values - see Cup Size -- a story about variables).

First, to avoid confusion, let's rename the parameter that the update method takes:

When you call update in line 4, the value of the variable str is passed to the update method, and is assigned to the parameter variable which I've called something in the code above. When you change the variable something in line 9, it will not have any effect on the variable str in the main method. So when the update method returns, and the program continues on line 5, the variable str will still hold the value that it had before the update method was called. So the output is: pqr
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator


First, to avoid confusion, let's rename the parameter that the update method takes:

When you call update in line 4, the value of the variable str is passed to the update method, and is assigned to the parameter variable which I've called something in the code above. When you change the variable something in line 9, it will not have any effect on the variable str in the main method. So when the update method returns, and the program continues on line 5, the variable str will still hold the value that it had before the update method was called. So the output is: pqr


oh k in line 4 update(str) means which passes the variable to update method k its fine,
in that what about xyz values where it will be passed?



i give input is 5,8,2,3,7.in this program my doubt is how the loop executes in j and k ie let be explain if j=0;0<5 then it goes to next loop ie k=j,it means k=0;0<5 after that it goes to if condition a[0]>a[0] that means 5>5 so if condition is not satisfied,so where the loop goes again now,how the excution occurs and sort the order in 2,3,5,8,7
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Will Zelan wrote:what about xyz values where it will be passed?


It's not passed anywhere. When the update method returns, the variable something goes out of scope and is thrown away. The update method has a misleading name; it really does not have any effect at all.

Will Zelan wrote:so where the loop goes again now


It simply executes the next iteration of the loop over k (line 3), and if k becomes 5, it executes the next iteration of the loop over j (line 1).

Put some System.out.println() statements in your code to print the values of j and k and certain points and then run the program, then you'll see how the flow goes.
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jesper de Jong wrote:

Will Zelan wrote:what about xyz values where it will be passed?


It's not passed anywhere. When the update method returns, the variable something goes out of scope and is thrown away. The update method has a misleading name; it really does not have any effect at all.

Will Zelan wrote:so where the loop goes again now


It simply executes the next iteration of the loop over k (line 3), and if k becomes 5, it executes the next iteration of the loop over j (line 1).

Put some System.out.println() statements in your code to print the values of j and k and certain points and then run the program, then you'll see how the flow goes.


yeah thankx it helps i understand,but i need some clarification that is
in the above program if i give the input 5 4 2 3 1 then output will be 1 2 3 4 5
so here we see the loop process how it goes

here actually my doubt is what value will be print a[j] at a[0] index which one and why?1 will be printed when i check the output but how it determines to print that value
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Did you put the System.out.println() statements in?
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Will Zelan wrote:

Jesper de Jong wrote:

Will Zelan wrote:what about xyz values where it will be passed?


It's not passed anywhere. When the update method returns, the variable something goes out of scope and is thrown away. The update method has a misleading name; it really does not have any effect at all.

Will Zelan wrote:so where the loop goes again now


It simply executes the next iteration of the loop over k (line 3), and if k becomes 5, it executes the next iteration of the loop over j (line 1).

Put some System.out.println() statements in your code to print the values of j and k and certain points and then run the program, then you'll see how the flow goes.


yeah thankx it helps i understand,but i need some clarification that is
in the above program if i give the input 5 4 2 3 1 then output will be 1 2 3 4 5
so here we see the loop process how it goes

here actually my doubt is what value will be print a[j] at a[0] index which one and why?1 will be printed when i check the output but how it determines to print that value


yes i put system.out.println in j and k loops, so i understand the loops and i wrote manually above please see that there is value of a[j] gives 4 types of values are there on that how 1 will be printed
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
If you've added the println statements, compiled and ran the program and looked at the output, you would see exactly what it is doing, and when in the process it's printing 1. You just have to carefully trace the steps to see what exactly is happening.

I added a few more println statements to print the content of the array, and the output now looks like this:


Start outer loop, j = 0
Start inner loop, j = 0 and k = 0
End inner loop, j = 0 and k = 0
Start inner loop, j = 0 and k = 1
The array contains: [5, 4, 2, 3, 1]; swapping a[0] and a[1]
After swapping, the array contains: [4, 5, 2, 3, 1]
End inner loop, j = 0 and k = 1
Start inner loop, j = 0 and k = 2
The array contains: [4, 5, 2, 3, 1]; swapping a[0] and a[2]
After swapping, the array contains: [2, 5, 4, 3, 1]
End inner loop, j = 0 and k = 2
Start inner loop, j = 0 and k = 3
End inner loop, j = 0 and k = 3
Start inner loop, j = 0 and k = 4
The array contains: [2, 5, 4, 3, 1]; swapping a[0] and a[4]
After swapping, the array contains: [1, 5, 4, 3, 2]
End inner loop, j = 0 and k = 4
End outer loop, j = 0
a[0] = 1
Start outer loop, j = 1
...


Will Zelan wrote:... there is value of a[j] gives 4 types of values are there on that how 1 will be printed


What does that mean, "a[j] gives 4 types of values"?
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jesper de Jong wrote:If you've added the println statements, compiled and ran the program and looked at the output, you would see exactly what it is doing, and when in the process it's printing 1. You just have to carefully trace the steps to see what exactly is happening.

I added a few more println statements to print the content of the array, and the output now looks like this:


Start outer loop, j = 0
Start inner loop, j = 0 and k = 0
End inner loop, j = 0 and k = 0
Start inner loop, j = 0 and k = 1
The array contains: [5, 4, 2, 3, 1]; swapping a[0] and a[1]
After swapping, the array contains: [4, 5, 2, 3, 1]
End inner loop, j = 0 and k = 1
Start inner loop, j = 0 and k = 2
The array contains: [4, 5, 2, 3, 1]; swapping a[0] and a[2]
After swapping, the array contains: [2, 5, 4, 3, 1]
End inner loop, j = 0 and k = 2
Start inner loop, j = 0 and k = 3
End inner loop, j = 0 and k = 3
Start inner loop, j = 0 and k = 4
The array contains: [2, 5, 4, 3, 1]; swapping a[0] and a[4]
After swapping, the array contains: [1, 5, 4, 3, 2]
End inner loop, j = 0 and k = 4
End outer loop, j = 0
a[0] = 1
Start outer loop, j = 1
...



oh ok i understand bt how it swaps the indexes which code be used to swap

Will Zelan wrote:... there is value of a[j] gives 4 types of values are there on that how 1 will be printed


What does that mean, "a[j] gives 4 types of values"?


after going to k loop ,then it enters into if condition here like this

so a[j] shows different values as shown above ie 4,2,3,1 so how it swaps index,if i got it i understand the concept fully

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Is your question about how these three lines work?

These lines swap the values that are stored in a[j] and a[k], with a clever trick. It's a clever trick because it doesn't use a temporary variable. Note that this is interesting, but you should never use clever tricks when writing any piece of serious software - it is much more important to write code that is clear and easy to understand for other programmers than it is to be "clever" in this way.

Let's use a slightly different notation to make it clear. Suppose we have to values A and B and we want to swap them. We do:

Step 1: A* = A + B
Step 2: B* = A* - B
Step 3: A** = A* - B*

I'm using * to indicate the new value of A or B, to distinguish it from its previous value.

You can substitute the variables in the above steps, so you get:

B* = A* - B = (A + B) - B = A
A** = A* - B* = (A + B) - (A* - B) = (A + B) - ((A + B) - B) = A + B - A + B - B = B

You see that in the end the new value of B is A, and the new value of A is B - in other words, the values of the variables have been swapped.

I'll say it again, this is unnecessary cleverness that has no advantages at all, and in a real application you should just use a temporary variable:

Ivan Jozsef Balazs wrote:> That is a party trick whose performance is slower than a temporary variable;

... and in contrary to the temporary variable solution, is prone to over- or underflow with some values.


I didn't check it precisely but I think that with this trick the over- and underflow will compensate for each other, so that it still works with all values.
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

ha i got it i know how it works ,but i just done a mistake for every loop i didnt swap the values that why it made me confused now i am clearly got it thank you lot

k if we use temp variable like the above,how it will done swap,i mean temp=a[0],then a[0]=a[1] so it means a[0]will swap to a[1],and a[1]=temp it means previously temp is a[0] so it will be swaped by a[1],is it corect
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator


I didn't check it precisely but I think that with this trick the over- and underflow will compensate for each other, so that it still works with all values.



You might be right but that this issue is raised is a sign that this kludge might be good for playing around or as a conundrum but it does not belong into production code.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Will Zelan wrote:
k if we use temp variable like the above,how it will done swap,i mean temp=a[0],then a[0]=a[1] so it means a[0]will swap to a[1],and a[1]=temp it means previously temp is a[0] so it will be swaped by a[1],is it corect


I wouldn't say "will swap to" - the = operator does assignment, it doesn't swap anything.

Line 1: Assign the current value of a[j] to temp.
Line 2: Assign the current value of a[k] to a[j]. Note that a[j] now contains the same value as a[k].
Line 3: Assign the value of temp, which is the old value of a[j], to a[k].

End result: The values of a[j] and a[k] are swapped (a[j] now contains what a[k] contained first and vice versa).
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jesper de Jong wrote:

Will Zelan wrote:
k if we use temp variable like the above,how it will done swap,i mean temp=a[0],then a[0]=a[1] so it means a[0]will swap to a[1],and a[1]=temp it means previously temp is a[0] so it will be swaped by a[1],is it corect


I wouldn't say "will swap to" - the = operator does assignment, it doesn't swap anything.

Line 1: Assign the current value of a[j] to temp.
Line 2: Assign the current value of a[k] to a[j]. Note that a[j] now contains the same value as a[k].
Line 3: Assign the value of temp, which is the old value of a[j], to a[k].

End result: The values of a[j] and a[k] are swapped (a[j] now contains what a[k] contained first and vice versa).


yeah i understand,let i will tell one example is it correct?

i understand the logic the above one is corect?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Will Zelan wrote:
a[j]=a[k]-->a[0]=a[1];(which is a[1]values is equal to a[0] which means the values interchanging so 4 moved to a[0]index )



Nothing is interchanged. Saying "interchanged" implies two things switch places. All that happens here--in any assignment statement in Java*--is that the value of the expression on the RHS is copied into the variable on the LHS.


*other than possibly some automatic type conversions, such as widening an int to a long
 
Will Zelan
Ranch Hand
Posts: 56
Python Windows Vista Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jeff Verdegan wrote:

Will Zelan wrote:
a[j]=a[k]-->a[0]=a[1];(which is a[1]values is equal to a[0] which means the values interchanging so 4 moved to a[0]index )



Nothing is interchanged. Saying "interchanged" implies two things switch places. All that happens here--in any assignment statement in Java*--is that the value of the expression on the RHS is copied into the variable on the LHS.


*other than possibly some automatic type conversions, such as widening an int to a long


k above example is my logic is correct or not?it means a[j] value is copied to a[k]and a[k]value is copied to a[j] so is it right
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Will Zelan wrote:

Jeff Verdegan wrote:

Will Zelan wrote:
a[j]=a[k]-->a[0]=a[1];(which is a[1]values is equal to a[0] which means the values interchanging so 4 moved to a[0]index )



Nothing is interchanged. Saying "interchanged" implies two things switch places. All that happens here--in any assignment statement in Java*--is that the value of the expression on the RHS is copied into the variable on the LHS.


*other than possibly some automatic type conversions, such as widening an int to a long


k above example is my logic is correct or not?it means a[j] value is copied to a[k]and a[k]value is copied to a[j] so is it right



Read what I wrote above. I very clearly said NO. In that line of code right there, the one quoted in this post, which I will now repeat:


In that code, the ONLY thing that happens is that the value of a[k] is copied into a[j]. So now a[j] and a[k] both hold the same value.

Now, if you're talking about the code further up the thread:


Then the answer is:
  • The first line copies the value from a[j] into temp, which means temp and a[j] now have the value--a[j]'s original value.
  • The second line copies the value from a[k] into a[j]. So now a[j] and a[k] both have the same value--a[k]'s original value.
  • The third line copies the value from a[k] into temp. So now a[k] holds the same value as temp (a[j]'s original value( and a[j] still holds a[k]'s original value, which it got assigned in the previous step.


  • You have a green cup with tea in it, and a yellow cup with beer in it. You want to switch the tea and the beer. So you get a blue cup. You pour the beer out of the yellow cup into the blue. Pour the tead out of the green into the yellow. Pour the beer out of the blue into the green. It's the same thing, except here we're moving a physical thing rather than copying a logical one.
     
    Will Zelan
    Ranch Hand
    Posts: 56
    Python Windows Vista Flex
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    The third line copies the value from a[k] into temp. So now a[k] holds the same value as temp (a[j]'s original value( and a[j] still holds a[k]'s original value, which it got assigned in the previous step.


    in that line copies values from temp into a[k] or a[k] into temp?
    let assume it is input 5 4 2


    again anything wrong inform me
     
    Jesper de Jong
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Suppose that a[0] = 5, a[1] = 4, a[2] = 2.

    Line 1: Copy the value of a[0] to temp, so now we have: temp = 5, a[0] = 5, a[1] = 4, a[2] = 2.
    Line 2: Copy the value of a[1] to a[0], so now we have: temp = 5, a[0] = 4, a[1] = 4, a[2] = 2.
    Line 3: Copy the value of temp to a[1], so now we have: temp = 5, a[0] = 4, a[1] = 5, a[2] = 2.

    End result: a[0] = 4, a[1] = 5, a[2] = 2.

    It's simple. Don't make it harder than it is.
     
    Will Zelan
    Ranch Hand
    Posts: 56
    Python Windows Vista Flex
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Jesper de Jong wrote:Suppose that a[0] = 5, a[1] = 4, a[2] = 2.

    Line 1: Copy the value of a[0] to temp, so now we have: temp = 5, a[0] = 5, a[1] = 4, a[2] = 2.
    Line 2: Copy the value of a[1] to a[0], so now we have: temp = 5, a[0] = 4, a[1] = 4, a[2] = 2.
    Line 3: Copy the value of temp to a[1], so now we have: temp = 5, a[0] = 4, a[1] = 5, a[2] = 2.

    End result: a[0] = 4, a[1] = 5, a[2] = 2.

    It's simple. Don't make it harder than it is.


    oh thanks totally i got it,by using temp variable we can copy the values one to another is it corect
    i had doubt in the below program

    i had doubt in 14 and 22 nd line,i know it is the method which do some functions,in 14th line what are passing and 22nd line what is inta[] and int n can you tell me
     
    Campbell Ritchie
    Marshal
    Posts: 80254
    428
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    That is the standard way of swapping values in a language with does not support multiple assignments. Very few languages support multiple assignments.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Will Zelan wrote:

    The third line copies the value from a[k] into temp. So now a[k] holds the same value as temp (a[j]'s original value( and a[j] still holds a[k]'s original value, which it got assigned in the previous step.


    in that line copies values from temp into a[k] or a[k] into temp?



    Oops, sorry. Yes, I had it backward. From temp into a[k].
     
    Jesper de Jong
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Will Zelan wrote:i had doubt in 14 and 22 nd line,i know it is the method which do some functions,in 14th line what are passing and 22nd line what is inta[] and int n can you tell me


    What do you think the answer to that question is yourself?
     
    Will Zelan
    Ranch Hand
    Posts: 56
    Python Windows Vista Flex
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Jesper de Jong wrote:

    Will Zelan wrote:i had doubt in 14 and 22 nd line,i know it is the method which do some functions,in 14th line what are passing and 22nd line what is inta[] and int n can you tell me


    What do you think the answer to that question is yourself?


    passing the parameter as array and array length in 14th line,will you tell me
     
    Jesper de Jong
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator

    Will Zelan wrote:

    Jesper de Jong wrote:

    Will Zelan wrote:i had doubt in 14 and 22 nd line,i know it is the method which do some functions,in 14th line what are passing and 22nd line what is inta[] and int n can you tell me


    What do you think the answer to that question is yourself?


    passing the parameter as array and array length in 14th line,will you tell me


    Exactly, it's just a method call like any other method call, where parameters are passed. Nothing special going on.
     
    Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
      Bookmark Topic Watch Topic
    • New Topic