ID:182279
 
So; my University is clever... last year they forgot to teach us any Java. This year, I'm on a course called "Advanced Design Techniques for Object Orientated Programming: Java" and 80 students (out of 90) have had two terms of Java lectures and labs... the other 10 (including me) have never seen Java before, unless we've studied it in our spare time.

To compensate us for this, they've given us one week to reach the standard of the rest of the class. One week, to learn one year's worth of material!

Anyhow; I have a sound knowledge of programming techniques, and a fair command of design as well. I just don't know any of the syntax or alike for Java, having never seen it before. If anyone knows a really quick guide/summary for all of the basics of Java I would re-he-heally appreciate a link to it. Even a quick guide on this forum would be great.

Thanks for your time!

~Ease~
What languages do you know?

You'll be pleased to hear that Universities teach Java at such a slow pace, you really could catch up in a week.
Ease wrote:

(...)If anyone knows a really quick (...) summary for all of the basics of Java I would re-he-heally appreciate a link to it.

This is what google yields as for a quick summary:
http://www.cs.fit.edu/~pkc/classes/cse1001/syntax.pdf

Some parts of it are decent, some are just plain out ugly...

As for a quick introduction:
Java, like BYOND, is based on a VM (though, unlike BYOND, you can use a preprocessor to speed up the code).
That means the user has full controll of what s/he allows the programm to do and thus is (asides of bugs and security holes) rather save.

'Java in a Nutshell' describes the language as:
"A simple, object orientated, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded and dynamic language"

Since version 1.5 was rather slow and unstable, I didn't bother to go past 1.42, meaning what I tell you might be outdated.

Java makes heavy use of packages (apis) that bring a nice functionality. You can get a quick overview at the sun homepage:

http://java.sun.com/j2se/1.4.2/docs/api/ overview-summary.html

To list each and every part of the "syntax" would bust the size of this posting, but the core syntax is very similar to C++ (minus pointers).

Since I am german and I assume you're american, I'm unsure about your education system, but maybe this might help you:

http://www.leepoint.net/notes-java/summaries/ summary-intro-prog.html

