It's annoying to have the framework of what I want to make 90% done, but with a few little things that keep me from building on it.
My little BBS script:
1. Creates new threads and makes a first post for the thread
2. Displays the list of threads
3. Loads clicked threads
But my PostInTopic class is being a silly goose. Entries show if I add them manually, but not when posted with the form. It's probably simple, but I'm too tired to figure out the exact cause. But I did make loads of progress on it. :rotate:
And I have a question for people with competency in DB design. Should I be tossing all my posts into one table (like they are now) with the associated thread's ID in each post (row), or should I be creating some new container for each thread's posts?
edit: Yay, solved the post issue. There was a mysterious if block on the post code that always returned false, so the post code never ran.
I love those. Why is this even here? What was I thinking? ... well, of course it works NOW.
It's annoying to have the framework of what I want to make 90% done, but with a few little things that keep me from building on it.
My little BBS script:
1. Creates new threads and makes a first post for the thread
2. Displays the list of threads
3. Loads clicked threads
But my PostInTopic class is being a silly goose. Entries show if I add them manually, but not when posted with the form. It's probably simple, but I'm too tired to figure out the exact cause. But I did make loads of progress on it. :rotate:
And I have a question for people with competency in DB design. Should I be tossing all my posts into one table (like they are now) with the associated thread's ID in each post (row), or should I be creating some new container for each thread's posts?
edit: Yay, solved the post issue. There was a mysterious if block on the post code that always returned false, so the post code never ran.
I love those. Why is this even here? What was I thinking? ... well, of course it works NOW.
I think it was an artifact from the original sample code. It was supposed to check to see if you were logged in, and if not set the user to anonymous. I just forgot to take the if part out.
So studying for my IT Design midterm, I ran into something I'm not sure what's going on. I normally do everything in one file, which isn't very robust. If you change one thing you have to change a lot of the files.
So we're learning about separating the application from the GUI.
This is the code I'm reading:
//*********************************************
// main - creates the application and the GUI.
//*********************************************
public static void main(String args[])
{
Comparer2 tester = new Comparer2();
CompareGUI2 myGUI = new CompareGUI2(tester);
tester.setGUI(myGUI);
}//main
//*********************************************
// setGUI - sets the pointer to call the GUI.
//*********************************************
public void setGUI(CompareGUI2 g)
{
gui = g;
}
I don't know why you would need a separate function for that. Why couldn't you just do that in the main?
Do something like: myGUI = gui;
While main is a member function of Comparer2, it's a static member function, which means that it can't access any non-static members of Comparer2. You don't necessarily need the setGUI function; if gui was a public member you could assign it directly within main, but you tend to make data members private and use set/get functions unless you're dealing with a simple data structure.
(As a side note, separating the application and the GUI only to couple them tightly - by having them maintain references to each-other - is not necessarily a good thing)
On a related note, I am currently working on my Software Engineering homework, and he says something about a "loop invariant" and how that should always be true. I have never once in the history of programming heard about this invariant. What is it exactly? Do I create a method that checks if something is true, and if it is it will continue the loop?
The hell is the point of that with conditional statements?
public class DutchNationalFlag
{
public DutchNationalFlag(int [] nums)
{
swap(nums[0],nums[1]);
}
public void swap(int left, int right)
{
int temp = left;
left = right;
right = temp;
}
public static void main(String [] args)
{
int [] n = {1,2,3};
DutchNationalFlag DNF = new DutchNationalFlag(n);
System.out.println(n[0]);
System.out.println(n[1]);
System.out.println(n[2]);
}
}
So I realize the above code doesn't do much of anything but I can't seem to figure out how to get some sort of output? The code compiles fine and when I run it it doesn't actually display anything (in spite of having the System.out's). What's the deal?
I know it's going to be something incredibly noobish but I'm tired of staring at it.
a) Where are you running it? Eclipse? Or on the command line, or what?
b) Your swap won't work. You're just passing the values of nums[0] and nums[1] to it.
End on
I wish that someway, somehow, that I could save every one of us
a) Where are you running it? Eclipse? Or on the command line, or what?
b) Your swap won't work. You're just passing the values of nums[0] and nums[1] to it.
I think that can be fixed by changing the function to
public void swap(int &left, int &right)
{
int temp = left;
left = right;
right = temp;
}
Seems to be working, other than the fact that I am having trouble actually getting the output to a command prompt window instead of at the bottom of netbeans. Does that make sense?
Seems to be working, other than the fact that I am having trouble actually getting the output to a command prompt window instead of at the bottom of netbeans. Does that make sense?
It probably uses its own runtime and maps all output to its debug window.
Yeah that's what it looks like. I swear I've seen it output to the cmd prompt but that could be C++...
How would you suggest I go about swapping with Java? I need to be able to swap two numbers from an array. My friend said make the methods static but that didn't do anything.
Yeah that's what it looks like. I swear I've seen it output to the cmd prompt but that could be C++...
How would you suggest I go about swapping with Java? I need to be able to swap two numbers from an array. My friend said make the methods static but that didn't do anything.
You can't modify the arguments directly, but you can manipulate their contents.
So you could do swapIndex(int[] array, int a, int b) and act on indices a and b.
But you can't do a "swap my parameters" in Java since it's pass by value.
Yeah that's what it looks like. I swear I've seen it output to the cmd prompt but that could be C++...
How would you suggest I go about swapping with Java? I need to be able to swap two numbers from an array. My friend said make the methods static but that didn't do anything.
You can't modify the arguments directly, but you can manipulate their contents.
So you could do swapIndex(int[] array, int a, int b) and act on indices a and b.
But you can't do a "swap my parameters" in Java since it's pass by value.
Yeah that's what it looks like. I swear I've seen it output to the cmd prompt but that could be C++...
How would you suggest I go about swapping with Java? I need to be able to swap two numbers from an array. My friend said make the methods static but that didn't do anything.
You can't modify the arguments directly, but you can manipulate their contents.
So you could do swapIndex(int[] array, int a, int b) and act on indices a and b.
But you can't do a "swap my parameters" in Java since it's pass by value.
it is pass by value on primitives.
No, it passes everything by value.
static public foo(Bar var1, Bar var2)
{
Bar temp = var1;
var1 = var2;
var2 = temp;
}
Try making that work with your object "references."
They're actually pointers, and the pointers are being passed by value. You can just use them to access the objects, not the variables themselves that were passed by the caller.
Yes, someone else who understands that Java is all pass-by-value. All object variables in Java are pointers, and these pointers are passed by value to functions.
I hear the "objects are pass by reference" thing so much, it's infuriating.
I think it's an importation of thinking from the C world. If you're passing pointers, you're passing by reference, with the important distinction that you're not passing a copy of the object, you're passing a reference to it. This is helpful in getting newbies to understand the distinction of passing primitives versus passing objects (i.e., why you have to write swap() with objects). Or, as Wikipedia puts it:
The term "call-by-value" is sometimes problematic, as the value implied is not the value of the variable as understood by the ordinary meaning of value, but an implementation-specific reference to the value. The term "call-by-value where the value is a reference" is common (but should not be understood as being call-by-reference). Thus the behaviour of call-by-value Java or Visual Basic and call-by-value C or Pascal are significantly different: in C or Pascal, calling a function with a large structure as an argument will cause the entire structure to be copied, potentially causing serious performance degradation, and mutations to the structure are invisible to the caller. However, in Java or Visual Basic only the reference to the structure is copied, which is fast, and mutations to the structure are visible to the caller. (See also call-by-sharing....)
Yes, someone else who understands that Java is all pass-by-value. All object variables in Java are pointers, and these pointers are passed by value to functions.
I hear the "objects are pass by reference" thing so much, it's infuriating.
It technically is pass by reference because what you're passing is a pointer to the data contained in thel object, and then dereferencing it in the scope of the method. I suppose you could view an object in Java as the value of a reference, but to me that just means it's a reference. Java just hides the pointer stuff from you so that you don't have to worry about dereferencing the pointer.
When you say Object obj = new Object();, obj is a pointer to the Object you just created. When you pass obj as a function parameter, you are passing the reference. When you use obj, Java automatically handles the dereferencing part so that you can access the Object it points to.
Primitive types are a special case Java in that they actually are values instead of references. When you pass a primitive as a function parameter, it is basically copying the value of the primitive into the scope of the function. So you can't do something like this:
public void swap(int a, int b){
int temp = a;
a=b;
b=a;
}
This is because a and b are now local to the method scope. Any modification you make to them will only be in that scope.
The wrapper objects for primitive types (Integer, Float, etc...) are references, but you can't modify the values they wrap, so you couldn't do the swap with them either. But you could define your own type like this:
public class MyInteger{
public int wrappedInt; //you could make this private and provide get/set methods.
public MyInteger(int wrappedInt){
this.wrappedInt = wrappedInt;
}
}
public void swap(MyInteger a, MyInteger b){
int temp = a.wrappedInt;
a.wrappedInt=b.wrappedInt;
b.wrappedInt=temp;
}
Trying to fix a FUBARd PDF that is supposed to be multiple documents but comes through as a single document through some wonky, outsourced, British (could be Indian for all I know) interface.
So the PDF that we're getting is actually multiple PDF documents that somehow got CATed together in their software through a batch. Okay, whatever, pretty trivial to fix but damn if it wasn't a waste of half of my day writing a byte processor for PDFs.
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
So... I just found out the homework I've been working on for a few days... Is only worth 2.5% of my entire final grade. And none of this homework will be on the final.
And I get what it actually means. But I don't know exactly what I'm supposed to be doing to check the invariant. Is it an actual condition I MAKE, or is it a variable?
And I get what it actually means. But I don't know exactly what I'm supposed to be doing to check the invariant. Is it an actual condition I MAKE, or is it a variable?
I think that's just something used for algorithm design. Are you specifically being told to check it?
And I get what it actually means. But I don't know exactly what I'm supposed to be doing to check the invariant. Is it an actual condition I MAKE, or is it a variable?
I think that's just something used for algorithm design. Are you specifically being told to check it?
This is what I read:
2.) The loop invariant.
a. As was said in class, this is the condition that is guaranteed to be met both before and after the execution of a loop. This could refer to the numbered indexes that divide the red, white, unknown, and blue sections in the array.
I will understand if it's part of the design, but I'm not sure if that's what it means.
There is no pass-by-reference in Java, just like there isn't in C. C++ has pass-by-reference.
People should check this out it explains it in depth if you don't know what pass-by-value and pass-by-reference actually are.
I've always preferred saying that there is no value type in java. Because the main "gotcha" is that you can not pass an object by value in java. You can pass its reference by value all you want, but not it as a value. (Granted. . . With some of the fancier escape analysis going on, this is not strictly true anymore. Supposedly some objects will be allocated on the stack instead of the heap, which makes them fit the traditional "pass by value" semantics.)
And I get what it actually means. But I don't know exactly what I'm supposed to be doing to check the invariant. Is it an actual condition I MAKE, or is it a variable?
I think that's just something used for algorithm design. Are you specifically being told to check it?
This is what I read:
2.) The loop invariant.
a. As was said in class, this is the condition that is guaranteed to be met both before and after the execution of a loop. This could refer to the numbered indexes that divide the red, white, unknown, and blue sections in the array.
I will understand if it's part of the design, but I'm not sure if that's what it means.
What are you building? Finding invariants is often a great way to pull out instructions from a loop, to make it "tighter."
There is no pass-by-reference in Java, just like there isn't in C. C++ has pass-by-reference.
People should check this out it explains it in depth if you don't know what pass-by-value and pass-by-reference actually are.
I've always preferred saying that there is no value type in java. Because the main "gotcha" is that you can not pass an object by value in java. You can pass its reference by value all you want, but not it as a value. (Granted. . . With some of the fancier escape analysis going on, this is not strictly true anymore. Supposedly some objects will be allocated on the stack instead of the heap, which makes them fit the traditional "pass by value" semantics.)
See the problem is that Sun renamed pointers to references, just to fuck with us all.
There is no pass-by-reference in Java, just like there isn't in C. C++ has pass-by-reference.
People should check this out it explains it in depth if you don't know what pass-by-value and pass-by-reference actually are.
I've always preferred saying that there is no value type in java. Because the main "gotcha" is that you can not pass an object by value in java. You can pass its reference by value all you want, but not it as a value. (Granted. . . With some of the fancier escape analysis going on, this is not strictly true anymore. Supposedly some objects will be allocated on the stack instead of the heap, which makes them fit the traditional "pass by value" semantics.)
See the problem is that Sun renamed pointers to references, just to fuck with us all.
There is never pass-by-reference in Java.
Depends how you mean it. If you simply mean that there is no pass by pointer, then you are wrong. All non-primitives are pass by pointer. What there is not, is no builtin "pointer to a pointer." Just as there is no pointer manipulation.
Edit: I should add to this. There is also no such thing as a stack allocated object. Which means no object can reference something on the stack, which means you can not promote something on the stack to being on the heap. If you want it on the heap --- to pass it by reference, for example ---, you have to declare it that way at the start.
There is no pass-by-reference in Java, just like there isn't in C. C++ has pass-by-reference.
People should check this out it explains it in depth if you don't know what pass-by-value and pass-by-reference actually are.
I've always preferred saying that there is no value type in java. Because the main "gotcha" is that you can not pass an object by value in java. You can pass its reference by value all you want, but not it as a value. (Granted. . . With some of the fancier escape analysis going on, this is not strictly true anymore. Supposedly some objects will be allocated on the stack instead of the heap, which makes them fit the traditional "pass by value" semantics.)
See the problem is that Sun renamed pointers to references, just to fuck with us all.
There is never pass-by-reference in Java.
Depends how you mean it. If you simply mean that there is no pass by pointer, then you are wrong. All non-primitives are pass by pointer. What there is not, is no builtin "pointer to a pointer." Just as there is no pointer manipulation.
No it doesn't depend, you pass those pointers by-value. By-value and by-reference are ways to pass parameters on a call, nothing more.
So, it's been a while since I've really delved into Java.
What infidel is saying is, since Java is a by reference system that when you pass variables you're technically passing them by value (value of the reference) rather than actually passing pointers back and forth like you would in C++.
What taeric is saying is that there's no such thing as "value" references in the grand scheme of thing because a reference is a pointer and semantically you're doing roughly the same thing just without the pointer syntax.
So, tl;dr : you guys are pretty much saying the same thing?
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
There is no pass-by-reference in Java, just like there isn't in C. C++ has pass-by-reference.
People should check this out it explains it in depth if you don't know what pass-by-value and pass-by-reference actually are.
I've always preferred saying that there is no value type in java. Because the main "gotcha" is that you can not pass an object by value in java. You can pass its reference by value all you want, but not it as a value. (Granted. . . With some of the fancier escape analysis going on, this is not strictly true anymore. Supposedly some objects will be allocated on the stack instead of the heap, which makes them fit the traditional "pass by value" semantics.)
See the problem is that Sun renamed pointers to references, just to fuck with us all.
There is never pass-by-reference in Java.
Depends how you mean it. If you simply mean that there is no pass by pointer, then you are wrong. All non-primitives are pass by pointer. What there is not, is no builtin "pointer to a pointer." Just as there is no pointer manipulation.
No it doesn't depend, you pass those pointers by-value. By-value and by-reference are ways to pass parameters on a call, nothing more.
I strictly meant when you said "Sun renamed pointers to references." If you mean that to be that there is no "pass by pointer" then I take exception. I believe, though, that you meant this caused unnecessary confusion with "pass by reference." And that, I can only agree with whole heartedly.
There is no pass-by-reference in Java, just like there isn't in C. C++ has pass-by-reference.
People should check this out it explains it in depth if you don't know what pass-by-value and pass-by-reference actually are.
That was an enlightening link. Thanks for that. The main point he is making is that references and pointers are different beasts. I guess I always thought of them as the same thing.
When you declare Object b;, you are creating a pointer to an Object. And when you pass b into a method/function, it's actually creating a new pointer on the stack (for the duration of the method scope) that points to the same Object that b was pointing to outside of the method scope.
So you are right, there is technically no pass-by-reference, because that would imply that the actual pointer b is being passed into the method when it's actually a new pointer that points to the same object.
Still, I think most people, when they think about pass-by-value, don't think about the fact that the value is really a pointer to something else.
Yeah, there is "passing A (java) reference" and "passing BY reference" and the distinction is very important.
References are basically auto-pointers, since they're not strictly pointers (a memory address and that's that) but a reference id so that Java can keep track of objects and refcounts and garbage collection etc. It's still functionally equivalent to pointers in practice though, it's just not something you can treat as raw memory.
Posts
I love those. Why is this even here? What was I thinking? ... well, of course it works NOW.
I think it was an artifact from the original sample code. It was supposed to check to see if you were logged in, and if not set the user to anonymous. I just forgot to take the if part out.
(As a side note, separating the application and the GUI only to couple them tightly - by having them maintain references to each-other - is not necessarily a good thing)
On a related note, I am currently working on my Software Engineering homework, and he says something about a "loop invariant" and how that should always be true. I have never once in the history of programming heard about this invariant. What is it exactly? Do I create a method that checks if something is true, and if it is it will continue the loop?
The hell is the point of that with conditional statements?
public class DutchNationalFlag { public DutchNationalFlag(int [] nums) { swap(nums[0],nums[1]); } public void swap(int left, int right) { int temp = left; left = right; right = temp; } public static void main(String [] args) { int [] n = {1,2,3}; DutchNationalFlag DNF = new DutchNationalFlag(n); System.out.println(n[0]); System.out.println(n[1]); System.out.println(n[2]); } }So I realize the above code doesn't do much of anything but I can't seem to figure out how to get some sort of output? The code compiles fine and when I run it it doesn't actually display anything (in spite of having the System.out's). What's the deal?
I know it's going to be something incredibly noobish but I'm tired of staring at it.
b) Your swap won't work. You're just passing the values of nums[0] and nums[1] to it.
I think that can be fixed by changing the function to
public void swap(int &left, int &right) { int temp = left; left = right; right = temp; }And I forgot to pass by reference
It probably uses its own runtime and maps all output to its debug window.
How would you suggest I go about swapping with Java? I need to be able to swap two numbers from an array. My friend said make the methods static but that didn't do anything.
String firstWord = "You're";
String secondWord = "Stupid!";
System.out.println(firstWord + " " + secondWord);
...
Man that was way too much work for a stupid joke. :P
You can't modify the arguments directly, but you can manipulate their contents.
So you could do swapIndex(int[] array, int a, int b) and act on indices a and b.
But you can't do a "swap my parameters" in Java since it's pass by value.
it is pass by value on primitives.
No, it passes everything by value.
static public foo(Bar var1, Bar var2) { Bar temp = var1; var1 = var2; var2 = temp; }Try making that work with your object "references."
They're actually pointers, and the pointers are being passed by value. You can just use them to access the objects, not the variables themselves that were passed by the caller.
I hear the "objects are pass by reference" thing so much, it's infuriating.
SE++ Forum Battle Archive
Thank you for correcting me, it seems I have been in C land to long.
It technically is pass by reference because what you're passing is a pointer to the data contained in thel object, and then dereferencing it in the scope of the method. I suppose you could view an object in Java as the value of a reference, but to me that just means it's a reference. Java just hides the pointer stuff from you so that you don't have to worry about dereferencing the pointer.
When you say Object obj = new Object();, obj is a pointer to the Object you just created. When you pass obj as a function parameter, you are passing the reference. When you use obj, Java automatically handles the dereferencing part so that you can access the Object it points to.
Primitive types are a special case Java in that they actually are values instead of references. When you pass a primitive as a function parameter, it is basically copying the value of the primitive into the scope of the function. So you can't do something like this:
public void swap(int a, int b){ int temp = a; a=b; b=a; }This is because a and b are now local to the method scope. Any modification you make to them will only be in that scope.The wrapper objects for primitive types (Integer, Float, etc...) are references, but you can't modify the values they wrap, so you couldn't do the swap with them either. But you could define your own type like this:
public class MyInteger{ public int wrappedInt; //you could make this private and provide get/set methods. public MyInteger(int wrappedInt){ this.wrappedInt = wrappedInt; } } public void swap(MyInteger a, MyInteger b){ int temp = a.wrappedInt; a.wrappedInt=b.wrappedInt; b.wrappedInt=temp; }Trying to fix a FUBARd PDF that is supposed to be multiple documents but comes through as a single document through some wonky, outsourced, British (could be Indian for all I know) interface.
So the PDF that we're getting is actually multiple PDF documents that somehow got CATed together in their software through a batch. Okay, whatever, pretty trivial to fix but damn if it wasn't a waste of half of my day writing a byte processor for PDFs.
Yeahhhh motivation to get it done? Next to none.
http://www.cse.yorku.ca/~franck/teaching/2001-02/2011S/1505.html
And I get what it actually means. But I don't know exactly what I'm supposed to be doing to check the invariant. Is it an actual condition I MAKE, or is it a variable?
I think that's just something used for algorithm design. Are you specifically being told to check it?
This is what I read:
I will understand if it's part of the design, but I'm not sure if that's what it means.
People should check this out it explains it in depth if you don't know what pass-by-value and pass-by-reference actually are.
I've always preferred saying that there is no value type in java. Because the main "gotcha" is that you can not pass an object by value in java. You can pass its reference by value all you want, but not it as a value. (Granted. . . With some of the fancier escape analysis going on, this is not strictly true anymore. Supposedly some objects will be allocated on the stack instead of the heap, which makes them fit the traditional "pass by value" semantics.)
What are you building? Finding invariants is often a great way to pull out instructions from a loop, to make it "tighter."
See the problem is that Sun renamed pointers to references, just to fuck with us all.
There is never pass-by-reference in Java.
Depends how you mean it.
Edit: I should add to this. There is also no such thing as a stack allocated object. Which means no object can reference something on the stack, which means you can not promote something on the stack to being on the heap. If you want it on the heap --- to pass it by reference, for example ---, you have to declare it that way at the start.
No it doesn't depend, you pass those pointers by-value. By-value and by-reference are ways to pass parameters on a call, nothing more.
What infidel is saying is, since Java is a by reference system that when you pass variables you're technically passing them by value (value of the reference) rather than actually passing pointers back and forth like you would in C++.
What taeric is saying is that there's no such thing as "value" references in the grand scheme of thing because a reference is a pointer and semantically you're doing roughly the same thing just without the pointer syntax.
So, tl;dr : you guys are pretty much saying the same thing?
I strictly meant when you said "Sun renamed pointers to references." If you mean that to be that there is no "pass by pointer" then I take exception. I believe, though, that you meant this caused unnecessary confusion with "pass by reference." And that, I can only agree with whole heartedly.
People should just read the article. Or the others like it.
edit: The confusion on calling them "references" yeah, that's the main reason why people have trouble with this.
That was an enlightening link. Thanks for that. The main point he is making is that references and pointers are different beasts. I guess I always thought of them as the same thing.
When you declare Object b;, you are creating a pointer to an Object. And when you pass b into a method/function, it's actually creating a new pointer on the stack (for the duration of the method scope) that points to the same Object that b was pointing to outside of the method scope.
So you are right, there is technically no pass-by-reference, because that would imply that the actual pointer b is being passed into the method when it's actually a new pointer that points to the same object.
Still, I think most people, when they think about pass-by-value, don't think about the fact that the value is really a pointer to something else.
References are basically auto-pointers, since they're not strictly pointers (a memory address and that's that) but a reference id so that Java can keep track of objects and refcounts and garbage collection etc. It's still functionally equivalent to pointers in practice though, it's just not something you can treat as raw memory.