Tuesday, February 16, 2016

Comments and Expression

Comments

Comments in programming languages are the values ignore by the compiler. Usually comments are used in the programming language for documentation. Suppose you write the code for the some specific piece of work and use in your current project, after 3 months you need that same function again you could remember that code is somewhere but you need to read all the logics and identified the desired code. If you have code in this code you can take that code from that place.
There are two type of comments
  • one line comments
  • multiline comments

Let see how to implements the comments in Java code
Single line comments

 // This is single line comments  
 public class Main {  
  // main method it is entry point for the java program  
   public static void main(String []args){  
    // Display the output on the console  
    System.out.println("Hello World");  
  }  
 }  

Multiline comments

 /* This is multiline comments  
   This class will use for the main method  
 */  
 public class Main {  
  /*  
    main method it is entry point for the java program  
    what you want to write here!!!  
  */  
   public static void main(String []args){  
    // Display the output on the console  
    System.out.println("Hello World");  
  }  
 }  

Expressions

In this lesson we will learn about the expressions in Java. Expression is combination of the values, variables, constants and operators. Let say x is a variables and has a value 4. If we say
y = x + 3
is expression.

 public class MyClass {  
   public static void main(String []args){  
    // expression  
    int x = 4;  
    // expression  
    int y = 7;  
    // expression  
    int answer = x + y;  
    // output  
    System.out.println(answer);  
  }  
 }  

 /*  
  This is expression example  
 */   
 public class MyClass {   
   public static void main(String []args){   
   // This is string value   
   String x = "Hello";   
   // expression   
   String y = "World";   
   // expression   
   String answer = x + y;   
   // output   
   System.out.println(answer);   
  }   
  }   


Monday, February 15, 2016

Keywords

KeyWords

There are some specific words are reserved for the compiler. Every compiler has some keywords. In Java every keyword always be small. There are 50 keywords in Java, because keywords are special for the compiler we cannot use these words for our purpose i.e. variables name, constant name and any other identifier we will learn in future.

  • abstract
  • assert
  • boolean
  • break
  • byte
  • case
  • catch
  • char
  • class
  • const
  • continue
  • default
  • do
  • double
  • else
  • enum
  • extends
  • final
  • finally
  • float
  • for
  • goto
  • if
  • int
  • interface
  • long
  • native
  • new 
  • package
  • private
  • protected
  • public
  • return
  • short
  • static
  • strictfp
  • super
  • switch
  • synchronized
  • this
  • throw
  • throws
  • transient
  • try
  • void
  • volatile
  • while
  • false
  • null
  • true
Just go through in those words you become expert when you will use in your program.

Variables AND Constant Implementation

Variables and Constant Implementation

Now we are aware of the variables and constant. Now lets have a look in real Java code.
Follow the steps:

  • Create a new project in Eclipse
  • Named it "VariableAndConstant"
  • Create a Class
  • Enter the code in this class
  •  public class Main {  
        public static void main(String []args){  
          int var = 3;  
          System.out.println(var);  
          var = 5;  
          System.out.println(var);  
          System.out.println(var);  
        }  
     }  
    
  • right click on project and run it.
The output should be

3
5 
5 


It is all because you set 3 in beginning and then you change it as 5 and use it two times.

Now it is your turn to change the variable and print. Create another and print it.

Now we are going to write second program for the constant tricks.
Create another project and create a class Main in it.
Write the following code.

 public class Main {  
    public static void main(String []args){  
      final int constant = 17;  
      System.out.println(constant);  
      System.out.println(constant);  
      System.out.println(constant);  
    }  
 }  

The output will be


17
17 
17 


Now try to change the value of the constant you find error. The final create some special effect to tell the compiler it is a constant.


Monday, February 8, 2016

Data Types

Variables And Constant

If you have already know other computer languages, you must be familiar with variables and constant. If you have a primitive knowledge of mathematics then again you are familiar with these terms. If you don't know about that don't worry we will go the necessary information of the variables and constant. 

Why we write software?
You may say to help the business, improve productivity, easiness, etc. Yes you are right but behind all the things we calculate data and want to get desired results. 

In the computer programming the data is the important. Suppose you have a very big program which calculate the whole year tax with lot of programming instructions but it gives you wrong results, it is useless. Or, if you have a game in computer but it is calculating wrong distance from the enemies, it must annoying you because of the wrong data calculation.

So, the data is everything we want from the computer. If you will learn the data manipulation efficiently and accurately in the computer programming then you are called expert programmer.

The data is represent in computer programming in Variables and Constant. Lets see what are the variables and constant. 

Variables

All the data in computer is reside in memory for the computer programs. I am not going to the memory details. Just consider memory is a location in your cupboard. If you put something in it and want me to bring this, you have to tell me the address in the cupboard. Same thing is variable. You can put your data in memory and keep its address in a readable format. Suppose you have a space in memory named x has value 2 but when you finished your work with 2 get rid of ir and save another value in the same place named x.

Constants

Constants are same but it contain only one fixed value during the program execution. It is very useful but for the time we are not going to discussed it. I want you to remember that the location name and value cannot change in constant. You better to understand when we use it.

