Hello, welcome back we are here with the next article of Get Started with Java Programming. Here we would be talking about Data Type, Operations, Decision Statement and loops. Java supports boolean data types as a primitive data type.

Data Type

Data type specify the the size and type of the values to be stored in the variable. Java have lots of data types which allow programmers to select appropriate data type they needed. In Java we can categorise data types into "Primitive types" and "Derived types". We will be discussing about build-in types(Primitive).

Integer Types

Integer can hold whole numbers. Java supports four type of integers, bytes, short, int, long. The size of the values that can be stored in a variables depends upon the type of integer. In the table below we specify Types size and value range:-
Type Size Minimum Value Maximun value
bytes One byte -128 127
short Two bytes -32768 32767
int Four bytes -2147483648 2147483647
long Eight bytes -9223372036854775808 9223372036854775807

Floating Point Types

floating point types can hold number combination fractional numbers. In Java there are two types of floating point integer float and double. In the table below range and size of these two types are specified. Floating points supports a special value know as Not-a-Number(NaN). NaN is used to represent the result of operation as dividing by zero. Float are of 4 bytes whereas double are of 8 bytes.

Other Types:

char:- Char is used to store character value in Java program.  This type is of 2 bytes but can hold only one single character.
boolean: In Java boolean is accepted as a primitive data type. Whenever we have to test a condition during the execution of the program this can be used. There are only two possible values of boolean type:- true or false. This use only one bit of storage.

Operators

A operation is symbol that tells the computer to perform certain mathematical or logical operations. we can classified into a numbers of categories:-
Arithmetic:- Used to conduct mathematical expressions as in algebra. +, -, *, /, % We can use Arithmetic operations on any build in data type except boolean type.
Lets Learn more about % operator:- the % operator will gives the remainder of the number. a % b will give the remender number (the remember while dividing a by b). The symbol of the value of the % will be based on the numerator.
10 % 3 == 1
10 % -3 == 1
-10 % 3 == -1
-10 % -3 == -1

Logical Operator:- && (and), ||(Or), !(Not). Logical operator will yield a value either true or false.
Var1 Var1 Var1! Var1 && Var1 Var1 || Var1
ture ture false ture ture
ture false false false ture
false ture ture false ture
false false ture false false

Relational Operator:- > , < , >= , <= , != Relation operator is used to compare two quantities. We can use these operation on selecting some task whenever some condition is satisfied.
Assignments Operator:- = , -= , += , *= , /= , %=
a +=b is similar to a = a + b
Equality:- == To show that thow of them are equal.
Unary:- ++ , -- Used for increment or decrement A = A + 1 is similar to A++
Ternary (conditional):- ?: Used as in if else condition. (Expression)?(Do this if true):(else do this);

Decision Statements

Decision statement are to make decision the the basic of the condition whether it is true or false. Java supports two discussion statements.
1. if statement
2. switch statement

if statement

if statement is used in decision making during the execution of the program. The if statement take of the following form:
if (condition)
{
    statements;
}
If the condition is true the statement will be executed else it will be skipped. Depending upon the complexity of the program if can takes various form as below:
  • if statement
  • if .. else statement
  • nested if else statement
  • else if statements 

Switch Statements

In switch statement statements are executed upon the required case are true. Switch case takes the following form.
switch (case){
case 1:
statement1;
case2:
statement2;
}
The program will check for the value of the variable case if it matches 1 statement1 will be executed if 2 statement2. we can define the default option in switch case too in case non case satisfied the statement in the default will be executed.

We can use any data types in if statements and any expression are accepted in if statements. But we can only make equality comparison on switch and only accept integer, char and Strings (allowed by Java SE 7).

Looping in Java

Java supports all the three looping namely while, do while and for loop. Loops are used when we need to execute certain part of the program repitately. Whenever a loop check the condition while entering in the loop such loop is known as entry control loop. When a loop checks the condition at exit such looping is known as exit control loop. While and for loop are entry control loop where as do while is exit control loop. We can set certain number of turns or we can set till some conditions happens.
Stack is a data structure, which is not provided by any programming language as fundamentals data type. The last element inserted will be on top of the stack. Since deletion is done from same end, last element inserted will be the first element to be removed out from the stack and so on.

