Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Michael_Lee on November 21, 2010, 12:24:28 am

Title: Quick guide to Java?
Post by: Michael_Lee on November 21, 2010, 12:24:28 am
Salutations!

I recently joined a robotics club at school, and joined the programming team.  However, as it turns out, the robot will NOT be programmed with either Axe or TI-Basic (blasphemy!) and will be programmed in Java instead.

So my questions to anybody who knows Java is:
0] Can anybody suggest a good guide/resource?
1] What are some common 'gotchas' that might trip me up, especially given that I'm used to coding calculators?
2] What exactly is a class?  And what does private/public/static/etc... mean?
Title: Re: Quick guide to Java?
Post by: jnesselr on November 21, 2010, 03:54:08 pm
0] Java in a nutshell. (http://oreilly.com/)
1] Let's see.  There are a few things.  One is the Object Oriented nature of Java.  That isn't that hard to get used to, though.  Another thing is how everything is structured.  It's not really complicated and explained in said book.
2] Not going to explain classes as it's in that book.  As for the others:
* Private refers to a method or class that cannot be really accessed.  For example, a private method cannot be called outside that class.  Mostly used for internal class stuff.
* Public refers to a method that can be called inside the class, inside the package and outside the package.
* Protected refers to stuff that can only be called inside the package, but not outside.
* Static is slightly complicated, as it requires knowledge of something you don't already know, so here we go.  Say I have a class called MyClass.  Say all MyClass did was store a number.  Not really class worthy as there are "native" things like int to take care of that (Although there is the Integer class, but that's beside the point right now), but nevertheless, let's try it.  So, the "constructer" of MyClass takes one argument.  It can have more than one constructer, btw.  So anyway, to create an "instance" of the class, I do this:
Code: [Select]
MyClass variableName = new MyClass(5);
Which creates a variable called variableName which is an instance of the class MyClass.  Now then, any method not declared static applies to the object itself.  So, the static method variableName.toString() would really be MyClass.toString().  In other words, static references the class, whereas no static references the object.