Now we know the basics concept of variables and constant. Now suppose you have different size of cupboard in your room and you want to put your stuff in it, I am sure you use the appropriate size for the things to save the space. Suppose you have 6" X 6" one box and the other one is 18" X 18". Now you want to keep your small cup in your cupboard, suppose your cup is 4" X 3", where you will keep it? Any of the empty space but now you have a very big15" X 10" book, you must keep your cup in small box and book in big box. It is logical, isn't it? Same thing we apply in programming languages. The space is called data types and what you can save in it is called literals.

Data Types

If you want to save small data you must use small data types. We have different data types with different literals. Lets discuss it one at a time.

Integer

This data type has whole number values. The number could be minus, zero or plus but cannot contain decimal places. All the whole numbers are literals of the integer. But in Java you get further division to keep your memory efficient. But how much for small space you need small number.

byte

It is 8 bit data type and the literal value range is -128 to 1274

short

It is 16 bit data type the literal value range is -32768 to 32767

int 

This is the most common used data type for the whole numbers. It takes 32 bits and its literal value range is -231 to 231-1

long

It takes 64 bits and its literal value range is -263 to 263-1

Tuesday, February 2, 2016

First Program


Prepare First Program

What is the flow of a Java program?

To make it easy for the noob I am writing the 3 simple steps. Don't worry about the details, when we finish the basic we dive in deep ocean of the Java knowledge.
  • Write the source code in Java programming language
  • compile it, you get a *.class file
  • run the compiled file
  • Remember; Java language is case sensitive. Its mean variable is not equal to Variable.
NB: If you know the term byte code forget about it for the time

How to write a Java program?

Open eclipse IDE and do the following steps:
  1. Got to "File>New>Project...". You will see a dialoug box.

  2.  Choos Java>Java Project from the dialog box.
  3. Click "Next" Button.
  4. Give any name to your project. In my case "HelloWorld" without spacing.
  5. Click "Finish" Button
  6. If ask for the perspective just select yes.
Your project is here.
Notice there is small arrow before the Project Name. It will expend the content of the project.
It has two things
  1. src Forlder
  2. JRE SYSTEM Library

src Folder

It contains all the source code. It is our space we create Java program files and put in this folder. 

JRE SYSTEM Library

It has the core Java libraries. Which are common and necessary for writing the basic Java program. It is provided by the Oracle JDK. We will see in depth these libraries in future.

Create Source File 

We write the Java program in a text file. This file is called source file, although it is a text file but we use its extension *.java
Follow the steps to create a source file in Eclipse IDE:
  1. Right click on HelloWorld project in left pane.
  2. It will show you a menu select
    1. New > Class
  3. In the new dialogue type the name of the any name of the class, in my case "HelloMain" without spacing and quotes.
  4. Now you will see the "default package" under the src folder. default package contains the file you just create.
  5. Type the following code 
    1. 1:  public class HelloMain {  
      2:    
      3:       public static void main(String args[]){  
      4:            System.out.println("Hello World");  
      5:       }  
      6:  }  
  6. You will notice that HelloMain tab in right pane become "*HelloMain". It shows that your file is not saved. Save the file from File menu.
You have complete your first code. Congratulations!!!

Compile and Run

In Eclipse IDE it is very easy to compile and Run the Java program.
Follow these steps:
  1. Select "HelloWorld" project.
  2. Right click on it and find "Run As".
  3. From the sub menu of "Run As" click "Java Application".
  4. You will see the output in console menu.

Monday, February 1, 2016

Getting Start

Getting Start

The most important thing to learn Java as beginner is installation of Java. We will start learning Java from installation to core Java concept. For installation you should aware what are the term related to Java installation. We wont dive deep in these term for this post. I will explain more when it required. For now just read and keep in mind the following terms.

  • JRE
  • JVM
  • JDK

JRE

JRE stands for "Java Runtime Environment". It is a container for the Java Virtual Machine. It contains all necessary files, which help to run JVM. In easy way it is a set of libraries which helps to run JVM.

JVM

JVM stands for "Java Virtual Machine". It is a process which helps to run the Java program. Every Java program run under a virtual machine. If virtual machine is crashed its mean your application crashed. 

JDK

JDK stands for "Java Development Kit". It has the compiler and core libraries to aid the computer programmers. It has many other useful tools we will learn in futures posts.

According to the definition of those term we need JRE to run JVM and JDK to write the code and compile it. In next post I will discuss what is compile code. Now focus on Java installation.

Follow the steps:
  • Download from here.
  • Look for Java SE
  • It takes you here.
  • Download first JDK.







I assume you are using Windows x64 Operating system. Download the JDK related to your OS and double click on it. It start installation wizard. Follow the instruction and when it finish it ask you for the JRE installation, install it as well.

NB: JDK contains development kit as well as JRE.

IDE

IDE stands for "Integrated Development Environment". What does it mean? Its mean all the tools for the development are in one software. The tools IDE provides, are:
  • Editor; where you can write your code
  • Compiler executer; where you can use JDK compiler
  • intellisense; suggest the Java vocabulary.
  • Debugger; explain in future post.
IDE provides us everything in one place rather we move on different software program to write code in Notepad and compile it in command line. There are many IDEs but we follow one of them:

How to install Eclipse

Download eclipse from here. Extract it in your favourite location and open its folder. You find eclipse.exe file there. Double click this file and eclipse IDE is ready.

How to install Netbeans
Downloads Netbeans from here. Double click on the downloaded file and follow the instruction. Install custom IDE features. When it finishes, go to the start menu and type netbeans run the installed file.

NB: IDE installation must after the JDK installation.