Java

First step to Java


Java is a programing language developed by James Gosling at Sun Microsystems. Currently it is a widely using language. Well… there are reasons that made it so important.

  • Easy to learn (now be happy :))
  • An open source (no need to pay for coding or using)
  • A cross-platform language (no matter you use Windows, Linux, Mac etc.)
  • Ability of creating web applications
  • Object-oriented
  • Secure and reliable

and many more…

So why wasting time.. Let’s begin to learn Java. First you need the followings.

  • JDK (Java Development Kit)
    You must have JDK to develop in Java. It gives the environment and the components to create Java applications.
  • IDE – integrated development environment (optional. But Java life becomes sooooo easy with an IDE.)
    Eclipse, InteliJIdea and Netbeans are some of the IDEs you can use. Auto-completing syntax, show errors on the spot and a lot of advantages are there.

The flow of a Java program


  1. Write the code in Java to create the source file ( .java file)
    > Run the source code in a compiler. Until the source code is right, compiler will show errors.
  2. Compiler will create the java byte code (.class file). This bytecode is platform independent
    > Java Virtual Machine (JVM), a software installed in your device will read the java byte code and gives your program as output.
  3. Finally your device is ready to run a java program.

Compiler vs JVM

Compiler  turns the source code into byte code or machine code (but normally to a byte code) and JVM turns the byte code into the working program.

Well, let’s see why compiler turns the code to byte code instead of machine code directly.

The machine language depends on the processor and to give the same output source code may have to be turned into different machine codes. Instead of that different JVMs can be installed depending on the processor and same source code can be turned into a working program.


Program structure


Source file contains a source code. In java it has the extension, .java (e.g. : name.java). There can be multiple classes in one file; but only one public class.

Class is an individual object and it contains one or methods.

Methods describe the behaviors of a class. It contains one or more statements.

Statements are the instructions of a program.


A simple Java program


public class Hello{
	public static void main (String arg[]){
		// Print “Hello World!”
		System.out.println(“Hello World!”);
        }
}

Here you can see that the blocks are bounded using curly braces “{ }” and at the end of every statement there is a semicolon “;”.


How to instruct?


Just like the real life java also has ways of giving instructions…

  • Declarations/ assignments (“This is my sandwich!”)
  • Loops (“Keep reading the book until you find what you want to know”)
  • Conditions (“if you are under 12 you can get this, 13-17 this, 18-20 this is suitable, 21 + probably this…”)

Declarations/ assignments

String name = “Sush”; // wherever you use the word name that means “sush”
int x = 21; // wherever you use the letter x that means 21
System.out.println(“Hi”);  // print “Hi”

Before going in to loops and conditions you have to know the following comparison operators.

  • > greater than
  • < less than
  • == equal (Yes, there are two equal marks)
  • <= greater than or equal >= less than or equal

Loops

There are three structures of Java loops: while, do-while, for. Loop has a condition and statements that should be repeated. When writing conditions, comparison operators should be used. Let’s take a while loop as an example.

int x=0;

System.out.println(“Before the loop x is ” + x);

While(x < 4){
   // Code will run for 4 times.
   System.out.println(“Inside the loop x is ” + x);
   x = x + 1;  // If you don’t put this, code will loop forever make a disaster.
}

System.out.println(“After the loop x is ” + x);

As long as x is less than 4, the code inside the curly braces will run. So the output is,

Before the loop x is 0
Inside the loop x is 0
Inside the loop x is 1
Inside the loop x is 2
Inside the loop x is 3
After the loop x is 4

It is that much simple.

Conditions

In the while loop we said, “As long as x is less than 4…”; but here it’s “if x is less than 4…”. Same structure like while loop, but statements inside the curly braces don’t get repeated.

int x=0;
System.out.println(“Before the condition x is ” + x);

if(x<4) {
   // Code runs only if x is less than 4
   System.out.println(“Inside the condition x is ” + x);
   x=x+1;
} 
System.out.println(“After the condition x is ” + x);

The output is,
Before the condition x is 0
Inside the condition x is 0
After the condition x is 1

‘Else’ can be used to enhance the condition. That means “if x is less than 4 do this, else do that…”

int x=0;
System.out.println(“Before the condition x is ” + x);
if(x<4) {
   // Code runs only if x is less than 4
   System.out.println(“Inside the condition x is ” + x);
   x=x+1;
} else {
   System.out.print(“x is not less than 4”); 
}
System.out.println(“After the condition x is ” + x);

The output is,
Before the condition x is 0
Inside the condition x is 0
After the condition x is 1


Today’s Lesson


So finally you know the basics of Java. Let’s write a small program using everything you learnt so far. (Here I am not using an IDE)

1. Open a text editor (Notepad, Gedit etc.)

2. Type the following code.

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

   	String name = "Sheldon";
   	int score = 99;

   	if (score >70){
		System.out.println (name + " passed the exam!");
  	} else {
		System.out.println("Sorry, you have to work hard " + name);
  	}

  	String neighbour = "Penny";
  	int pairsOfShoes =10;

   	while(pairsOfShoes <= 13){
			System.out.println("Penny got " + pairsOfShoes + " pairs of shoes");
			pairsOfShoes = pairsOfShoes + 1;
   	}
   }
}

3. Save as “Register.java”

4. Open command promt/ terminal and go to the path where you saved the file “Register.java”.
(Mine it is C:\Users\Hydrogirl\Downloads)

5. Type “javac Register.java”

6. If there are no errors,  type “java Register”

7. You will see the output 🙂

Advertisements

So.. What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s