Learn Java: Getting your hands dirty with this language!

learnjavaUpdated by Pratiksha Amit Sharma On February 26, 2014

Learning java can give you headaches if you are a beginner. Why? Because before starting to learn Java programming, you need to prepare your machine. You need to install everything you need for java programming, making it suitable for coding in Java language.  But don’t you worry…we will arm you with all the tools you need to get started, including this renowned Ultimate Java Tutorial for Beginners.

First off, some introductions. Java is a high-level, object-oriented programming language developed by Sun Microsystems. Learning Java has its incentives, as it’s a powerful language for developing desktop applications, web applications, and many smart devices run on Java. Java is also a platform-independent programming language, highly portable. If your computer runs on Windows, Linux or Mac OS, they all are same for Java because it runs on virtual machine.

javastackChecklist before you start coding

So, first things first – before writing your first code in Java, you need to install what is called as Java virtual machine (JVM), also known as Java Runtime Environment (JRE). JRE can be downloaded from this link:

http://java.com/en/download/index.jsp

One that’s complete, you will have JVM installed on your PC. That allows Java programme to run on your machine. To write and test the Java codes, you need to install Java’s software development kit (JDK). The JDK can be downloaded from this link:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Here, you have a bewildering list of options to download from. Look for Java SE (Standard Edition). One of the links on this webpage will enable you to download JDK and NetBeans. More explanation on NetBeans in a bit. Install the JDK on your machine, and be sure that you are downloading all the software appropriate to your Operating System. You should be clear about whether you have a 32-bit or a 64-bit OS.

What’s the next step? How do you compile and run a Java programme?

Before moving on to the next step i.e., IDEs, let’s talk about some nitty-gritties of how Java programme works. You always start writing codes in a text editor (you will find in-built text editors with IDEs: NetBeans, Eclipse, or Jcreator), called the source code which is saved with a file extension .java. The java compiler (Javac) turns the source code into a class file with extension .class. Once you have the class file, it can be run on JVM. Now to make this process hassle free, IDEs come to rescue.

Now, what’s the next step in java learning?

Let’s get your hands dirty with this IDE thing. The IDEs (Interface Development Environment) take care of all the creating and compiling of jobs for you behind the scenes. They takes your code, create the java file, launch compiler to arrive at class file, and let you run your programme. Here is a list of some quality IDEs like:

  1. Eclipse – a free, popular program. (Learn Java Programming Using Eclipse)
  2. Netbeans – another free program; it is open source and available in many languages.
  3. Jcreator – A for-pay program that provides a bit more power than most other IDEs.

As described in this Java course, once you install your IDE, you will need to begin a new file to program in.  Exactly how this is done will depend on your particular IDE.

Brief intro of How to start using IDEs for Java coding

1.      Eclipse

Download and install the eclipse. Once you initialize the eclipse, it will ask for workspace. You may use the default one, or may specify the desired path. All the files generated during java programming will be stored in this workspace. When you have the Eclise interface window open, go to ‘File,’ and then click ‘New Java Project’. The “Create a Java Project” dialogue box appears as shown below:

java1

Give name to your project for example: FirstProject.

Click next, then click Finish.  Then, right-click on the Project folder in the upper left, hover over ‘New,’ then click ‘Class.’  Name your Class anything you want, such as ‘firstproject’. Now look for the box that has Eclipse ‘create the main method,’ and make sure that this is checked. The New Java Class dialogue box opens up:

java2

The workspace will be created by Eclipse for you to write codes. A snapshot of the Eclipse workspace will look like this:

java3

**Start Learning Java Now With This Training Course From Udemy**

2.      NetBeans

Download and install the Netbeans. With your first run, the screen will look like this:

java4

To start a new project, click on File then New Project.  The following dialogue box appears:

java5

Select Java under Categories and Java Application under Projects. Once you click on Next we have a New Java Application dialogue box:

java6

In the project name area, type the name of your project, and in the Create Main Class box name the class with the .Main extension.  In the above example, we have a project “FirstProject” and with class as “firstproject.Main”, click Finish and NetBeans will go to work and prepare the workspace for us, with an in-built text editor for us to write our codes. A screen shot of the work space of the NetBeans is provided below:

java7

Walking Before You Run – Hello World!

The ‘Hello World!” program is a classic training program often used to help new students learn Java and other languages. You can learn about it in this Java training course .

We will use Eclipse IDE for learning java programming in this tutorial. Once you have created your first Class (in Eclipse IDE), you should be looking at a text-editor type screen with some codes already written for you. You will see the words ‘public class firstproject’ and under that, the words ‘public static void’ with some words after that.  This second group of words is known as the ‘main method’, and it is what we will be focusing on. The entire structure will look like:

public class firstproject {
       /**
       * @param args
       */
       public static void main(String[] args) {
               // TODO Auto-generated method stub
       }
}

You might be thinking, what the heck is going on? Some purple lines and curly brackets {}, what are they and why they are used?

