Sunday, March 20, 2016

Basics of Java programming language

As we are already aware of the Java's benefits and introduction , now we can start implementing it ..
If you have not read the introduction and benefits of Java then click here to read it first:
"Introduction to Java".
So lets get started.In this tutorial we will learn about some basics of Java programming language.
Before proceeding further , keep in mind that Java is a case sensitive language . It treats "a" and "A" as two different variables which means we cannot use "A" in place of "a".
Here are the few important and commonly used terms , commands (Syntax) , keywords and their role.



Common terms used in Java:

*  Package :
              It is the collection of Classes.
*  Class:
              It is collection of functions ( methods).
*  Methods:
             
 Methods are the functions that we want to perform.For example if we want to print something then "Print" is a method here.
*   Keywords:
             
 Keywords are the reserved words in Java language.You can neither change  keywords nor their case.We have to use them as it is.For example "System" is a keyword in Java, we cannot write it as "system" because of the fact that Java is a case sensitive language.

*  Identifiers:
               
 Identifiers are the variables which are not already defined in Java language. For example "Test" is an identifier (variable) but "print" , "System" ,"String" etc cannot be used as identifier.The reason is that we cannot use a predefined word as identifier.

Most commonly used keywords and commands in Java:
1) import:
This is the most commonly used word in Java. This is a keyword ( also known as reserved word ) and you can neither change this word nor its case. It is used to import a package. 
For example: import java.util.Scanner;


2) For displaying something:
System.out.print("Anything that you want to print");

The above command is used to show display of anything written between the parenthesis.
For example in present case it will show :
Anything that you want to print

 3) Terminator:
 Semi-colons are known as terminator in Java language because they are used to terminate(end) any command.
For example:

System.out.print("Terminators are semi-colons");

After the above command we used semicolons which terminates it.If we don't use that at the end of the command then it will not end that command and will consider next piece of code as a part of the previous one.So it is necessary to terminate a command before starting a new one.
4) Main:
Main is a method which is used to differentiate between an Application and an Applet.
If we use "main" in start of our code then it means that the code is 
executable( i.e it can run on windows in offline mode too (not necessarily) ) .But if we don't use it then it becomes an applet and it can run only in websites.

5) Scanner:

Scanner is a class which is used when we want to take input from user. If we want to take input from user then we need to create an object first which can be created by following command:
Scanner input=new Scanner(System.in);
Here we created an object with name "input" .You can rename it anything that you want .

6)Data Types:

Data type means the type of data that we are dealing with.For example if we want to take input from user which is in integer form then it is known as Integer Data Type.
The most commonly used Data Types are:

*  Int : used to take input in the form of integer.
* double: used to take input in the form of fractions.
* String: used to take input in the form of string (anything can be written in the string).


7)Storing input into a variable:

When we take input from user we need to store it in a variable so that we can use it further when we are required to do so .
It can be done by the following command:
variable=input.nextInt();
This will store the input into variable. We used the word variable during our code . You can name it anything that you want, but make sure you use the same name in the complete code to avoid any trouble.
Here input is the object name that we created in step 5.You will have to use the object name here that you created above.Otherwise the code will not be compiled.

8) If , Else & Else If conditions:


 "if" is used at the start from where we enter our first condition.If the condition becomes true then the part of the code that is present in the "if " section will be executed.
Otherwise it will go to the else part and execute it.
But if we have more than one condition then we enter our 1st condition in "if " section and the 2nd condition in "else if " section and so on.

9) Signs (operators ) used in Java:


We don't enter signs in Java as we do in Algebra.For example in Algebra we use "=" to show a value equal to the other but in Java we need to use "==" to show that.

For example in Algebra we write , a=b .
While during coding we write:
if (a==b)
System.out.print("A is equal to B");
 similarly , "||" is used to represent "or" , "!=" is used for "Not equal to " etc.

10) Curly brackets {} :
Curly brackets are mostly used when we need to enter more than one statement in a given class , condition , method etc.
It is necessary to use a curly bracket at the end if we have used it at the start otherwise code will not be compiled.

Now i hope that after reading all this , you are aware of the Basics of Java programming language .
If you like this , then please share it with your friends.
Keep visiting . Keep learning.

Credits: Picture by ShutterStock

YouTube Video :

0 comments:

Post a Comment