A stack is ordered collection of homogeneous data elements where the insertion and deletion operations only occurs at one end. The end is often known as Top Of the Stack (TOS).

As stack is not provided by any programming language we need to create our own stak. Use of control + Z (undo) is done through the use of Stack, Reversing of the string and checking the validation and matching the parentheses in programming language in compiler are done using programming language.

How Stack Works?

Stack is a one ended operation system. There are only two operations permitted in stack, Push and pop. Push to insert in the stack where as pop to delete the element form the stack. The TOS (Top Of the Stack) always point to the top position where the next can be popped off. We can add another element on the (TOS + 1).

When ever we try to push to the stack when the stack is full then we get the error which is known as Stack Overflow. Whenever we try to pop out from the empty stack then we get an error which is known as stack underflow.

When the stack is empty TOS = -1 When we insert the first element the TOS will be on 0. When we add another element again the TOS will be 1 and so on. So when new element is inserted the TOS will be incremented. When we pop the element the TOS will reduced by one. Let say TOS is on 5 now. when we pop a element, New TOS will be 4 then when we pop again the TOS will be 3 and so on. As we will get an error when we try push to the stack which is full and try to pop from the empty stack so we should always check whether we can pop and push or not.

Stack Implementation

we can implement stack using these two ways:
Static implementation: Using array
Dynamic Implementation: Using linked list

Static Implementation

Here we use array to realize the principle of stack. In array we can insert the data and access the data. Here we use the array in a such a way that we will insert the element in the TOS and access the element from TOS. So by default when the stack is empty TOS is -1. (As array begins with 0, we make TOS to -1 for easiness). Then we insert the element in the TOS+1 location of the array. So we pass element to be added, array and the TOS when ever we want to insert data in the Stack. And in the same way we will remove a data from the stack and reduce the TOS by one, thus we pass array and TOS during pop.

Algorithm:
#define MAX=5
push(s[MAX],element,TOS)
  if TOS == MAX - 1
    display "Stack is full"
  else
    TOS = TOS+1
    s[TOS] = element
  return;

pop(s[MAX],TOS)
  if TOS == -1
    display "Stack is Empty"
    return 0
  else
    element = s[TOS]
    TOS = TOS -1
    return element

Application of Stack

It is used in recursive function calling. Function calling and returing process are in reverse order.
It is used to reverse a string.
It is used to evalutate and check the correction ness of the mathematical operation.
Implementation by word processors to perform UNDO REDO operations.
Used in parenthesis matching by compilers.

Details of the examples

How Parenthesis matching by compiler using Stack?

In every programming language there must be even numbers of parenthesis in the program. Here we will discuss how stack helps on parenthesis matching. So when we open a parenthesis the program will save that on the stack and search for next parenthesis. If it find another opening parenthesis it will add that on the stack again. And again keep looking for the parenthesis. When a closing parenthesis is found it will remove the pair (Opening and Closing) together and keep looking for parenthesis. If at the end The stack is empty there is not error in parenthesis else of the stack is not empty, there must error in parenthesis.

Stack for UNDO and REDO

Every actions will be stored in stack. so when we undo it the last command will be removed. if undo again the second last command and third last and going on..... In the same way the poped commands will be stored in another stack in case of REDO. so when we press redo we will get the last undo and the undo then after and then after.
We will be discussing about data structure. You should have knowledge in basic programming. We will be using C and Java for some example programming. Here we would discuss about Array, Structure, Pointer and Class. These are the topics we will be using throughout this article series in Data Structures.

Array

Array is primitive data member provided by C programming. Array is the collection of similar data types. Memory allocation of the array is sequential. ie. Array are stored in contiguous block of memory. Array only supports similar data type. Strings are the array of character with \0 at the end. \0 = NULL

Structure

Structure is the collection of different type of data type. In C programming Structure is the only way to create complex data type. Structure is the heterogeneous collection of data. Theoretically it will not occupy any memory unless we create the variable of that Structure. Memory allocation  of structure is sequential. We can access members of array with dot operation(.) and Arrow operation(->). Dot operation is used to access memory of structure variables. and Arrow operation is used to access members from Pointer of the structure.

1
2
3
4
5
6
struct Person
{
    char name[30];
    int age;
    char address[40];
}

