Sunday, 20 September 2015

Hello World & Exercises

Hi,

Verify Java

I hope you have successfully installed Java and eclipse and set up you environment.
To check if you have done that correctly open Command Prompt,

  1. Type java -version and verify that you get the version information of java installed on your machine.
  2. Check below image.


Getting familiar with eclipse

Check below image,
  1. This is the perspective that is open right now in eclipse. We need Java for this tutorial.
  2. This is Project explorer, where you can see all the projects and files within the project in a hierarchical fashion. 
  3. This is main window where you will see opened files and write you code.
  4. This is a list of views which will be handy later soon.
  5. Status of what eclipse is doing right now.


OK, so now we are good to go.

Create Hello World!

  1. Click on File -> New -> Java Project
  2. Type MyFirstJavaProject and Click Finish button at the bottom.
  3. Check below image

  1. Click small arrow symbol as shown in below image.
  2. Right Click on src folder and select New -> Package.
  3. Type your package name as myCode.
  4. Click Finish button at the bottom.


  1. Right click on myCode package that we just created.
  2. Select New -> Class.
  3. Type your class name as HelloWorld.
  4. Click Finish button at the bottom.


  1. Congrats, now we will write some code to make our class do something for us.
  2. For now we want it to print Hello World!
  3. Double Click on HelloWorld.java file that we have just created.
  4. You will see it has some code as shown below.
Old Code:

package myCode;
public class HelloWorld {
}

New Code:

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

I will explain the code later but for now just write it and run it.

  1. To run the code right click on the class file you want to run and select Run As -> Java Application.
  2. A console view will show up with output as Hello World!
  3. Voila, you have done it.


Some Exercises for you

  1. Print your name instead of Hello World.
  2. Print your friends' or siblings name.
  3. Print your favorite quote or mantra.

Keep smiling & keep reading...

Amit Kumar

No comments:

Post a Comment