|  | |  |
09-29-2008, 07:34 AM
|
#1 (permalink)
| | Regular Contributor
Join Date: Oct 2004
Posts: 227
| getting input from user and other java questions first off, random tangent. i made it to college. woo! on the downside my classes are still mindumbingly boring
all the computer science classes here are taught in java, which is something i dont quite understand. as ive read in here, if you want to write applications for your cell phone java is a great choice, but outside of that, it doesnt serve as much porpose as faster languages such as c++, especially the classes for game design majors and computer science majors are the same thing, and almost all games are written in c++ i dont see why im being forced to learn a language that will serve me little purpose in what i want to do in the real world. no offense to people who program java for a living.
now to the question, my class has us using the scanner class from java.util to get input from the user, and it just seems like there should be a better way to do it. i read the bulk of a c++ book back in my day and cin already seems like it was better than using the scanner class, is there a better alternative.
second question that comes to mind, my teacher said that once a variable in java was stored, its not possible to remove it from memory, is that true? she also claimed that objects could be removed from memory. now my understanding of classes so far has been, that when you create a class, your essentially creating your own type of variable with its own set of parameters and methods. and that an object is an instance of that class, stored into a variable. now if she is right why can one remove an object from memory but not a variable.
third question, does java ever use the free store or pointers?
fourth, how does one user reference in java? or do they even exist
PS sorry if any portion of that seems off, the school notebooks have the tendency to make the cursor jump to random points as im typing, or my hand hits the touch pad. in either sense typing on these are annoying. |
| |
09-29-2008, 05:43 PM
|
#2 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,175
| Java can be used for games, but in general platform API C/C++ is more widely supported. Java is geared more towards enterprise solutions, simulations, etc.
I haven't used Scanner - it was added after I stopped active Java development. It appears, however, to neatly do what used to take several steps - that is parse a String into a primitive. To a seasoned C/C++ programmer, this might seem bulky, but it's purposefully designed so. Java doesn't allow for syntactic shortcuts like C/C++ does. Java syntax is very simple and very strictly enforced - that reduces programmer error. In the case of Scanner, it also does a fair bit of error checking - something you would either need to do yourself in C/C++.
Your next question in more of a JVM question, and I'm kinda on weak ground here. The JVM does garbage collection on its own schedule. That is, it goes through and cleans out anything in memory that is no longer being referenced. If you remove an Object from an ArrayList, and don't reference it anywhere else in your program, the JVM will remove that Object from the memory. Your teacher is somewhat correct in your telling - a variable in Java is either a primitive or an Object. A primitive has the value assigned to it, whereas an Object variable is actually the pointer. You can null out the Object pointer, but you can't do that with a primitive. Both remain in memory until the garbage collector decides the program no longer needs them and frees up their memory.
I'm not sure I'm clear on what you're asking in your third question. Yes, Java uses pointers, but for the most part they're transparent to the programmer, so you don't manage them yourself.
Question 4 - again, not sure what you're asking. In Java, an Object variable is a pointer reference, whereas a primitive variable is the value assigned to it.
Additionally, in Java, methods are pass-by-value, even in the case of Objects (the object isn't copied, but the pointer is - see here for a more indepth explaination).
Additionally, the Java Language Spec might be helpful |
| |
09-30-2008, 08:27 PM
|
#3 (permalink)
| | Regular Contributor
Join Date: Oct 2004
Posts: 227
| well i started to read a c++ book awhile ago, and it put heavy emphasis on pointers and their uses especially pointers to the free store. but as your saying java seems to keep them hidden even to the programmer.
and for referencing a variable i meant as making a variable point to another. although the word point is very misleading there. in php you reference a variable usring $var &= $var2; and i believe in c++ the & is used for references too.
now i may be wrong, but my understanding is when you reference another variable, the data is copied but the variable only points to the location in memory of the first variable. so referencing for the most part when used correctly would save memory, and im assuming increase speed.
and as far as i know the only java program i use is azureus, and most azureus users generally switch to utorrent or something else because of azureus lack of speed from the JVM.
and for games, i know runescape is an mmorpg programmed in java, not bad for what it is, having very low requirements when i played. but it wasnt unleashing WOW from its monopoly on mmorpgs. im just very bitter being taught java and not c++ as i feel like im going to have to do a lot of supplementing after school to transition to c++, and i have to pay for it all. |
| |
10-01-2008, 02:49 AM
|
#4 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,175
| Quote:
Originally Posted by falsepride well i started to read a c++ book awhile ago, and it put heavy emphasis on pointers and their uses especially pointers to the free store. but as your saying java seems to keep them hidden even to the programmer. | Well, you don't have to manage the pointers in Java - it's done automatically for you. This is good because it prevents a major source of human error in programming. Quote: |
and for referencing a variable i meant as making a variable point to another. although the word point is very misleading there. in php you reference a variable usring $var &= $var2; and i believe in c++ the & is used for references too.
| In Java, all you do is a = b. Assuming a and b are declared as Objects, you've just reassigned the pointer. If you ever want a copy of an object, you generally need to call a method called "clone". Primitives are just assigned the value - no pointers involved. Quote: |
now i may be wrong, but my understanding is when you reference another variable, the data is copied but the variable only points to the location in memory of the first variable. so referencing for the most part when used correctly would save memory, and im assuming increase speed.
| The only time data is copied is when you pass an Object reference to a method, and only then the pointer is copied, not the data contained by the Object. See that link I provided above for more information Quote: |
and as far as i know the only java program i use is azureus, and most azureus users generally switch to utorrent or something else because of azureus lack of speed from the JVM.
| The problem with Azureus isn't the JVM but rather that the program has become bloated. If uTorrent tried to do as much as Azureus was doing, it'd be slow too. I work with Java programs all the time that are just as fast or faster than if they were written in C/C++, but they were written by very skilled programmers. The thing is that Java is very forgiving, so it allows the programmer to make mistakes or write inefficient code. C/C++, on the otherhand, is not nearly as forgiving and has a higher threshold for entry. Quote: |
and for games, i know runescape is an mmorpg programmed in java, not bad for what it is, having very low requirements when i played. but it wasnt unleashing WOW from its monopoly on mmorpgs. im just very bitter being taught java and not c++ as i feel like im going to have to do a lot of supplementing after school to transition to c++, and i have to pay for it all.
| Only recently has Java allowed decent access to the OpenGL framework, and even then most game designers are using DirectX for game design anyhow. Because Java is a cross-platform language, it's hard to leverage the knowledge and talent currently out there for game design when the most popular graphics framework isn't available to you.
However, keep in mind that your over-riding priority after college is getting a job. Java is a very hot commodity right now - most of the people I've interviewed with note that right off the bat as a big plus. No, it might not be all that helpful for game design (at least the game design you want - you could design mobile or web games), but it will help get you employed as a programmer. You can then get your company to pay for your education as you attend a school that'll teach you C/C++
Last edited by Belisarius; 10-01-2008 at 04:26 AM.
|
| |
10-01-2008, 08:02 AM
|
#5 (permalink)
| | Regular Contributor
Join Date: Oct 2004
Posts: 227
| i can understand the makers of java wanting to make human error less prone, but it does restrict the amount of freedom programmers have. and im my opinion programming shouldnt be something for the weak. now im going to cite wikipedia here, but the number of bad web apps programmed in php getting sql injection or other things is increasing every year, by large percentages. php is easy to program in, but it leads to thousands of bad web apps being written every day.
but thank you for clearing some things up for me, im sure as this class progresses at its slow rate, ill be coming up with more questions. |
| |
10-01-2008, 05:05 PM
|
#6 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,175
| Well, it doesn't really restrict the programmer - it simply does the pointer management for you. I haven't seen a legitimate concern over it yet (maybe you've encountered it). I mean, most high-level programming languages don't have you working the registers either, but I don't think anyone really feels restricted by their absence.
What I like about Java is that while it's forgiving to the novice, it's still very strict. That is that while it doesn't blow up on you if you do something wrong, it does require you to adhere to a very strict syntax - something PHP (or C++ for that matter) doesn't. In PHP, you never know if you're dealing with a string or an int or a boolean. In Java, it's explicit. |
| |
10-07-2008, 10:49 PM
|
#7 (permalink)
| | Code Monkey
Join Date: Apr 2004 Location: Silicon Valley, CA
Posts: 64
| Quote:
Originally Posted by falsepride i can understand the makers of java wanting to make human error less prone, but it does restrict the amount of freedom programmers have. and im my opinion programming shouldnt be something for the weak. now im going to cite wikipedia here, but the number of bad web apps programmed in php getting sql injection or other things is increasing every year, by large percentages. php is easy to program in, but it leads to thousands of bad web apps being written every day.
but thank you for clearing some things up for me, im sure as this class progresses at its slow rate, ill be coming up with more questions. | I don't understand what you're saying with the php bit... are you simply offering two sides to an argument in the same paragraph?
what you said about php in effect is precisely why java handles the pointers for the programmer. java handles pointers so the number of insecure apps aren't increasing every day. in c++ pointers are where a lot of security flaws come from. in c++ if you have, for example, an array of ints of length 10 called array1, calling an element of the array is essentially just pointing to a place in memory. so if you call array1[2] then you're saying look at array1 and up 8 bytes to find the int. but if the programmer doesn't limit the input and someone calls array1[20] then its saying look at the array and up 80 bytes and c++ thinks nothing of it even tho the array is only 40 bytes long. In certain cases this can lead to serious security flaws. java would return an ArrayOutOfBoundsException and quit.
that's my understanding... please correct me if something is wrong. |
| |
10-07-2008, 11:39 PM
|
#8 (permalink)
| | Code Monkey
Join Date: Apr 2004 Location: Silicon Valley, CA
Posts: 64
| oops, that should be IndexOutOfBoundsException. |
| |
10-08-2008, 06:48 PM
|
#9 (permalink)
| | Regular Contributor
Join Date: Oct 2004
Posts: 227
| well granted the c++ book i was reading wasn't the best, it never claimed to be a windows or universal c++ book, but all the code examples and main returning void, which is microsoft specific i believe. but it strongly warned the dangers of using global variables, and recomended storing most of your data on the free store.
i can see c++ having a lot of room for error, the more freedom you give a person the more likely the person can screw up, but more freedom generally leads to things being done easier.
for example, for rubiks cubes, i have a professors cube(5x5x5) and a standard (3x3x3). you would think with the added complexity of the professors cube its alot harder to solve, but in reality the added freedom or more places to stash cubes its easier for the first half of the solve(which is actually just grouping cubes until it acts like a 3x3x3). but at the same time, with the added complexity, if you screw up an algorithm, your more likely not going to be able to recover from it and will have to back track serverly.
but in essence the added freedom makes it easier. so more freedom in a language could lead to things being easier |
| | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -8. The time now is 01:22 AM. |
Copyright © 2000-2008, Milano Interactive Web Hosting provided by Portal 360 Web Hosting |  | |