I'm writing 2 Vectors:
PVector a = new PVector (5,6);
PVector b = a;
b.add(new PVector (2,2));
print(a);
Why does a = (7,8)?
I didn't change a, I changed b.
PVector b = a;
It didn't actually created new object of PVector, but made a reference to the vector a.
Therefore you have 2 references to the one place in the memory where is PVector(5,6) and when you add sth using one reference it will surely impact on the other one.
Hope it helps :)
Related
Here is a Python-like pattern I need to re-create in Chapel.
class Gambler {
var luckyNumbers: [1..0] int;
}
var nums = [13,17,23,71];
var KennyRogers = new Gambler();
KennyRogers.luckyNumbers = for n in nums do n;
writeln(KennyRogers);
Produces the run-time error
Kenny.chpl:8: error: zippered iterations have non-equal lengths
I don't know how many lucky numbers Kenny will have in advance and I can't instantiate Kenny at that time. That is, I have to assign them later. Also, I need to know when to hold them, know when to fold them.
This is a good application of the array.push_back method. To insert lucky numbers one at a time you can do:
for n in nums do
KennyRogers.luckyNumbers.push_back(n);
You can also insert the whole array in a single push_back operation:
KennyRogers.luckyNumbers.push_back(nums);
There are also push_front and insert methods in case you need to put elements at the front or at arbitrary positions in the array.
I don't think I can help on when to hold them or when to fold them.
A way to approach this that simply makes things the right size from the start and avoids resizing/rewriting the array is to establish luckyNumbers in the initializer for Gambler. In order to do this without resizing, you'll need to declare the array's domain and set it in the initializer as well:
class Gambler {
const D: domain(1); // a 1D domain field representing the array's size
var luckyNumbers: [D] int; // declare lucky numbers in terms of that domain
proc init(nums: [?numsD] int) {
D = numsD; // make D a copy of nums's domain; allocates luckyNumbers to the appropriate size
luckyNumbers = nums; // initialize luckyNumbers with nums
super.init(); // mark the initialization of fields as being done
}
}
var nums = [13,17,23,71];
var KennyRogers = new Gambler(nums);
writeln(KennyRogers);
So, I am dealing with trees whose compareTo() methods will often evaluate to the same thing, but I need new trees to be inserted before the existing nodes in the PriorityQueue. Right now, it seems that the java implementation of PriorityQueue places new node in random position to similar nodes.
while (pq.size() >= 2) {
System.out.println("Iteration: " + i++);
printPQ();
BinaryTree b1 = pq.remove();
BinaryTree b2 = pq.remove();
BinaryTree newTree = new BinaryTree(b1, b2);
//add the newly created tree back into pq
pq.add(newTree);
System.out.println();
}
For those wondering, this is for a Huffman Code implementation.
You could modify the compareTo(...) method. If you would add the creation time as property to your BinaryTree, this can than be included to the comparism and ensure the order of newly added elements in the PriorityQueue.
Me and my friend are having some trouble in regards to understanding Static and Dynamic scoping. I believe with dynamic, the variable (global) will keep being updated by other functions until printed, whereas with static I think that whatever value get's assigned to a variable first stays that way.
Is this thinking correct or no?
For an example using my thoughts above I have calculated the following from this code snippet.
int a, b, c;
void p() {
int a = 3;
b = 1;
c = a + b;
q();
}
void print() { printf(ā%d %d %d\nā, a, b, c); }
void q() {
int b = 4;
a = 5;
c = a + b;
print();
}
main() {
int c = 5;
p();
}
Output with static scoping: 315
Output with dynamic scoping: 549
With static scoping, print would fail because neither a, b, nor c are assigned values either inside print or at the scope where print is defined (namely, the first line of the file).
With dynamic scoping, the output would be 549, since each of a, b, and c has a value assigned in q. Not demonstrated by your code is also the fact that after q returns from its call inside p, the local variable a has the value 5 set in q, not the global variable. Namely, the following occurs:
Global variables a, b, and c are declared, but do not have values. Let's assume your language initializes such values to 0.
main is called. A variable c local to main is given the value 5; global c still equals 0.
p is called. A p-local variable a is assigned the value 3; global a is still 0.
No local variable b exists in p or its caller, main, so the global b is set to 1.
No local variable c exists in p, but one does in c, to its value is set to 3 + 1 = 4.
q is called. A local b is declared and set to 4, leaving global b set to 0.
No local variable a exists in q, but one does in its caller p, so that value changes from 3 to 5.
No local variable c exists in q or its caller p, but does in p's caller main, so that value is set to 5 + 4 = 9. Global c is still 0.
print is called, and lacking any local a, b, or c, it looks back in its call chain. It uses a from p, b from q, and c from main (none of the globals are used.
q returns. In p, the values of a and c are still 5 and 9 as set in q. b is still 1, since q declared a local b.
p returns. In main, we still have a=0 (since p declared its own copy before calling q), b=1 (since p modified the global b), and c=9 (since q ultimately modified the variable local to c).
main returns. We still have global a=0, b=1, and c=0.
If that's confusing (and I didn't confuse myself and make any mistakes), you might understand why most languages use static scoping: it's not only much easier, but possible, to reason about the behavior of the program without having to run or simulate it just to track variable assignments.
static scoping- 5 1 9// it takes global values as variables not defined within print function
dynamic scoping- 5 4 9
So I have this problem where I have to figure out the output using two different scoping rules. I know the output using lexical scoping is a=3 and b=1, but I am having hard time figure out the output using dynamic scoping.
Note:the code example that follows uses C syntax, but let's just treat it as pseudo-code.
int a,b;
int p() {
int a, p;
a = 0; b = 1; p = 2;
return p;
}
void print() {
printf("%d\n%d\n",a,b);
}
void q () {
int b;
a = 3; b = 4;
print();
}
main() {
a = p();
q();
}
Here is what I come up with.
Using Dynamic scoping, the nonlocal references to a and b can change. So I have a=2 ( return from p() ), then b=4 ( inside q() ).
So the output is 2 4?
As we know, C doesn't have dynamic scoping, but assuming it did, the program would print 3 4.
In main, a and b are the global ones. a will be set to 2, as we will see that this is what p will return.
In p, called from main, b is still the global one, but a is the one local in p. The local a is set to 0, but will soon disappear. The global b is set to 1. The local p is set to 2, and 2 will be returned. Now the global b is 1.
In q, called from main, a is the global one, but b is the one local in q. Here the global a is set to 3, and the local b is set to 4.
In print, called from q, a is the global one (which has the value 3), and b is the one local in q (which has the value 4).
It is in this last step, inside the function print, that we see a difference from static scoping. With static scoping a and b would be the global ones. With dynamic scoping, we have to look at the chain of calling functions, and in q we find a variable b, which will be the b used inside print.
C is not a dynamically scoped language. If you want to experiment in order to understand the difference, you're better off with a language like Perl which lets you chose between both.
I have two statements:
String aStr = new String("ABC");
String bStr = "ABC";
I read in book that in first statement JVM creates two bjects and one reference variable, whereas second statement creates one reference variable and one object.
How is that? When I say new String("ABC") then It's pretty clear that object is created.
Now my question is that for "ABC" value to we do create another object?
Please clarify a bit more here.
Thank you
You will end up with two Strings.
1) the literal "ABC", used to construct aStr and assigned to bStr. The compiler makes sure that this is the same single instance.
2) a newly constructed String aStr (because you forced it to be new'ed, which is really pretty much non-sensical)
Using a string literal will only create a single object for the lifetime of the JVM - or possibly the classloader. (I can't remember the exact details, but it's almost never important.)
That means it's hard to say that the second statement in your code sample really "creates" an object - a certain object has to be present, but if you run the same code in a loop 100 times, it won't create any more objects... whereas the first statement would. (It would require that the object referred to by the "ABC" literal is present and create a new instance on each iteration, by virtue of calling the constructor.)
In particular, if you have:
Object x = "ABC";
Object y = "ABC";
then it's guaranteed (by the language specification) than x and y will refer to the same object. This extends to other constant expressions equal to the same string too:
private static final String A = "a";
Object z = A + "BC"; // x, y and z are still the same reference...
The only time I ever use the String(String) constructor is if I've got a string which may well be backed by a rather larger character array which I don't otherwise need:
String x = readSomeVeryLargeString();
String y = x.substring(5, 10);
String z = new String(y); // Copies the contents
Now if the strings that y and x refer to are eligible for collection but the string that z refers to isn't (e.g. it's passed on to other methods etc) then we don't end up holding all of the original long string in memory, which we would otherwise.