Author Topic: Quick guide to Java?  (Read 10993 times)

0 Members and 1 Guest are viewing this topic.

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Quick guide to Java?
« 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?
« Last Edit: November 21, 2010, 12:25:38 am by Michael_Lee »
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Quick guide to Java?
« Reply #1 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. ;-)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Quick guide to Java?
« Reply #2 on: November 21, 2010, 04:12:33 pm »
0: Some random java notes. These are kind of advanced though, but there are some basic concepts that are explained there. also, if you need help, this forum 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;
     }
}
« Last Edit: November 21, 2010, 04:12:52 pm by nemo »


Offline calcforth

  • LV3 Member (Next: 100)
  • ***
  • Posts: 62
  • Rating: +4/-4
    • View Profile
Re: Quick guide to Java?
« Reply #3 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.

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.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Quick guide to Java?
« Reply #4 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).

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Quick guide to Java?
« Reply #5 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.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Quick guide to Java?
« Reply #6 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?

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Quick guide to Java?
« Reply #7 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.


Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Quick guide to Java?
« Reply #8 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.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Quick guide to Java?
« Reply #9 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.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Quick guide to Java?
« Reply #10 on: November 21, 2010, 10:33:06 pm »
Are there specific browsers restrictions too?

Offline calcforth

  • LV3 Member (Next: 100)
  • ***
  • Posts: 62
  • Rating: +4/-4
    • View Profile
Re: Quick guide to Java?
« Reply #11 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 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?

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Quick guide to Java?
« Reply #12 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.

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Quick guide to Java?
« Reply #13 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 ;)
« Last Edit: November 21, 2010, 11:25:18 pm by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Quick guide to Java?
« Reply #14 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.
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.