( http://www.leepoint.net/JavaBasics/index.html //
http://www.leepoint.net/notes-java/ )

Unless you have more specific questions, I'm sorry, I couldn't come up with anything else in a short time.
I hope it helps and I wish you fun / success.

MIT OpenCourseWare
If you know C++, you're well ahead of the game. Java syntax is almost exactly the same since Java syntax is based on C++'s.

Something else to keep in mind with Java: everything, with very few exceptions, is an object. Your program entry point is itself part of an object and can be inherited, strings are objects, there is an integer object, if you program in the standard Java style then all of your program components will be encapsulated into objects. Java is about as far toward OOP as you can go on the procedural vs. OOP scale.

If you don't know C++, the fact that you probably can use Byond (I'm assuming that since you're posting here) is a decent start since Byond is also based on C++.

Now to get to the point (And I'll try to do so quickly and shortly)...

I'll be using "Java terms" from here on out. Java junkies are very picky about the terms you use and the way in which you write your code.

An object is a "class"
A function/method/procedure is a "method"

A class is defined with the keyword class followed by the class' name, and its scope is surrounded by {} brackets.

Variables are defined by their type followed by a space followed by the variable name and can be initiated at their declaration as you're used to.

Lines end in semicolons.

Example
public class MyJavaObject
{
int myAge = 100;
String myName = "Bob";
}

That public keyword means that this class is publically available for use by the rest of your code, as opposed to the private keyword which is the opposite.

Not only is everything a class in Java, but there is TONS and TONS and a GAZILLION MORE TONS of objects that come with Java that do everything under the son. Lots of them are inherited from others (lots of deep hierarchy) and also make great use of other objects. I know this paragraph probably makes no sense without an example, so here's another one.

System is a built in object, and there are lots of input/output handling objects, and System has an attribute called "in" and one called "out" which are your standard input and standard output streams. System.out has a print() method and a println() method (println() just adds a newline to the end of the print so you don't have to do so yourself, print() doesn't automatically go to the next line when it's done)
public class MyJavaObject
{
int myAge = 100;
String myName = "Bob";

void whoAreYou()
{
System.out.println("I'm " + myName + ", and I'm " + myAge + " years old.");
}
}

Now, given everything I've said, I still haven't told you how to start your program. Every program has an entry-point class which has to have a main() method. Your program starts at its main().
public class MyEntryPoint
{
public static void main(String args[])
{
System.out.println("Welcome to my program!");
}
}

All that program will do is to print the welcome message to the end user.

Generally, different classes are contained in different files with the java extention.
// MyEntryPoint.java

public class MyEntryPoint
{
public static void main(String args[])
{
MyJavaObject myObj = new MyJavaObject();

myObj.whoAreYou();
}
}

// MyJavaObject.java

public class MyJavaObject
{
int myAge = 100;
String myName = "Bob";

void whoAreYou()
{
System.out.println("I'm " + myName + ", and I'm " + myAge + " years old.");
}
}

Run that and your program will output "I'm Bob, and I'm 100 years old." and then it will be over.

Now that I've explained enough to get you started, you'll probably want to know about the programming environment.

Java's development tools, the JDK, comes with command-line tools. That's what I prefer to use, as it's really easy and convenient if you get used to it. In case you don't know about using a command prompt, in Windows you can open one up by going to "Start Menu -> Run" and telling it to execute cmd.exe (On older systems, if I recall correctly, I think it might have been command.exe instead of cmd).

Once you get to the command line, javac.exe is the tool to compile your java files. However, to use it (properly) from the command line, you have to make sure that your system %path% variable equals the path to the JDK's "bin" directory so it knows where to find the Java stuff.

I'll let you look that part up on google for the sake of saving space here. It's not that difficult.

I don't recall there being any other setup to use java properly, but I could be mistaken.

javac.exe is the compilation tool

After you have a command prompt up, the following is what you could type in to compile the above two code example files.
javac MyEntryPoint.java MyJavaObject.java

If there's any errors, it will output them to you. If not, it won't output anything and just compile successfully.

Compiling the files will create a new file for every class, each of these files will have .class extention, and these are what Java uses to run the program. java.exe runs the program, you supply it the entry point class name as an argument.

The following is what you'd type in to compile and then run the program example.
javac MyEntryPoint.java MyJavaObject.java

java MyEntryPoint

And then it would show the output right under that.

You can also package your entire program into a .jar file using java's jar.exe utility, but I won't go into that since it's not required, and I never bother to do so, not even for the programs I make for work.

Now, if you can get all that to work, there's your intro into Java. Now if you want to do anything specific you want to be able to do, you can just Google it. In fact, Java has lots of great documentation, some of the best I've seen, and Google finds lots of great information and the top result is usually sufficient for whatever you need.

For example, Google "java InputStreamReader" literally gives you a link to full documentation on Java's InputStreamReader as the first hit. Just suffix any of the search stuff with "example" and you'll often get the official "Java trails" documentation on whatever you're interested in that show how to use stuff.

And I just remembered that I didn't show how to use any of the extra packages Java has. import keyword sucks in extra classes you need. For example, Java has a Random class that you can use to generate random numbers.
import java.util.Random;

public class RandomExample
{
public static void main(String args[])
{
Random randObj = new Random();

for(int index = 0; index < 10; index += 1)
{
System.out.println("Random integer: " + randObj.nextInt());
}
}
}

And there's your super fast and super short Java intro. I'll reply to this post with any more information if I think of anything else that would be good to shove in your face quickly. ;)
In response to Loduwijk (#4)
Jeez that was a short summary... ;)

Awesome Loduwijk, that was really really helpful. I got the book "Thinking in Java" by Bruce Eckel (which apparently is THE book for Java... and was free =P) - and had a brief flick through it. It's good and all, but that summary you did for me explained as much (if not more) than the first 100 pages of Eckel's book.

Thanks man. I have a two hour programming lab session tomorrow morning, so I'll come back with a pile of question after that ;)

Oh, one quick question. We had a lecture on casting (before I even knew how to start a program =P) and the lecturer said something about making a Dog that Extended and Object, then putting that Dog into a list of Objects (thus making the Dog an Object) and then taking the Dog out and having to cast it back as a Dog. My question is; if the Dog had a load of variable set that an Object doesn't have, will the Dog lose all its set values for those variables after the above process?

Thanks again, I really appreciate all this help!

~Ease~

<Edit>And for reference, I know DM above average, C fairly average and C++ okay.</edit>
In response to Ease (#5)
It will not lose them but you cannot access them through the reference.

George Gough
In response to KodeNerd (#6)
Thanks man! I thought it'd be something like that. BYOND is such a great place; I reckon I could bet £100 that you wouldn't find this level of advice and support on any other forum, and win.

~Ease~
In response to Ease (#7)
You lose those £100.

Gamedev.net is a really good community with a similar community to this one.

George Gough

[Note]
Dude, I got the symbol right on the first try, woo!
In response to KodeNerd (#8)
He he, well I was going to say about how BYOND (and its DM) taught me such a solid base of design philosophy as well. Does GameDev supply its own language/IDE that's been completely moulded to help newbies? Those two combined things, makes BYOND the best for me... and if I'd said that in my previous post, I'm fairly sure I could keep that £100 =P

~Ease~
In response to Ease (#5)
(edit)
I just reread this post and saw that I made some typos. Also, I had started writing some of the code examples one way then changed, but there were still a couple things I forgot to change which made some of it messed up. It's fixed now.
(/edit)

Ease wrote:
Oh, one quick question. We had a lecture on casting (before I even knew how to start a program =P) and the lecturer said something about making a Dog that Extended and Object,

That's the same thing as what you'd do in DM with objects. /movable is a subtype of /atom, /mob and /obj are subtypes of /movable, and you can make a /pc or a /player, /npc, /monster or whatever that are subtypes of /mob. This is the same thing.

class Animal
{
}

class Dog extends Animal
{
}

class Cat extends Animal
{
}

class Siamese extends Cat
{
}

etc.

then putting that Dog into a list of Objects (thus making the Dog an Object)

Remember that many other languages are strongly typed. Byond isn't, but you can still do something similar to Byond and have the same problem.

var/mob/M = new /mob
M.health = 10
var/atom/A = M // A now references the same object...
A.health = 5 // but it's forgotten it's a mob, and this will error

In Java, lists are type-casted. By default, it's to Object, which is the base object type in Java, similar to /datum in Byond. So everything in the list has to be of that type.

That said, you can still go back and forth with inherited objects.

ArrayList myAnimals = new ArrayList();
Dog fido = new Dog();
fido.bark();
myAnimals.add(fido);
// now I want to get fido to bark, but while it's still in the list!
(Dog)myAnimals.get(0).bark();

You have to type cast it with "(Dog)" so that it knows you're getting a dog, because myAnimals only understands basic Object class.

However, you could have done...
ArrayList<Dog> myDogs = new ArrayList<Dog>();
myDogs.add(fido);
//now you can do whatever, even use dogs from the list itself...
myDogs.get(0).bark();
// you can do stuff like in the above line because Java is a strongly typed language, and
// it knows what's in that list, so you can call the object's bark() straight from there.

My question is; if the Dog had a load of variable set that an Object doesn't have, will the Dog lose all its set values for those variables after the above process?

If you keep track of it with an Object type variable, it will forget it has those variables and you can't access them, but that's the same in Byond. If you want to access its other stuff, either type cast it or use a variable of the appropriate type to reference it.
In response to Loduwijk (#10)
Good explanation though late.

George Gough