Common features of programming language

Common 7 features of programming language

Common features of programming language:

 

Let’s try to understand the Common features of programming language basics of any programing language before moving to Java:

Below are the common/basic futures of any programming language:

  1. Data Types
  2. Variables
  3. Operators
  4. Conditional Statements
  5. Control Statements (loops)
  6. Arrays
  7. Function / Methods

Data Types:

Data types are used to allocate memory and specify what type of data you are using in the program.

All the data types are keywords which are having built-in meaning. Below are the few data types in Java.

  1. int: which is used to store integer value and it will not accept any decimal value. It allocates 4 bytes of memory.
  2. long: which is used to store integer value and it will not accept any decimal value. It allocates 8 bytes of memory
  3. float: which is used to store decimal value. It allocates 4 bytes of memory
  4. double: which is used to store decimal value. It allocates 8 bytes of memory
  5. char: which is used to store a single character and it uses to take only one byte.
  6. Short: which use to take 2 bytes of memory and stores an integer value.
  7. Boolean: which takes only true or false
  8. Byte: which takes only byte memory

 

String: is not a data type but it is a predefined class. It allows storing sets of characters.

Variables: Variables are nothing but in which data is stored. It is nothing but a memory location name by which you can refer your data.

Declaration and initialization of a variable

datatypevariablename=value

Ex:int a=10;

int b=20

int c=a+b

Rules to follow for declaring the variable name:

  1. Should start with lower case and the first letter of the second word should start with a capital letter. i.e. first name, orderNumber
  2. Can start with _ (underscore)
  3. Can start with $
  4. The first letter cannot be a capital letter
  5. It can contain numbers but not in the middle
  6. No other special symbols allowed

Example of data types and using variables:

public class JavaDataTypes {

 

public static void main(String[] args) {

inta,b,c;

a=10;

b=20;

c=a+b;

System.out.println(“Addition of a and b is ” +c);

 

}

}

Operators:

  1. Arithmetic:
    • +
    • *
    • /
    • % (modules) which gives remainder.
  2. Relation Operator:
    • >
    • <
    • >=
    • <=
    • !=

3. Equality Operator / comparison:

    • == which checks for equality

4. Concatenation Operator:

    • +: is used to value with string or string with value
      Ex: String a=”Ashok”int b=10a+b=Ashok10b+a=10Ashok

Logical Operators

  • &&(And operator)
  • ||(OR operator)
  • ! (logical not operator)

Conditional statements: are used to execute a set of statements based on condition satisfaction.

  1. if
  2. else
  3. else if
  4. nested if

 

if: Here we write one condition and execute a set of statements based on the condition. Statements under if will be executed only if the condition is true.

Syn: if(condition)

{

statements;

}

This block of statements will be executed only if the condition is true.

if-else: It’s the same as if but we will one additional else. If we understand that it will execute only if the condition is true but we do not have any else statement. Here if the condition is false then it will go and execute else statements. For else we don’t need to give any condition and it will execute only if the condition is false

syn: if(condition)

{

statements;

}

else

{

statements;

}

 

else-if: Using if and else we are able to check for two conditions only. Let’s take an example you want to write a program which should display whether student is pass or fail based on marks given. So here using if else we can display just pass or fail. But what if I want to write a program where it should display student division based on marks provided like First division, second division and just pass. So here we are checking for multiple conditions one for first class, another one for second class and for just pass. We can achieve this using else-if.

Syntax: if(condition)

{

statements;

}

else if(condition)

{

statements;

}

else

{

statements;

}

 

Nested if: writing if inside if

 

Switch:

Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int.

Syn:

Switch(expression)

{

case“a”:

statements;

break;

case“b”:

statements;

break;

default:

statements;

}

Control statements (loops):

Are used to execute a set of statements as per a defined number of times. All the programming languages have the below 3 types of loops.

  1. while
  2. for
  3. do-while

While Loop:

Syntax:

initialization;

while (condition)

{

statements;

incrimination / decrementation of number

}