Okay, so you have some line of codes already written on the text editor by your IDE. Let’s check more about the lines above.

What are Comments?

When the programme runs, the comments are ignored. That means you can write anything in the comment section. Use comments whenever possible, as they are the easiest way to communicate to others about what your code does. Suppose you are working in a team environment and you are distributing your code for further work to some other team member –  the comments will help your teammates understand the thinking process that you went through in writing that piece of code. They are especially important when you will re-visit your programme codes for updates or modifications in the future. The comments will tell you your exact logic and code at the time.

The comments are enclosed in between: ‘/*’ before starting the comment and ‘*/’ at the end of your comment. For example:

/* your comments starts here…..

The logic behind your code….

Your comment ends ….. */

A special note on single line commenting.

You can insert the single line comment using ‘//’. For example:

// This is a single line comment

The above multi-line comment can be re-written using the single line commenting style.

Example:

// your comments starts here…..

// The logic behind your code….

// your comment ends …

A brief note about the Javadoc comments. A Javadoc comment starts a with ‘/**’ (forward slash followed by two asterisks) and ends with a ‘*/’. For example:

/** This is a Javadoc comment */

The skeleton of your programme

‘public class firstproject{} is a code segment (more about the classes later). You should have taken a note of the ‘{ }’ bracket symbols. The start of the code segment is done with the left ‘{‘curly bracket and the end of the code segment is followed by the right ‘}’ curly bracket. Anything inside the ‘{}’ belong to that code segment.

Inside the code segment of the class we have another code segment:

public static void main(String[] args) {
                // TODO Auto-generated method stub
        }

You will be typing your code in between these curly brackets. Look at the word “main”; it’s very important. Whenever a Java programme starts, it looks for the method “main()”. A method is some piece of code. And “main()” is a special method or code segment with its own ‘{}’, and it serves as the entry point of the Java programme.

You will be curious about the entries before the word “main”: public static void. To learn about them, you need to go through the course. That is a pretty big topic in itself. In brief, “public” means that the method “main()” can be called outside the class in which it is defined; “static” means that you don’t have to create a new object; “void” states that your method “main()” is not returning any value; the round brackets ‘()’ after main contain command line arguments.

In nutshell, you have a class – firstproject with its main() method.

Running your First Programme – “Hello World”

Inside the curly brackets of the main method, insert this tiny piece of the code -

‘System.out.println(“Hello World!”);’     /* This is known as a print statement. Write your code without the Apostrophe (‘) */

Run the programme by clicking the Run in Run Menu or by using ctrl + F11.

You will see the output of the program in the box at the bottom of the screen.  If everything went right, it should say “Hello World!”  Congratulations!  You’ve just created your first Java program, and are on your way to making your own applications and web codes.  Be sure to save your program.

Dissecting your first program

Before jumping ahead, let us discuss some important concepts. You would be wondering about the nitty-gritties of your little and very simple code that you just typed “System.out.println(“some string”)”. You surely would want to know what the heck is this “System” followed by some period or dot (.) then “out” which is again followed by a period and then some method “println()”?

And you’re right, let’s dissect this little piece of code.

‘System’ is a built-in class present in java.lang package. The System class contains several useful class fields and methods. It cannot be instantiated. Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array (according to Javadocs).

As System class is contained in the package java.lang, and since java.lang package is imported in every java program by default, you therefore need not to import java.lang package. Otherwise, you need to import packages explicitly before using them. In fact, java.lang is the only package in Java API which does not require an import declaration.

‘out’ represents output stream (i.e Command window) and is the static data member of the class System. Thus, System.out denotes the System as the class & out as the static object.

‘.println(“string”)’  is method  of out object that takes the text string as an argument and displays it to the standard output i.e on monitor screen.

Your little programme will cause the computer to print out whatever words or symbols are there between the “ “ quotation marks and within the parentheses ‘()’ to console.

A little warning!

You need to remember to include the semi-colon at the end – this is the signal that this particular line of code is done.

As always, the basics are the most important, but you don’t have to be content with just the basics. Continue to learn Java for more advanced tasks like sorting, searching, and network programming with Advanced Java Programming classes on Udemy.

For more comprehensive training, try out some of these tutorials:

Comments

  1. hiiiiiiiiiii

  2. I realy love these site.

  3. Really help full. thanks :)

  4. studying…..

  5. Hmmm.,.,.A gud learning site.,.,

  6. learn java

  7. nice course..but wiziq is providing a much better java course at a much lesser price…

  8. very usefull

  9. i just started to study the Java

  10. Simply Superb Admin and the post was really interesting while on the Search I saw Smart Mind Online Training course on <a href="http://smartmindonlinetraining.com/adv-java-online-training/"&gt; Advance Java Online Training </a> with less price and +15 real time faculty ,live project,job support.

    India : +91 9949599844
    US : + 1-347-606-2716
    Email:contact@smartmindonlinetraining.com

Speak Your Mind

*

Email