|
I didn't see one of these in the generic thread, so I guess it's just a matter of time. Here's a thread for small questions relating to everyone's favourite programming language, Java. Chuck your questions up here. Attention! Java is not JavaScript Despite their similar names the languages are hugely different; questions relating to JavaScript should be directed to the Web Development Megathread I'm going to follow this with a post on IDEs/tools/etc. Anyone who wants stuff added to that post or contact me or whatever. zootm fucked around with this message at 18:57 on May 21, 2008 |
# ? Feb 23, 2008 11:11 |
|
|
# ? May 11, 2024 16:37 |
|
Hefty information post Here's the promised post on tools. IDEs A quick note on IDEs: Java is very verbose, so autocompletion is handy when working on it for any period of time. Luckily Java has some really great IDEs handy: NetBeans IDE - Open source, developed primarily by Sun. Gets a lot of features based around whatever Sun is currently pushing, uses Ant as its package format (meaning it's easy to run your build remotely) and has a really nice GUI editor, along with excellent support for Ruby on Rails should you decide to run, screaming, from Java at some point. Also comes with a fairly good profiler which I would recommend more highly if it hadn't crashed and burned when I was trying to profile something over a transatlantic link the other day. Eclipse - Another open source IDE, a little bit more flexible than NetBeans, but does more stuff for you. This was clearly better than NetBeans for a long time so this is what most teams use these days. Has a huge wealth of plugins (of variable quality) for the various frameworks available for Java. Also Bubblegum Wishes recommends this series of videos teaching Java from the ground up using Eclipse. IntelliJ IDEA - Costs money, but pretty much nobody who tries it ever goes back. I've not used it, but I don't think I've ever heard a negative review. Another note, though - it might be worth learning Java without these tools. They do a lot for you, and you will miss out on a lot of the subtleties of how Java works. Finding problems Debuggers and profilers (all of the tools above have at least the former), as with most languages, can be invaluable in finding Java problems. It's always worth knowing how your dependencies work, too, since it's the cause of a lot of misconceptions. Preventing problems Another tip is to use FindBugs to find common errors in your code. It may give you a lot of "false" positives, but if you keep your code clean you get a much nicer protection against the dumb mistakes that we all make sometimes than you get from "it compiles". I've run this ever since spending 2 hours with a workmate trying to find the cause of weird behaviour which turned out to be caused by someone doing == comparison on a boxed integer value ( this doesn't do what it looks like it does). For those of you who like to be doubly paranoid, PMD does this sort of job too. PMD works on source code (rather than compiled bytecode) though, which makes it a whole bunch easier to, among other things, add your own rules. This gives you the freedom to add a check on something you know you do a lot, which is handy. It's probably for more-advanced or -paranoid users to run both, though, and FindBugs will likely be easier and more productive if you only want to use one. Also, run your compiler with the "-Xlint" flag. This turns on all the warnings. This is a good thing. If you look at your code and see a warning that you decide is unwarranted (sadly there are some things that just don't work well with generics without warnings), you can add the @SuppressWarnings( "warningType" ) annotation to the method in questions, but do not do this until you are confident you understand the reason for the warning. Your code should be clean of warnings from both the compiler and FindBugs, unless you have a drat good reason to the contrary. zootm fucked around with this message at 13:26 on Jul 15, 2009 |
# ? Feb 23, 2008 11:19 |
|
I'd like to post that everyone who uses Java to make money should know how both .equals() and .hashCode() work. You can find numerous guides online, plus Joshua Bloch's Effective Java goes into great detail about these methods. I won't get into it here to avoid a huge poo poo post, but here are some links: http://www.geocities.com/technofundo/tech/java/equalhash.html http://www.ibm.com/developerworks/java/library/j-jtp05273.html a good reason why you should know your poo poo: http://www.hibernate.org/109.html Also, understand the ramifications of having immutable Strings. A lot of people I've worked with don't understand the concept of immutable Strings and don't know a reason for using StringBuilder other than "you just do." What the gently caress. Also available in numerous online resources or by reading Effective Java. IntelliJ is really nice. Eclipse is ok if you are willing to put up with its poo poo. Someone likened it to Mac vs PC. If you want something that just works, use IntelliJ, if you like to tinker, go with Eclipse. I haven't used NetBeans enough to say if it's good or bad, all I'm sure of is that Sun is really pushing JRuby so their ruby support is very good.
|
# ? Feb 24, 2008 00:12 |
|
Glad to see this thread here. It looked like java was dead based on the activity in this forum. I didn't start writing java until about a year ago, and started using an IDE just a few months ago. I started with Eclipse because I read about its tomcat plugin. It absolutely blew my mind that I could run a webapp in tomcat, play around on my browser, and be debugging it in eclipse. When I had to write a GUI application, I switched to NetBeans 6.0, which I absolutely love. It seems cleaner and simpler than Eclipse, and the GUI designer is terrific. I'm also very pleased with the subversion integration (showing changes) and the build configurations and how it creates a "dist" directory complete with a README and all of your JARs in a lib folder. So no questions now, but I'm sure I'll have one soon.
|
# ? Feb 24, 2008 06:46 |
|
Can one use IntelliJ or Netbeans if they're developing Java RCP applications that target the Eclipse framework itself, or does that lock one into having to use Eclipse? I'm doing this at work, but I can't friggin' stand Eclipse. It's slower than an rear end in a hat.
|
# ? Feb 24, 2008 16:56 |
|
csammis posted:Can one use IntelliJ or Netbeans if they're developing Java RCP applications that target the Eclipse framework itself, or does that lock one into having to use Eclipse? I'm doing this at work, but I can't friggin' stand Eclipse. It's slower than an rear end in a hat. I'd assume if it were making calls into the framework library, then you are pretty much stuck with Eclipse. If it were making calls into a library that acts as an intermediary framework between the plugin and the IDE, then it could be ported, but I have no idea if such a platform exists.
|
# ? Feb 24, 2008 18:27 |
|
csammis posted:Can one use IntelliJ or Netbeans if they're developing Java RCP applications that target the Eclipse framework itself, or does that lock one into having to use Eclipse? I'm doing this at work, but I can't friggin' stand Eclipse. It's slower than an rear end in a hat.
|
# ? Feb 24, 2008 22:31 |
|
csammis posted:Can one use IntelliJ or Netbeans if they're developing Java RCP applications that target the Eclipse framework itself, or does that lock one into having to use Eclipse? I'm doing this at work, but I can't friggin' stand Eclipse. It's slower than an rear end in a hat. I don't know how well it'll work, though. It'd be wise to not wipe Eclipse before you try it out for a week or so, despite how tempting it is.
|
# ? Feb 25, 2008 04:14 |
|
I'm having problems getting the visual editor working in eclipse on OS X. Has anyone in here gotten it to work or do you have any tips to try to get it working? I tried installing it through Help->Software Updates->Find and install, using the Graphical Editing Framework server option thing. It didn't work, as I can't see any graphical editing tools anywhere in eclipse. This same method, however, DID work for the Ruby tools and the C/C++ tools. So, does anyone know how I can fix this? I may need it for some programming projects for class later on this semester, and I'd like to have it working by then.
|
# ? Feb 25, 2008 07:10 |
|
Not strictly a Java question, but, does anyone know if Eclipse has anything similar to visual studio's #region ?
|
# ? Feb 25, 2008 12:07 |
|
Belgarath posted:Not strictly a Java question, but, does anyone know if Eclipse has anything similar to visual studio's #region ? code:
Incidentally this feature is available by default on Netbeans which uses an XML syntax like this: code:
|
# ? Feb 25, 2008 18:43 |
|
Stupid IDE question.... I've been using JCreator LE, another free Java IDE, and, other than plain text editors, the only thing I've used for Java programming. I'm almost ashamed to admit that I haven't played around with any others. It's just been a "it's what I downloaded first, so that's what I use" kinda thing. What benefits do the others have and how does JCreator rank among the others?
|
# ? Feb 25, 2008 23:38 |
|
I'm not sure about JCreator since I've never used it, and the other ones are basically like I noted in the second post of this thread; is there anything specific you'd like to know?
|
# ? Feb 25, 2008 23:53 |
|
FuzzyBuddha posted:Stupid IDE question.... Well first and foremost, JCreator is Windows only. All the others are cross compatible. From a Windows user point of view JCreator is nice since its written in C++ and has a very fast clean interface. The debugger is also very well done, similar to the .NET debugger. The downsides are fairly large, It doesn't do code completion unless you purchase the pro version, it only connects to a CVS repository (no SVN support), No drag/drop GUI creator, and it doesn't have the ability for plugins. Not supporting plugins is what really hurts it. Simple things like debugging a JSP, or Servlet which is pretty straight forward in the other IDE's just isn't possible with JCreator. As someone who has paid for the professional version of JCreator, I wish I had put the money into IntelliJ.
|
# ? Feb 26, 2008 00:26 |
|
FuzzyBuddha posted:Stupid IDE question.... after a quick look at the features of JCreator Lite, I can point out a few things that NetBeans has that it doesn't: Code Completion Context-sensitive help Debugger bean methods wizard There's a lot more. NetBeans seems more friendly whether you started your application there or you're working from existing sources. It creates ant build scripts for you. The refactoring is incredibly useful -- whether you want to rename a class/package or easily add get/set (bean) methods to something. The javadoc popups and code completion have really been useful. It just makes a lot more sense than any other IDE I've used. Plus it's cross platform, free, and works with some other languages. There are a lot of plugins too, and I think it's less cluttered than Eclipse. Try NetBeans for a day or even an hour. It's easy to get up and running.
|
# ? Feb 26, 2008 02:56 |
|
edit: see next post
icehewk fucked around with this message at 05:59 on Feb 26, 2008 |
# ? Feb 26, 2008 04:13 |
|
After six weeks of my first programming course, I know absolutely nothing. The instructor doesn't teach along with the book, nor does he teach much of the ideas behind the code. To borrow a lovely analogy, it's like he goes over what you can nail where without explaining where the spot to be nailed is located or why it's being nailed. As such, I've taken it upon myself through the resources provided here to learn Java outside of class and hope I can catch up before the next project is assigned. Unfortunately, my latest project is due Wednesday and I don't know where to begin. Can you guys help?quote:CS 172
|
# ? Feb 26, 2008 06:05 |
|
Hmm I was thinking about that program in the shower from what you posted in the other thread, and it looks a bit different from what I thought it would be. When I was thinking of the addition, I was implementing reducing to the lowest terms. This looks like it just multiplies the denominators and leaves it like that. Just curious though, would a feature to keep people from entering fractions that would become complex use the modulus operator?
|
# ? Feb 26, 2008 06:58 |
|
icehewk posted:After six weeks of my first programming course, I know absolutely nothing. The instructor doesn't teach along with the book, nor does he teach much of the ideas behind the code. To borrow a lovely analogy, it's like he goes over what you can nail where without explaining where the spot to be nailed is located or why it's being nailed. As such, I've taken it upon myself through the resources provided here to learn Java outside of class and hope I can catch up before the next project is assigned. Unfortunately, my latest project is due Wednesday and I don't know where to begin. Can you guys help? OK. With objects, what you're doing is describing their behavior, so that if you've got r1 and you add it to r2, you explain how the sum is found with plus(). I'll show you how to make an invert() function, something which isn't part of your assignment but is useful for a RationalNumber class. In order to invert a number, we make its numerator equal to the denominator and the denominator equal to the numerator. code:
|
# ? Feb 26, 2008 07:55 |
|
fret logic posted:When I was thinking of the addition, I was implementing reducing to the lowest terms. This looks like it just multiplies the denominators and leaves it like that. It says in the instructions that in the next project they will reduce the fraction to it's lowest terms. For now it's enough that they just keep multiplying the denominator.
|
# ? Feb 26, 2008 08:11 |
|
icehewk posted:After six weeks of my first programming course, I know absolutely nothing. The instructor doesn't teach along with the book, nor does he teach much of the ideas behind the code. To borrow a lovely analogy, it's like he goes over what you can nail where without explaining where the spot to be nailed is located or why it's being nailed. As such, I've taken it upon myself through the resources provided here to learn Java outside of class and hope I can catch up before the next project is assigned. Unfortunately, my latest project is due Wednesday and I don't know where to begin. Can you guys help? Let's walk through plus(): First, how do you add fractions? If you have two fractions a/b and c/d, what is their sum in terms of a, b, c, and d? Second, which field of which object corresponds to a, b, c, and d?
|
# ? Feb 26, 2008 08:43 |
|
MEAT TREAT posted:It says in the instructions that in the next project they will reduce the fraction to it's lowest terms. For now it's enough that they just keep multiplying the denominator. Yeah, I was jus' sayin.
|
# ? Feb 26, 2008 08:55 |
|
Incoherence posted:Let's walk through plus(): First, how do you add fractions? If you have two fractions a/b and c/d, what is their sum in terms of a, b, c, and d? Second, which field of which object corresponds to a, b, c, and d? It would be a+c when b=d. How would you go about finding the LCD? Is the data field the part that corresponds to a, b, c and d?
|
# ? Feb 26, 2008 18:15 |
|
icehewk posted:It would be a+c when b=d. How would you go about finding the LCD? Is the data field the part that corresponds to a, b, c and d? Don't worry about least for the moment, just common.
|
# ? Feb 26, 2008 18:42 |
|
Given the following conditions: code:
|
# ? Feb 26, 2008 20:18 |
|
icehewk posted:It would be a+c when b=d. How would you go about finding the LCD? Is the data field the part that corresponds to a, b, c and d? And, as previously mentioned, don't worry about the LCD. Look at the example output for a hint as to what denominator the result should have. NotHet posted:Given the following conditions: Incoherence fucked around with this message at 20:39 on Feb 26, 2008 |
# ? Feb 26, 2008 20:36 |
|
Incoherence posted:It's a floating point quirk. If you HAVE to have 56.9 for some reason, round it.
|
# ? Feb 26, 2008 20:50 |
|
NotHet posted:Well then this becomes irritating on two levels. I guess I can sorta understand a floating point quirk in Java, but why the hell did Eclipse decide to be inconsistent and represent the value as 56.9 in it's debugger? Oh well. Thanks for the help! http://www.ibm.com/developerworks/java/library/j-jtp0114/
|
# ? Feb 26, 2008 21:05 |
|
Incoherence posted:No, if b = d, then it'd be (a+c)/b. I'm being pedantic here for a reason: this isn't a Java problem, but an algorithmic problem, and if you can't specify how to add fractions algebraically how do you expect to tell the computer how to? So what would I write to find the common denominator? I really don't understand much of the actual language beyond syntax and reference.
|
# ? Feb 26, 2008 21:29 |
|
Fly posted:Most people must readjust their expectations when using fixed precision floating point numbers, such as the IEEE 754 implementation Java has. There are rounding issues because it is impossible to represent some decimal values exactly in such floating point representations. You can also try BigDecimal if you don't need balls to the wall arithmetic performance.
|
# ? Feb 26, 2008 21:54 |
|
icehewk posted:So what would I write to find the common denominator? I really don't understand much of the actual language beyond syntax and reference.
|
# ? Feb 26, 2008 22:21 |
|
Seems the uni knows how tough the class is so they provide a free tutor three times a week. Algebraically, a/b and c/d would require b*d and d*b along with a*d and c*b. That would be (a*d)+(c*b) over (b*d), right?
icehewk fucked around with this message at 00:15 on Feb 27, 2008 |
# ? Feb 27, 2008 00:08 |
|
icehewk posted:Seems the uni knows how tough the class is so they provide a free tutor three times a week. Algebraically, a/b and c/d would require b*d and d*b along with a*d and c*b. That would be (a*d)+(c*b) over (b*d), right? Okay, now that's step 1. Step 2 is to figure out how to express each of a, b, c, and d in terms of the two objects. code:
|
# ? Feb 27, 2008 00:17 |
|
Got it, thanks a ton.
|
# ? Feb 27, 2008 00:53 |
|
Java 1.5: I'm trying to figure out how to load a text file contained inside my program's JAR (into a FileReader if that matters). java.util.ResourceBundle was the first place I looked, but it seems to want to split the file into key/value pairs and that's not what I'm going for. The JAR layout: code:
|
# ? Mar 1, 2008 00:49 |
|
csammis posted:Java 1.5: Class.getResource() and friends will get you a file if it's on your classpath. So, in your example: code:
|
# ? Mar 1, 2008 01:17 |
|
Does anyone have a quick tutorial or example for one of the EE persistence APIs? Everything I come across seems to be part of some framework.
|
# ? Mar 1, 2008 02:56 |
|
rotor posted:You won't be able to write to it, IIRC. Leehro posted:Does anyone have a quick tutorial or example for one of the EE persistence APIs? Everything I come across seems to be part of some framework. Other than that the Javadoc for the API is actually pretty good.
|
# ? Mar 1, 2008 10:37 |
|
I have a receipt printer, connected to a cash drawer that takes plain text input (with special control characters for drawer popping and paper cutting) outputting formatting receipts in my software. The catch is I have to support a variety of printers with different control codes (easy), widths (harder) and demonic possessions (hardest). So what I'm looking for is an easy way to use an existing layout language (LaTeX, HTML, etc.) to layout a document that eventually is put into a String on which getBytes() is called and 100% plain vanilla already formatted text with the right column alignment and justifications. I'm leaning heavily towards either HTML or some horrible hack of ReportMill - but I'm open to ideas and conjecture, as I've got a little while to think about this. I'll be storing the "templates" in the database and they're accessed from just a plain text field, since thats how they end up when sent to the printer :/
|
# ? Mar 4, 2008 02:11 |
|
|
# ? May 11, 2024 16:37 |
|
roadhead posted:I have a receipt printer... Interestingly enough I have been messing around with a receipt printer that I got from a friend. It uses a serial connection with a cash drawer, but can use any old serial connection. I use rxtx for the serial communication by the way. ( http://users.frii.com/jarvi/rxtx/ ) The code was modified from examples from Sun and various other sources. It sends a string to the printer and then sends the print command (0x0D on my printer) to print the buffer. http://snipplr.com/view/5264/comm-write/
|
# ? Mar 4, 2008 05:29 |