|
This, this right here, is why schools that don't teach C/C++/[pointers] any more are causing Problems
|
# ? Apr 18, 2008 22:26 |
|
|
# ? Mar 28, 2024 09:52 |
|
That or they should explain what passing by reference actually does I guess I should feel fortunate that a lot of Java schools probably don't even touch on that. Oh well, I'm going into SEGFAULT territory this coming fall
|
# ? Apr 19, 2008 01:04 |
|
When my kids start learning how to use objects I go out of my to explain this to them. But yeah I don't think the Deitel book even offers an explanation.
|
# ? Apr 19, 2008 03:41 |
|
Coming from a C/C++ background on pointers and references, that was a very helpful discussion. I was always suspicious of java's magic when it comes to passing around objects. My question relates to the garbage collection in the example. How would you get rid of the point object entirely? just by setting pnt = null in the main method?
|
# ? Apr 19, 2008 19:42 |
|
Leehro posted:Coming from a C/C++ background on pointers and references, that was a very helpful discussion. I was always suspicious of java's magic when it comes to passing around objects. The VM will mark it collectible after its containing block's scope ends unless it's referenced somewhere else currently in scope or static, like a Map or a List of some kind. Note that it's just marked for collection, the actual collection happens at some other point. Like you said, you can set it to null manually and call System.gc(); but absolutely no guarantees are made about when the memory consumed by that class will be reclaimed, if ever.
|
# ? Apr 19, 2008 20:21 |
|
Let me see if I'm on the same page with youadante posted:
type: this should be as simple as value.getClass().getName() value: this is where you are stuck. adante posted:
|
# ? Apr 19, 2008 20:47 |
|
hey wiz posted:Let me see if I'm on the same page with you I think I get his point now. There's no good way to get the entries from the internal JS map in the Java host. I hosed with this in Groovy because it allows for much easier class introspection, and you can basically call .get() thusly: code:
The problem comes when you try to do this poo poo in Java, because you can't even import sun.org.mozilla.javascript.internal.ScriptableObject, at least not with my JDK. You could probably get rhino and cast to whatever ScriptableObject Rhino provides and get() from there. It's a mess and you might want to write a wrapper class for it that implements Map, because honestly this is pretty worthless as it is.
|
# ? Apr 19, 2008 22:35 |
|
hey wiz posted:Let me see if I'm on the same page with you hey wiz posted:If you were working on simple classes such as String, Boolean, Double as you mentioned in the example, you could just print value.toString(). However, it seems like you are trying to print the value of Objects that don't have a friendly toString() method. These objects may still have a method which will print exactly what you want, but for each class it would be different. If this was the case, how would you determine which method to call on which class to print what you want? As to how to figure out the type and so forth of the values - well I will deal with that after I figure out how to get the values . As I understand it js is basically variable/object/functions anyway - variables map to string/bool/double, and if I figure out how to manipulate objects (1st part of the problem) then that is solved, which leaves functions to deal with. TRex EaterofCars posted:The problem comes when you try to do this poo poo in Java, because you can't even import sun.org.mozilla.javascript.internal.ScriptableObject, at least not with my JDK. You could probably get rhino and cast to whatever ScriptableObject Rhino provides and get() from there. It's a mess and you might want to write a wrapper class for it that implements Map, because honestly this is pretty worthless as it is. Unfortunately you cannot cast it (or I cannot figure out how to): code:
TRex EaterofCars posted:groovy
|
# ? Apr 20, 2008 09:08 |
|
TRex EaterofCars posted:The VM will mark it collectible after its containing block's scope ends unless it's referenced somewhere else currently in scope or static, like a Map or a List of some kind. Note that it's just marked for collection, the actual collection happens at some other point. TRex EaterofCars posted:Like you said, you can set it to null manually and call System.gc(); but absolutely no guarantees are made about when the memory consumed by that class will be reclaimed, if ever. adante posted:Actually I cannot get the key or list of keys either (maybe you can, if so please share how ).
|
# ? Apr 20, 2008 13:54 |
|
I hata Java. Can someone explain to me why Java keeps spitting out:code:
code:
code:
|
# ? Apr 20, 2008 18:33 |
|
Row 16 in the file that the JVM complains about says: "Graphics2D g = (Graphics2D) drawSpace.getGraphics();" -- at that point, however, drawSpace is not initialized to anything and is therefore null.
|
# ? Apr 20, 2008 18:41 |
|
Anybody here know a good guide to using Derby/JavaDB? I've been following instructions to the t, yet I still can't connect to my friggin' database.
|
# ? Apr 20, 2008 22:48 |
|
ynef posted:Row 16 in the file that the JVM complains about says: "Graphics2D g = (Graphics2D) drawSpace.getGraphics();" -- at that point, however, drawSpace is not initialized to anything and is therefore null. This. A tip to spot these kind of problems right away is using a debugger in Eclipse or NetBeans. They both will display all visible variables and indicate their value, which makes "NullPointerException" easier to deal with on multi statement line of code.
|
# ? Apr 21, 2008 16:56 |
|
ShinAli posted:This. A tip to spot these kind of problems right away is using a debugger in Eclipse or NetBeans. They both will display all visible variables and indicate their value, which makes "NullPointerException" easier to deal with on multi statement line of code. A further tip: In eclipse there is a button in the breakpoints pane to add breakpoints when exceptions occur. Add one to java.lang.NullPointerException
|
# ? Apr 21, 2008 19:19 |
|
I have a question about the HashMap class. I have populated a HashMap with a simple object I have created, basically consisting of two strings. One of these strings holds a segment of text from a file, the other holds the file name. What I want to do is to use the HashMap.contains method to determine if a particular string matches any of the text segments, and then use whatever get method is in the class to determine the file name. I figured that overriding the hashcode method would do the trick; I defined the hashcode method of the class to return the hashcode value of the string that we will be searching for. Apparently, this doesn't quite do the job. I am fairly new to hash tables, so if anyone could give me a few tips it would be very appreciated.
|
# ? Apr 22, 2008 02:57 |
|
clayburn posted:I have a question about the HashMap class. I have populated a HashMap with a simple object I have created, basically consisting of two strings. One of these strings holds a segment of text from a file, the other holds the file name. What I want to do is to use the HashMap.contains method to determine if a particular string matches any of the text segments, and then use whatever get method is in the class to determine the file name. I figured that overriding the hashcode method would do the trick; I defined the hashcode method of the class to return the hashcode value of the string that we will be searching for. Apparently, this doesn't quite do the job. I am fairly new to hash tables, so if anyone could give me a few tips it would be very appreciated. 2. You're not being exactly clear on what you're doing. What it sounds like is that you're building an index and searching it, so your hash map looks like this: "foo" -> "file1.txt" "bar" -> "file2.txt" "baz" -> "file3.txt" "cat" -> "file1.txt" and your search term is "foo". In this case, the strings should be the keys, and the filenames should be the values, and contains() will do what you want. But it sounds like either (a) the strings aren't the keys (what is, then?), (b) you're actually using HashSet instead of HashMap (you want HashMap), or (c) this is not what you mean at all, at which point you should clarify.
|
# ? Apr 22, 2008 03:34 |
|
I had a nice long post typed up and lost it due to the forum maintenance. Basically I asked if this code:code:
edit: This works perfectly, even better than I had hoped it would. Thanks for the help. clayburn fucked around with this message at 04:51 on Apr 22, 2008 |
# ? Apr 22, 2008 04:25 |
I hope Javascript questions are welcome here. There's no "Javascript Megathread", and this question is very very simple, so I didn't want to start a new thread just for it. I'm very new to Javascript (Perl's my language of choice), but I'm writing a web-app using Perl's CGI::Ajax, which enables Ajax by mapping Javascript functions on your web page to Perl functions. Anyhoo, the Javascript functions need to have their parameters sorted in a particular way for CGI::Ajax. I'm having problems with this... so I'm hoping you guys can help me. code:
In my example, I need to feed the following list of parameters into the Perl function - ['foo','yabba','dabba','doo']. Hard coding all the values works fine, or even hard coding a specific array element works. However, just providing the array (with the hope that it'd just iterate through each value) doesn't work. It's hard to tell exactly what it's feeding into functionToInvoke(), but it looks like it's trying to feed the index and then element. Any ideas? EDIT: Just a bit more information... I tried code:
syphon^2 fucked around with this message at 15:30 on Apr 22, 2008 |
|
# ? Apr 22, 2008 15:08 |
|
You might be looking for the concat() method. Try something like this:code:
|
# ? Apr 22, 2008 15:28 |
Fehler posted:You might be looking for the concat() method. Try something like this: How come alert seemed to display my data properly, but when supplying it in the same manner for functionToInvoke, it failed? Maybe something to do with how CGI::Ajax works?
|
|
# ? Apr 22, 2008 15:34 |
|
Regarding hashCode and equals, it's really important that java developers read and understand this: http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf e: fixed link epswing fucked around with this message at 15:46 on Apr 22, 2008 |
# ? Apr 22, 2008 15:37 |
|
syphon^2 posted:That worked! Thanks!
|
# ? Apr 22, 2008 16:23 |
|
syphon^2 posted:I hope Javascript questions are welcome here. There's no "Javascript Megathread", and this question is very very simple, so I didn't want to start a new thread just for it. For future reference - the web dev and the general programming questions thread might be better. JavaScript and Java really only have the name in common.
|
# ? Apr 22, 2008 18:02 |
EDIT: ^^^^^ Duly noted (right on the heels of another post). Thanks for the tip. Another javascript question... I'm trying to use setInterval() and setTimeout() to create a basic timer. I want my page to run doIt() every 10 seconds for 30 minutes, and then stop. (although I have the times cut down drastically for testing purposes) code:
Note that if I change code:
code:
This seems like a pretty simple use-case. What am I doing wrong?
|
|
# ? Apr 22, 2008 18:02 |
|
Try making intervalID global, i.e. add "var intervalID;" at the top outside the function.
|
# ? Apr 22, 2008 20:05 |
Fehler posted:Try making intervalID global, i.e. add "var intervalID;" at the top outside the function. Awesome... that seems to have done the trick! One last time... before I stop pestering you java gurus with js questions... why did this happen? I mean, obviously having intervalID as an argument within an argument brought it out of scope from where it was declared... but why would it do that? Also, how can I reference the intervalID scoped inside the autoDoIt() function? Given the layout of my .js file... I really really want to avoid having a single random global floating around there.
|
|
# ? Apr 22, 2008 20:19 |
|
setTimeout is a bit weird, I believe it always executes in global scope or something to that effect. Someone else can probably give you a proper explanation.
|
# ? Apr 23, 2008 00:36 |
|
triplekungfu posted:setTimeout is a bit weird, I believe it always executes in global scope or something to that effect. Someone else can probably give you a proper explanation. setTimeout()/setInterval() can take a closure as well as a string: code:
|
# ? Apr 23, 2008 02:14 |
|
Incoherence posted:1. First rule of hashCode(): if two objects have the same hashCode(), equals() should return true on them as well. The converse is usually the part people trip over (overriding equals() but not hashCode()). I'm not sure you keep that invariant, which you should do. Unless I'm reading this wrong, you got it backwards. This is a valid, but stupid implementation of a hashcode. code:
logically equivalent but put in a different way: if a.hashcode() != b.hashcode() then a.equals(b) should be false.
|
# ? Apr 23, 2008 02:27 |
|
poopiehead posted:Unless I'm reading this wrong, you got it backwards. If you override either hashCode() or equals(), you should override both such that a.hashCode() == b.hashCode() iff a.equals(b) (and b.equals(a))
|
# ? Apr 23, 2008 03:29 |
|
Incoherence posted:I didn't get it backwards (both directions are equally true), but I should have been more clear about that being an if-and-only-if statement: Sorry. That't not correct though. Multiple values can have the same hash code. You want to minimize collisions, but they will happen. These are both valid hash functions but wouldn't be if a.hashcode()==b.hashcode() -> a.equals(b) return 0; \\1.hashcode() == 2.hashcode but 1 != 2 return x % 35; \\ 1.hashcode() == 36.hashcode() but 36 != 1 java posted:
poopiehead fucked around with this message at 03:54 on Apr 23, 2008 |
# ? Apr 23, 2008 03:48 |
|
poopiehead posted:Sorry. That't not correct though. Multiple values can have the same hash code. You want to minimize collisions, but they will happen. quote:These are both valid hash functions but wouldn't be if a.hashcode()==b.hashcode() -> a.equals(b)
|
# ? Apr 23, 2008 04:12 |
|
It looks like I have another problem with my HashMap involving collisions. Looking at my data set, I know for a fact that collisions are happening, and this is causing problems for me. Every time that I search for a value at a specific key, I only get one of the values, where sometimes I may want the other value. For example, if "foo" and "bar" are stored at the same key, I will only ever get "foo" even though sometimes I may want to find any string that is not "foo." Is there any way to go about this? I have read the API entry for HashMaps over and over and cannot seem to find a solution to this.
|
# ? Apr 23, 2008 16:48 |
|
clayburn posted:It looks like I have another problem with my HashMap involving collisions. Looking at my data set, I know for a fact that collisions are happening, and this is causing problems for me. Every time that I search for a value at a specific key, I only get one of the values, where sometimes I may want the other value. For example, if "foo" and "bar" are stored at the same key, I will only ever get "foo" even though sometimes I may want to find any string that is not "foo." Is there any way to go about this? I have read the API entry for HashMaps over and over and cannot seem to find a solution to this.
|
# ? Apr 23, 2008 17:03 |
|
^ goddamn youclayburn posted:It looks like I have another problem with my HashMap involving collisions. Looking at my data set, I know for a fact that collisions are happening, and this is causing problems for me. Every time that I search for a value at a specific key, I only get one of the values, where sometimes I may want the other value. For example, if "foo" and "bar" are stored at the same key, I will only ever get "foo" even though sometimes I may want to find any string that is not "foo." Is there any way to go about this? I have read the API entry for HashMaps over and over and cannot seem to find a solution to this. So wait, are you trying to store multiple objects under the same key?
|
# ? Apr 23, 2008 17:07 |
|
Yes, and I'm starting to get the impression that it was a really bad idea. I thought that it would simply handle that as a collision and store the object somewhere else that would still be accessible.
|
# ? Apr 23, 2008 17:15 |
|
clayburn posted:Yes, and I'm starting to get the impression that it was a really bad idea. I thought that it would simply handle that as a collision and store the object somewhere else that would still be accessible. If you want to store multiple items under a unique hash, you should make the value a Collection: code:
|
# ? Apr 23, 2008 17:21 |
|
BELL END posted:I was under the impression that HashMaps don't allow more than one value to be stored with the same key, each subsequent call to put(k) overwrites what is already at k. quote:Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old value is replaced by the specified value. Edit: Also TRex's suggestion is the "traditional" way of doing this, and will work if you don't want to pull a JAR dependency. It's just a little bit more work to deal with the collection yourself.
|
# ? Apr 23, 2008 17:58 |
|
zootm posted:The Commons Collections (I link to collections15 since it's the "generic" version) contains a MultiMap collection type for these sorts of semantics. Well I'll be goddamned. It must be a side effect of working in an environment devoid of intellectual curiosity but I never even bothered to check Commons for something like this. Thanks.
|
# ? Apr 23, 2008 18:09 |
|
|
# ? Mar 28, 2024 09:52 |
|
TRex EaterofCars posted:Well I'll be goddamned. It must be a side effect of working in an environment devoid of intellectual curiosity but I never even bothered to check Commons for something like this. Thanks. Glad it's helped you too
|
# ? Apr 23, 2008 19:46 |