Self Referencial Structure

The structure that is pointing to self is called self referential structure. Ie a member of structure is pointer to that structure
struct list

1
2
3
4
5
struct List
{
    int age;
    struct List *next;
}

Union

It is similar as of the structure. The only difference is that union only occupy the memory of the largest member data. So we car use only one data of the union at a time. Can there be pointer to union? yes they can be :)

Courses to Complete During Software Engineering

Pointer

What is pointer in a word? A pointer is a variable. A pointer is a reference variable which store memory location. All the pointer occupy same memory bits. The pointer of the floating point data and the integer data. will occupy only 2 bits of memory. What is Void Pointer and null pointer? Void pointer is the pointer that can point to any kind of data type. This make use of type casting to to this job. Null pointer os the pointer which points to non or invalid address. Remember Structure pointer can only point to structure.

float data type is of 4 bit of memory, so when pointer points to the floating point it will point the base address of the variable. Points to the starting block of the memory for that variable.

Class

A class is data type. class is user defined variable that can be used to create objects. The difference between the structure and the class is that class have methods (Behaviour) which structure does not have. When ever the object are created the members are copied to the variables but not the methods. All the objects share the same methods. What is Abstract class? The class which have pure virtual function and cannot make any object.

Abstract means hiding the implementation details. We Drive the car but it is no concern to use how all the functions in the car works.
Java is a general-purpose, Object Oriented Programming Language high level programming language. It was developed by Sun Microsystem in 1991. Originally James Gosling, Mike Sheridian, Patrick Naughton start a project named "Green Project". They were developing special software for consumer electronic devices. They name this new programming language as "Oak". The World Wide Web appeared, then they develop a web browser called "HotJava" using the new programming language they developed, which demonstrate the Power the Programming language and it become popular among the internet. They Rename "Oak" into "Java" in 1995.

To run a java code in any machine we need JRE (Java Runtime Environment). JVM is a component of JRE. JVM(Java Virtual Machine). All programming language compilers translate source code into machine code for special computer. However Java first compile the java Source code into a bytecode for a the JVM. Then java interpreter generate the machine specific code. This is why java is Platform Independent. Any code written and compiled in Linux machine could be interpreted and run on Windows machine.

A java source file Hello.java is first compiled using compiler (Javac) which will generate a bytecode Hello.class then Java Interpret(Java) will gives the output. JDK(Java Development Kit)is used for java program development. JRE will be automatically installed when JDK is installed in the system. In Windows machine we need to set environmental variables to run Java or Javac command in Command Prompt. For that Right Click in My Computer and Click properties. Then Click in Environmental Variables. Add "path" as a name and <Location of java installation> in value field in System Variables. The location should be like "c:\program files\java\jdk\bin"

We set up the Environment verialbes now we are ready to go to Run our First Program

1
2
3
4
5
6
7
8
9
class Hello
{
        public static void main(String args[])
        {
                System.out.print("Hello ");
                System.out.println("Java ");
                System.out.println("Welcome to Java");
        }
}

Java contents Packages with any prederined functions and class to use while programming in java. Packages are those refered as Class Libararies in other programming language. While Defining Identifier. Always use UpperCase first character of class and every first Letter of next word without any space. for Functtion name use first lowercase and next word's first character as a UpperCase.
Identifier Examples:-
Class Name:- Hello, HelloJava, WelcomeToJavaProgramming
Method name:- display(), displayMessage(), displayNewMail()

In the above program we use String and System class from Package java.lang We should alway import the package name before using them in our program. But java.lang is a basic package which will be automaticlly inserted.

import java.lang.String will import only that class where as import java.lang.* will import all the classes in that Package. It is better to use .* while during studying purpose but for development we will impor individual package as required.

As java is purely object oriented programming language, we need to create at least one class to run the java program. The main function is automatically called when executing the program. ie the execution of the program starts from the main function. Main function is Public, can be accessed by outer function, Static because no object is created to call the main function. void as it do not return anything. The main function is called using the class name as Hello.main()

String args[] are known as Command line arguments. We could pass arguments to the main function from command line.
Check: Java, General-purpose Programming Language

Prerequisites:- Basic Knowledge of Object Oriented Programming.