Java

Learning JAVA with Yasser

Getting Started
NetBeans
Java Comments
The Structure of Java Code
Printing to the Output Window
Sharing your Java programs


You need to install NetBeans. This is one of the most popular IDEs (Interface Development Environment) in the world for writing Java programmes. then  you need Java components and files. First of all is something called the Java Virtual Machine.

The Java Virtual Machine

Java is platform independent. This means that it will run on any operating system because of the Java Virtual Machine. The Virtual Machine is a program that processes all your code correctly. So you need to install this program (Virtual Machine) before you can run any Java code.
You can check to see if you already have the JRE on your computer by clicking the link "Do I have Java?".
and here is the link of manual.
After downloading and installing, you may need to restart your computer. When you do, you will have the Java Virtual Machine.

The Java Software Development Kit

At this stage, you still can't write any programs. The only thing you've done is to install software so that Java programs can be run on your computer. To write code and test it out, you need something called a Software Development kit.
Java's Software Development Kit can currently be downloaded from here:
The one we're going to be using is called Java SE. (The SE stands for Standard Edition.). Click on that link in the Top Downloads on the right. You'll then find yourself on a page with a bewildering list of options to download. Because we're going to be using NetBeans, locate this:
JDK 8 with NetBeans
Click the Download link to be taken to yet another page. Locate the link for your operating system We're going to be using NetBeans to write our code. Before launching the software, however, here's how things work in the world of Java.

How things work in Java

You write the actual code for your programs in a text editor. (In NetBeans, there's a special area for you to write code.) The code is called source code, and is saved with the file extension .java. A program called Javac is then used to turn the source code into Java Byte Code. This is known as compiling. After Javac has finished compiling the Java Byte Code, it creates a new file with the extension .class. (At least, it does if no errors are detected.) Once the class file has been created, it can be run on the Java Virtual Machine. So:
  • Create source code with the extension .java
  • Use Javac to create (compile) a file ending in .class
  • Run the compiled class
NetBeans handles all the creating and compiling for you. Behind the scenes, though, it takes your sources code and creates the java file. It will launch Javac and compile the class file. NetBeans can then run your program inside its own software. This saves you the hassle of opening up a terminal window and typing long strings of commands.


NetBeans


To start a new project, click on File > New Project from the NetBeans menu at the top. You'll see the following dialogue box appear:

select Java under Categories, and then Java Application under Projects. Click the Next button.
In the Project Name area at the top, type a Name for your Project. Notice how the text at the bottom changes to match your project name (in the text box to the right of Create Main Class):
firstproject.FirstProject
The Class created will be called FirstProject, with a capital "F", capital "P". The package is also called firstproject, but with a lowercase "f" and lowercase "j".
The default location to save your projects appears in the Project Location text box. Click the Finish button and NetBeans will create all the necessary files for you.
When NetBeans returns you to the IDE, have a look at the Projects area in the top left of the screen (if you can't see this, click Window > Projects from the menu bar at the top of the software):


the compiler demands that the source file and the class name match each other (mean the same name).

Java Comments

//This is a single line comment
/*
This is a comment spreading
over two lines or more
*/
There's also something called a Javadoc comment. You can see two of these in the coding image on the previous page. A Javadoc comment starts with a single forward slash and two asterisks (/**) and ends with an asterisk and one slash ( */ ). Each line of the comment starts with one asterisk:
/**
*
This is a Javadoc comment 
*/
Javadoc comments are used to document code. The documented code can then be turned into an HTML page that will be helpful to others. You can see what these look like by clicking Run from the menu at the top of NetBeans. From the Run menu, select Generate Javadoc. There's not much to see, however, as you haven't written any code yet!

At this stage of your programming career, you can delete the comments that NetBeans generates for you. Here's our code again with the comments deleted:
package firstproject;

public class Firstproject {


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

The Structure of Java Code

You can think of a class as a code segment. But you have to tell Java where code segments start and end. You do this with curly brackets. The start of a code segment is done with a left curly bracket { and is ended with a right curly bracket }. Anything inside of the left and right curly brackets belong to that code segment.
What's inside of the left and right curly brackets for the class is another code segment. This one:
public static void main( String[ ] args ) {

}
The word "main. When a Java program starts, it looks for a method called main. (A method is just a chunk of code. then it executes any code within the curly brackets for main. You'll get error messages if you don't have a main method in your Java programs. But as its name suggest, it is the main entry point for your programs.

public means that the method can be seen outside of this class; static means that you don't have to create a new object; and void means it doesn't return a value - it just gets on with it. The parts between the round brackets of main are something called command line arguments.


Printing to the Output Window

package firstproject;
public class Firstproject {
    public static void main(String[] args) {
        System.out.println("Hello World");
    } 

}



Sharing your Java programs

You can send your programs to other people so that they can run them. To do that, you need to create a JAR file (Java Archive). NetBeans can do all this for you. From the Run menu at the top, selectClean and Build project(project name) Project.

When you do, NetBeans saves your work and then creates all the necessary files. It will create a folder called dist and place all the files in there. Have a look in the place where your NetBeans projects are and you'll see the dist folder:

No comments:

Post a Comment