SRL Archive - Java Help and Tutorials

Making your first Java Program.


ZaSz

2010-01-20 20:51:41

Zasz's Making your first Java Program Tut.

Getting an IDE


Well, if you want to make a java program, I would 100% recommend an IDE, because it will correct your syntax and everything.

The IDE I will be working with is Eclipse, and I will use it throughout the tutorial, as it is an excellent IDE in my opinion.

You can get Eclipse here. I personally would recommend the Eclipse IDE for Java EE Developers (189 MB), but if your only aiming for standard development, go with the Eclipse IDE for Java Developers (92 MB).

Creating the source file.


Right click in the projects window, and click new java project. (see picture)


Then use the drop down arrow on the new project to expand the project. Then right click on the src folder and click make a new class file. (Pic)


A popup will come up, and all you need to do is fill out the Name field and click finish, you don't have to mess with package or any of that other stuff.


Making a Hello World.

Lets say I name the class file test. In the main section, test.java will popup with

public class test {

}
in the debug box.

The public class test is like Program asdasd; in scar. Except that Class (name) must match the file name. So if it was
public class me
it would have to be saved as me.java

Now we will create the main procedure that will be called when the program is ran. All we have to do is add.

public class test {
public static void main(String[] args) {
}
}


Main will be called as soon as we run the program. We could easily just make it write out hello world right from that procedure, but I want to show you how to do a few different kinds of procedures.



public class test {

public static int hi(){ // int means we have to return an integer.
System.out.println("Finding 50+50");
return 50+50; // Will return 100. You can just put 100, but Im saying you can use equations for the return.
}

public static boolean hi(String see){ // boolean means we would have to return an boolean. We can also have
//Duplicate names of procedures if the parameters are different (So they can do different names)
if(see=="yessss"){
System.out.println("Finding if 50+50 = 100!");
return (50+50)==100; // Could've said true
} else {
return (20+60)==100; // Could've said false. But an equation example
}
}

public static void main(String[] args) {
System.out.println("Hello, Lets see an int."); // This is like a println from scar. But we are calling from System.out
//Println is a lot like scar. You don't have to use "" for non strings.
/* Using the /* will block
out anything after until you close it. Much like // except if you
want to block out large lines of code.
*/
System.out.println(1); // Will write out the #1.
System.out.println(hi()); // Will write out 100
System.out.println(hi("yessss")); // Will write out true;
System.out.println(hi("blah")); // Will write out false;
WriteDebug("We can make a shorter system.out.println!");
WriteDebug("Hello World!");
}

public static void WriteDebug(String message) { //Procedure names are case sensitive also!
//Also, it doesn't matter where the procedure is in the program compared to the main procedure.
System.out.println(message);
}

}


Save your first program. (ctr-s = fastest)

Running your first program!


The most easiest and quickest way to do this is by batch.

First find your project directory, then find your source file.

Right click in the folder. And make a new text document.
Paste this code into the document.

@ECHO OFF

ECHO Looking for JDK
cls

SET KEY_NAME=HKLM\SOFTWARE\JavaSoft\Java Development Kit
FOR /F "tokens=3" %%A IN ('REG QUERY "%KEY_NAME%" /v CurrentVersion 2^>NUL') DO SET jdkv=%%A
SET jdk=

IF DEFINED jdkv (
FOR /F "skip=2 tokens=3,4" %%A IN ('REG QUERY "%KEY_NAME%\%jdkv%" /v JavaHome 2^>NUL') DO SET jdk=%%A %%B
) ELSE (
FOR /F "tokens=*" %%G IN ('DIR /B "%ProgramFiles%\Java\jdk*"') DO SET jdk=%%G
)

SET jdk=%jdk%\bin
SET javac="%jdk%\javac.exe"

IF NOT EXIST %javac% (
javac -version 2>NUL
IF "%ERRORLEVEL%" NEQ "0" GOTO :notfound
) ELSE (
GOTO :setpath
)
GOTO :eof

:notfound
ECHO JDK is not installed, please download and install it from:
ECHO http://java.sun.com/javase/downloads
ECHO.
PAUSE

:setpath
SET PATH=%jdk%;%PATH%
javac test.java
pause
cls
java test
pause
GOTO :eof

The only thing you need to edit is at javac at the end, edit that to your programs java name. Then edit java test to java "your programs name without the .java"

Go to file-save as- Then name it something like "Compile Test.bat" - Then change Save as type : Text Documents (.txt) to all files.

Click save. Now your program is ready to run! Just double click the .bat to see it in action!


Maybe my next will include gui?

Method

2010-01-20 20:57:21

The code in this tutorial is all over the place. Some things that could use 'fixing':

  • Class names should be capitalized.
  • Strings should be compared using the equals method of the String class, not ==.
  • The casing on your WriteDebug method should be fixed.
  • Also, if you're developing this in an IDE, why are you running the class with a batch file?

mrpickle

2010-01-20 21:15:19

Erm… teach using a more simple program? I mean, this is for first-timers. :)

Eclipse or netbeans will confuse the heck out of first timers… so many buttons, and too many options from drop down menu.

To teach, use something more simple. Ready To Program-Holts (its crappy… but does not scare newbies).

HyperSecret

2010-01-21 09:22:14

Also you might want to explain a little about overloading. I know you have a little explanation of it commented but that really doesn't make any sense. No one knows (from reading this) that it is called overloading and what it means.

n3ss3s

2010-01-21 13:29:46

The most easiest and quickest way to do this is by batch.


Not with your batch, thats for sure… A Java programmer needs to have his java and javac put to the environment variables anyways, so all you need to do to compile a .java to a .class is javac File.java, and to run java Class.

ZaSz

2010-01-21 13:55:33

Thanks for mostly positive input. Yeah, I might edit later how to set up the system environment variables so all they would need is the javac + java. Also will edit later with if testing just running with the IDE, but I was kinda making it so you can use it on the go.

And I'll also edit with some of the other suggestions.

arash

2010-01-25 18:25:48

Eclipse has one of the most advanced compilers(incorrect usage but w/e), and console's. Why even bother using a batch?