Example:

int i=1;

while(i<=10)

{

statements;

i++ OR  i=i+1;

}

From above code statements inside the loop will be repeated for 10 times.

You can implement an infinite loop using the while statement as follows:

boolean b=true;

while (b){

// your code goes here

if(login==)

b=false

}

Note: You need to make sure make the b value set to false to avoid an infinite loop

For Loop:

Syntax:

for(initialization; condition; incrimination / decrementation of number)

{

statements;

}

Example:

For(int i=1; i<=10;i++)

{

Statements;

}

Do While:

Syntax:

do

{

statements;

incrimination / decrementation of number

}while(condition);

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once

Example:

do

{

Statements;

incrimination / decrementation of number

}while(i<=10);

Best Manual Software Testing Online Training

Our Trainer Profile 

Arrays: Arrays are nothing but a set of similar elements stored in a single variable.

If we take a normal variable (int x;) it will take/store only one value at a time and we cannot store more than one value.

Let’s say I would like to write a program where I want to store student rule numbers for college in which there are 1000 students available. If I use a normal variable I have to declare a thousand variables like below:

int rolno1=1;

int rolno2=2;

int rolno3=3

And it goes on till

int rolno1000=1000;

Here you are spending more time declaring variables rather than writing an actual programs. So to avoid this we can go for using arrays where you can declare only one array variable and store 1000 values in this variable.

Declaration of Arrays:

Data type arrayname []

Example: int rolno[];

Allocating Memory for Arrays:

int rolno[] = new int[1000]

From the above line, we are declaring rolno variable as an array and we specifying size as 1000 which means rolno variable can be used to store 1000 values in one array variable.

We are using “new” here new is a keyword which basically used to allocate the memory. We will discuss “new” keyword in the coming topics in detail.

Initializing/assigning the values for Arrays:

In the above, we understand that we have allocated the size of the array as 1000 and we can store 1000 integer values in the variable called rolno.

Let’s assign values for the array: arrays index always starts with zero

int rolno[] = new int[1000];

rolno[0]=1;

rolno[1]=2;

rolno[2]=3;

rolno[3]=4;

And it goes on….. Till

rolno[999]=1000;

Since arrays start with indexing zero last value will be stored always size – 1 which is 1000-1= 999.

Note: You need to always use loops with arrays to retrieve the values of an array.

Methods / Functions:

The method is a set or group of statements that can be re-used to repeat any number of times. Methods are written for a specific task/purpose.

We have two types of functions/methods:

  1. Pre-defined: Which are already defined and we going to use them. Example main method.
  2. User-defined: Which are going to be created by ourselfs.

Methods have the following:

  1. Method definition (here you write the code for which purpose method is written)
  2. Method calling (when you call a method it goes to method definition and executes code that is written in the definition)

Calling of Method: We need to call a method using object name dot method name. Example obj.method1();

The method can take parameters and also it can return a value but it can return at a time only one value.

Let’s take an example of a method:

public class Functions {

public static void main(String[] args) {
// TODO Auto-generated method stub
Functions obj=new Functions();
int sum=obj.add(10,20);// function calling
System.out.println(“Addition of two numbers are ” +sum);
System.out.println(“After function calling”);
sum=obj.add(30,40);
System.out.println(“Addition of two numbers are ” +sum);
}
//function definition
int add(int x, int y)
{
int result=x+y;
return(result);

}
}

Code Explanation:

Here we have written a method called “add” which is adding two integer numbers.

int add(int x, int y): This method is taking two parameters of type two integers and adding them, and returning a value.

int sum=obj.add(10,20): Here we are calling the method by passing two parameters as 10, 20. These parameters will be stored in variables called ‘x’, ‘y’ and after adding these two numbers are storing results in a variable called ‘results’. Once the results are calculated we are returning the value of the results. When the function is executed then it will go to the same line from where it is called and store the value of the results in a variable called sum.

Leave a Comment

Your email address will not be published. Required fields are marked *

18 Years Experienced Trainer