Final words: Just read the book. ;-)
Title: Re: Quick guide to Java?
Post by: nemo on November 21, 2010, 04:12:33 pm
0: Some random java notes (http://www.leepoint.net/notes-java/index.html). These are kind of advanced though, but there are some basic concepts that are explained there. also, if you need help, this forum (http://www.javaprogrammingforums.com/) responds relatively quickly. they'll make you feel stupid, but they give good explanations and links.

1: if you're used to axe, do NOT use ^ as the modulus operator. modulus is %.(5%3 is 2) also, if you haven't programming computers much here are some examples of operators:

int p = 5;  // two forwardslashes denote comments. assigns 5 to signed integer variable p. there are no unsigned variables.
if (5==p)  // if p is equivalent to 5. notice that there are two equal signs. you'll make the mistake of putting = a few times. so put the numeric on the left side, because then the compiler will say "hey, you can't assign a variable into a numerical number."
/* this is also a comment */

2. a class is essentially your own data type. it has three things: Instance Variables, Constructors, Methods.
instance variables are local to the class. all of your instance variables should be like this:
Code: [Select]
private int instanceVar;
they should have the private visibility modifier. constructors are a special kind of method. they initialize instance variables. methods are basically subroutines. here's a really, really basic class:

Code: [Select]
public class MyClass{
     //instance variables!
     private int aNumber;
     private boolean amIAwesome;
     //constructor(s)!
     public MyClass(/*parameter list goes here*/){
     aNumber = 0;
     amIAwesome = true;     //i am awesome by default.
     }
     public MyClass(int num, boolean isAwesome){
     aNumber = num;
     amIAwesome = isAwesome;
     }

     public void setAwesomeness(Boolean awesomeness){  //public means it can be accessed outside this class. void means it doesn't return anything.
     amIAwesome = awesomeness;
     }
 
     public int getNumber(){          //int means it returns an integer. note: you can have an empty parameter list. you still need the paranthesis.
     return aNumber;
     }
}
Title: Re: Quick guide to Java?
Post by: calcforth on November 21, 2010, 09:13:44 pm
I recently joined a robotics club at school, and joined the programming team.  However, as it turns out, the robot will NOT be programmed with either Axe or TI-Basic (blasphemy!) and will be programmed in Java instead.
Yup. Java is popular so people are using it where it does not belong.
What are some common 'gotchas' that might trip me up, especially given that I'm used to coding calculators?
Surprisingly enough the biggest gotcha is not the Java - it's the culture around Java. It's decidely unhealthy (http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html).

All books and courses will try to teach you "Java way": where extensibility and testability is paramount and efficiency is secondary concern. This works fine when you develop some complex and slow system (where the ability to create something working is often more important then efficiency of the result: if the thing does not work at all then it does not matter if it's efficient or not), but for simple things "Java way" often produces horrible results.

If the task is simple and does not need extensibility then often it's better to just use array or matrix (as if you worked with Basic) rather then hierarchy of objects (as "true Java way" teaches).

Why is that? The problem here is dynamic memory and garbage collection: the more objects you create the more unpredictable the timing becomes - and this is NOT a good thing for robotics. Books will say that lots of temporary objects is not a problem: GC is sophisticated enough and will take care of everything... and this is just not true.

It does not matter how big and elaborate your collection of objects becomes as long as it's essentially static (so you don't allocate anything on constant basis and so garbage collector is not forced to clean up everything constantly), but when you start creating myriads of new objects in response to exteranl stimulus - you are in trouble. I've seen the case where simple logging spent more resources then actual work in automation system because it used standard library - and something like printf("Parameter f is %3.0lg",f); generates dozens of objects (starting from the call itself: it actually creates anonymous array). Jerky reaction become smooth and fast when said logging was replaced with home-brewn procedures which used pre-allocated arrays.
Title: Re: Quick guide to Java?
Post by: DJ Omnimaga on November 21, 2010, 10:06:21 pm
I guess maybe they absolutely want the team to code it in Java. Is it for some sort of java classroom and the teacher decided that it would be in java? That said, in the end, if what is produced is great, I think that's what counts the most, not how it was made. Back in 2004 I remember cases where TI-83+ BASIC games were put down by elitist ASM fanboys just because they were written in BASIC, even if the ASM counterparts weren't even as great (except for speed).
Title: Re: Quick guide to Java?
Post by: jnesselr on November 21, 2010, 10:07:44 pm
I guess maybe they absolutely want the team to code it in Java. Is it for some sort of java classroom and the teacher decided that it would be in java? That said, in the end, if what is produced is great, I think that's what counts the most, not how it was made. Back in 2004 I remember cases where TI-83+ BASIC games were put down by elitist ASM fanboys just because they were written in BASIC, even if the ASM counterparts weren't even as great (except for speed).
True.  In this case, it's basically ease of coding vs. speed requirements.  I program in Java all the time, but usually won't release something in it unless it can afford to be slow, or doesn't do labor intensive things.
Title: Re: Quick guide to Java?
Post by: DJ Omnimaga on November 21, 2010, 10:22:04 pm
That said, I didn't knew robots could be programmed in java. I thought java was only for computer apps, such as simple games and web stuff? Is it even possible to control stuff like robots with it?
Title: Re: Quick guide to Java?
Post by: nemo on November 21, 2010, 10:29:17 pm
Java can be programmed for mostly anything, as of recently. even platform-specific electronics can sometimes run java. if there's one thing java's good at, it's portability its ability for old versions of programs to still work today. oracle is so paranoid about java backwards-compatibility, that some of the constants aren't all uppercase (Color.Green == Color.GREEN). phones can run java, computers can obviously run java, i've even see a VC recorder run java. but what calcforth said is true. it really isn't a good language. and if you try to do something out-of-normalcy in the language, you'll get hit for it because java is all about standards.
Title: Re: Quick guide to Java?
Post by: DJ Omnimaga on November 21, 2010, 10:31:07 pm
Are there exceptions, though? I remember a year ago the Java applet for IRC on Omni website stopped working completly for many people. After a few java updates, it seemed to work fine, though.
Title: Re: Quick guide to Java?
Post by: jnesselr on November 21, 2010, 10:32:28 pm
Are there exceptions, though? I remember a year ago the Java applet for IRC on Omni website stopped working completly for many people. After a few java updates, it seemed to work fine, though.
Java doesn't work on everything.  It must have a JVM or Java Virtual Machine built inside to run the Java ByteCode.
Title: Re: Quick guide to Java?
Post by: DJ Omnimaga on November 21, 2010, 10:33:06 pm
Are there specific browsers restrictions too?
Title: Re: Quick guide to Java?
Post by: calcforth on November 21, 2010, 10:52:58 pm
Java can be programmed for mostly anything, as of recently.
You have very peculiar definition of recently. Java Card (http://www.oracle.com/technetwork/java/javacard/overview/index.html) was introduced 15 years ago.
if there's one thing java's good at, it's portability its ability for old versions of programs to still work today.
Not true as well. C beats Java by wide margin: lots of programs from 1970th can be compiled and run today. And if we are talking about unmodified code, then the answer is two words: Java ME.

if you try to do something out-of-normalcy in the language, you'll get hit for it because java is all about standards.
Yup. Really, the only advantage of Java is the fact that muddleheads can write half-working code. If you give some specs and Java IDE to bunch of monkeys then there are some non-zero probability that they'll produce some kind of more-or-less working program. With C++, Haskell or even Pything this probablity is very close to zero so you are constantly faced with the dilema: what to do with people who are ruining the projects but can not be fired because of labour legislation?
Title: Re: Quick guide to Java?
Post by: DJ Omnimaga on November 21, 2010, 11:01:40 pm
Can we stop with the java trolling? The OP was seeking for help on the Java language itself, not asking how terrible it was. Thank you.
Title: Re: Quick guide to Java?
Post by: AngelFish on November 21, 2010, 11:24:56 pm
I recently joined a robotics club at school, and joined the programming team.  However, as it turns out, the robot will NOT be programmed with either Axe or TI-Basic (blasphemy!) and will be programmed in Java instead.

Is there any chance you can use a different language, or is this for something like FIRST where you're given language requirements? If you can switch languages, I'd recommend something like Python (if possible). It's easy to write and is extremely powerful. If all else fails, you can switch to mechanics. Then you can blame the programmers for everything that goes wrong ;)
Title: Re: Quick guide to Java?
Post by: Michael_Lee on November 21, 2010, 11:59:50 pm
*Gasp* - could this be the genesis of a flame war?  I better nip it in the bud.

This is for the FIRST robotics competition.
I was informed of two choices - Java or C (maybe C++?  One of the C's)
I have no experience in Java - but the team (which I just joined this year) is reusing the bulk of the program from last year, which was written in Java (because one of the seniors last year knew Java).
I would love to use Python, but I have no idea how to get a python environment working.  I had to go to a presentation this Saturday just to learn how to get Java/C++ running.
I don't care much about the detriments of Java, especially because I don't have any viable alternative to present and because I'm not experienced+eloquent enough to communicate the difference well.

In conclusion, thanks Graphmastur and nemo - I'll be sure to check out those guides.
Title: Re: Quick guide to Java?
Post by: DJ Omnimaga on November 22, 2010, 12:03:31 am
Don't worry, I'm gonna make sure this won't turn into a language wars. Check forum rules #2 anyway (I guess 1 might apply too)
Title: Re: Quick guide to Java?
Post by: AngelFish on November 22, 2010, 12:26:45 am
This is for the FIRST robotics competition.

Is it the season already? That means that I've been slacking as a mentor... :P
Title: Re: Quick guide to Java?
Post by: calcforth on November 22, 2010, 04:27:49 am
This is for the FIRST robotics competition.
I was informed of two choices - Java or C (maybe C++?  One of the C's)
All things being equal C++ will be better choice. Of course things rarely are equal :(
I have no experience in Java - but the team (which I just joined this year) is reusing the bulk of the program from last year, which was written in Java (because one of the seniors last year knew Java).
Well, if they already have something working then it's not a good idea to start total rewrite in different language. It's rarely a good idea in general (http://www.joelonsoftware.com/articles/fog0000000069.html), but especially when you are competing with someone.
I would love to use Python, but I have no idea how to get a python environment working.
Python is worse then Java in many aspects. It very nice to program, but quite slow. The one thing which is fast in Python and slow in Java is C/C++ interface.
I had to go to a presentation this Saturday just to learn how to get Java/C++ running.
My advice WRT to Java/C++ integration: don't try to push small pieces of task to C/C++. JNI is very slow by design: because it must do lot's of thing WRT GC (objects must be compacted and frozen, etc), so while C/C++ procedure may be faster then Java the switch from C/C++ and back will often eat all adavantages of such hybrid approach.
I don't care much about the detriments of Java, especially because I don't have any viable alternative to present and because I'm not experienced+eloquent enough to communicate the difference well.
You must keep them in mind if you want to write competive Java program. It's very easy to lose track of things in Java - and this leads to tons of unneeded work in runtime. It's possible to write good program in Java, but sadly Java encourages mindless programming so you must resist.

Also, don't forget about the biggest advantage of Java: powerful IDEs. Two top free ones are Eclipse (http://www.eclipse.org/downloads/moreinfo/java.php) and NetBeans (http://netbeans.org/). Two top paid ones are IntelliJ (http://www.jetbrains.com/idea/) (it has free community version (http://www.jetbrains.com/idea/free_java_ide.html) and classrom license (http://www.jetbrains.com/idea/buy/buy.jsp#classroom)) and JBuilder (http://www.embarcadero.com/products/jbuilder) (very powerful but also very expensive). My advice: don't try to program Java without help of IDE (VIM and emacs with bunch of addons can be considered an IDE but only make sense if you were avid VIM/emacs users before trying Java). This is greatest strength of Java and a reason for it's continuing existence.

Also: ask around for what other members of team are using. Often it's easier to use the same IDE as others are using rather then using superior (in your opinion) IDE which does not play well with changes made by others.

And last but not least: learn some version control system if you still have time! My preference will be Git (http://git-scm.com/) - but only if you are using Eclipse (http://www.vogella.de/articles/EGit/article.html) or IntelliJ (http://wiki.jetbrains.net/intellij/Using_Git_Locally): NetBeans and JBuilder have unofficial modules (development of official NetBeans plugin started just recently (http://netbeans.org/projects/versioncontrol/pages/Git_main)) thus intergration is not as good. But again: talk with other members of a team.
Title: Re: Quick guide to Java?
Post by: Binder News on November 22, 2010, 06:50:09 am
The best guide I ever read for Java was Thinking in Java by Bruce Eckel. http://www.mindviewinc.com/Books/TIJ4/PurchaseBook.php
As an "IDE", I use Notepad++ http://notepad-plus-plus.org/ It has hilighting for C,C++,Java, and a ton of other languages as well. It is also small, and can be run from a flash drive.
Title: Re: Quick guide to Java?
Post by: calcforth on November 22, 2010, 07:14:13 am
As an "IDE", I use Notepad++ http://notepad-plus-plus.org/ It has hilighting for C,C++,Java, and a ton of other languages as well. It is also small, and can be run from a flash drive.
Notepad++ is fine editor, but it's not an IDE. Good Java IDE includes "code-sense": it detects errors as you type your program, proposes changes to it if you are doing something in suboptimal way, etc. This is what makes it possible to use Java for "monkey programming". The only problem here is the fact that it's not omnipotent and it's suggestions are helpful about 90% of time and the other 10% of time you should say "no" to it's hints. And it only knows simple patterns - it'll never propose to replace hash with rb-tree (or the other way around). Still it's useful tool if you use it as a suppllement to your own brain and not as a replacement for it.
Title: Re: Quick guide to Java?
Post by: Lionel Debroux on November 27, 2010, 09:48:11 am
IDEs (or at the very least, editors with syntax coloring) are nice to use Java indeed... but probably only after one knows several basics, e.g. command-line java and javac, the CLASSPATH :)
On Thursday afternoon, at work, we lamented about the high proportion of students who learned Java only under Eclipse/Netbeans, and are lost without that foundation... Such students can be completely incapable of debugging simple NoClassDefFoundError-type problems, which arise during their internships if they're using external libraries.