26 Jan 2017

What is for-each loop in java..??

You know about the loop concept in java if you don't know then click me ...๐Ÿ˜Ž๐Ÿ˜Ž

Now come to the point about the for-each loop.This loop is specially designed to handle the elements of a collection . Collection is the group of  elements. You know the array part of java .we can take an array as a collection of similar elements like Integer or String etc., or a class is a collection of similar methods or a package like java.util, is a collection of classes and interfaces .


What is a collection..?

A collection represents a group of elements like integer values or objects or methods classes .Examples for collections are arrays and java.util classes (Stack, LinkedList etc. ).


How can you explain it ..!!

The for-each loop repeatedly execute a group of statements for each element of the collection. It execute as many times as there are number of elements in the collection.

Lets assume an example but first know the syntax of that loop :

Syntax:

for(var: collection) //here the var is a variable attached to a collection .
{
statements;
}


here var represents each element of the collection one by one.Suppose If a collection has 5 element then the loop will execute 5 times and the var represents these elements one by one .

Lets take an example of for-each loop:

class mobile
{public static void main(String ar[])

{
int a[]={5,35,69,96,75}; // declare an integer type array with five elements.

for(int i : a)  //use for-each loop for retrieve the elements in array
{System.out.println(i);}
}
}


Output :
5
35
69
96
75

for more examples of  for-each loop click me....๐Ÿ˜Š๐Ÿ˜Š


What is for loop..??                   What is while loop..??

What is do-while loop..??              What is control statement..??

What is if-else statement..??           What is switch statement..??













,

What is control statements ..??

First of all the main question arises about the statements  that is  "What is a statements in a program ".

The answer is "It is smallest  unit of a program that is a complete instruction in itself. " Statements of java generally contain  expressions a end with a semi-colon (;). The two most commonly used statements in any programming language are as follows:


Sequential statements : These are the statements which are executed one by one .

Control statements : These are the statements that are executed randomly and repeatedly .
These are followed by java also.

Now lets assume an example :

System.out.println("Hello Jarvis");
a=b+c;
System.out.println("a");

So what you think , what type of statement it is

I tell  you that  is the example of Sequential statements as you can see that the execution of statements are in sequence .

What is control statements?

Control statements are the statements which alter the flow of execution and provide better control to the programmer on the flow of execution . They are useful to write better and complex programs.


These are the following control statements are available in java:


  1. if-else statement
  2. do-while loop
  3. while loop
  4. for loop 
  5. for-each loop 
  6. switch statement 
  7. break statement 
  8. continue statement
  9. return statement 





















What is do-while loop..??

This loop is used when there is a need to repeatedly execute a group of statements as long as condition is true.If the condition is false the repetition will be stopped and the execution  comes out of the loop.

So the syntax of do-while loop :

do
{
statements;
}while(condition);

Lets take an example of

class mobile
{public static void main(String ar[])

{int x=1;
do{System.out.println(x);
x++;
}
while(x<=10);
}}

Output:
1
2
3
4
5
6
7
8
9
10

while loop is also called as entry restricted ,similarly the do-while loop is also know as entry level loop.

The question arises what is the differences  between while and do-while loop..??

The difference is that in do while loop , the loop must execute at first time if the condition false or true but in while loop if the condition is false then the JVM comes out to the loop ..


Lets take as example of do-while loop in which the while condition is false :


class mobile
{public static void main(String ar[])

{int x=1;
do{System.out.println(x);
x++;
}
while(x>=10);
}}

When you execute this program ,the loop will execute first time but after that it checks the condition which is false by me so the loop will exit.


 for more examples of do-while loop  click me .....๐Ÿ˜„๐Ÿ˜„


What is while loop ..??           What is for loop..??

What is switch statement..??   What is control statements..??











What is while loop..??

As you know that , loop is the executing of set of statement in multiple time. The while loop is same as for loop but the syntax is little difference. The while loop is also called entry restricted loop.

As you know that , all the loop statements are under the control statements, so lets checkout the syntax of while loop:

initialization;

while (condition)
{
statements;
}

increment;

Lets take an example of while loop :

class mobile
{public static void main(String ar[])
{int a=1;
   while(a<=10)
{
System.out.println(a); // you can also use increment direct in print() method .
}
a++; // increment
}
}

Output :
1
2
3
4
5
6
7
8
9
10



Now the question arises why while loop called as entry restricted..??

Because the entry in loop is possible when the condition is true otherwise you cannot enter in the loop that is why while loop is also called entry restricted loop.

Lets take some other Examples:

class mobile
{public static void main(String ar[])
{int a=1;
   while(a<=10)
{
System.out.println(a++); //increment passes in the println() method
}
}
}

Output is same as above appear.

class mobile
{public static void main(String ar[])
{int a=1;
   while(a<=10)
{
System.out.println(++a);
}
}
}

Output:
2
3
4
5
6
7
8
9
10
11


for more examples of while click me...๐Ÿ˜„๐Ÿ˜„

What is for loop..??                      What is do-while loop..??

What is switch statement..??        What is JVM..??




















What is method in java??

Method is also called as function in C/C++, but there are many differences between java and c++.

Method: A method is a group of statements or collection of statements are grouped to perform actions as well as operations. As you know that main method is also a method in java program that is called by the JVM.

A method can be called by two ways :


  1. The first way is to creating object of class that belongs to the method .
  2. The second  way is , by declaring as static method .

To know about the static keyword click me..๐Ÿ˜„๐Ÿ˜„


Syntax of a method :

public static int methodname(int a )
{
// This is the body to perform action as you like
}

The syntax contains:

 public static: This portion called as modifier , so the question arises what is modifier.

int: It is return type to return some value.

methodname: methodname is the name to call it by using it name.

int a : It is the parameter of method .

and the last thing is the body of that method to perform different type of actions.

you can say that :

modifier returntype methodname (parameter)
{//body}

So lets take an example of method:

class number
{
public static void main(String as[])
{
System.out.println("This is main");
show();
}

public static void show()    //I used static keyword otherwise it will throw error.
{System.out.println("This is show");}

}

Output :
This is main
This is show


If you compile this program you will get an error because static member based on rules

"A non-static method cannot be referenced from static context"

So you must use static keyword other wise you will use that method by creating object of that class 
like :




class number
{
public static void main(String as[])
{
System.out.println("This is main");
number m=new number();
m.show();  //calling method by creating object of that class
}

public static void show()    //I used static keyword otherwise it will throw error.
{System.out.println("This is show method");}

}

The output is same as above appear , this is the second way to call method by using object of class.


What is Static Keyword..??               What is object..??

Why we use Static Keyword..??       What is class..??

Method Examples..!!                 What is loop..??

What is for loop..??             What is while and do-while loop..??

How many Types of loop...??





















24 Jan 2017

Examples of for loop..!

Lets take an example of for loop:

class remote
{public static void main(String b[])
{
   for(int a=0; a<=10; a++)
{
   System.out.println(a);
}
}
}

If you execute this program you will show that the numbers for 0 to 10 and if the condition will false the body will terminate.Now let's some changes in above example print statement out of loop :

class remote
{public static void main(String b[])
{
   for(int a=0; a<=10; a++)
{
   System.out.println(a);
}
System.out.println(a);
}
}

what is the output of this program for more examples comment me ....๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚










What is for loop in java?

First of all the question arises that, what is loop ..?   in simple way the doing same process multiple time is called loop as well in java "the execution of  a set of statements in multiple time is know as loop .  In java there are four types of loop exists :


  •  For loop
  • while loop
  • do-while loop
  • for each loop


Different types of loops have different ways to perform statement execution...๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚

So lets talk about the for loop syntax and their working process: 
 the syntax of for loop

for(initialization; condition; increment)
//statements execute if the conditions is true otherwise it escape from the loop 
{statements}

now how it works as you know about the  if-else condition with the help of boolean expression ,Similarly here the middle is condition ,  

  • first declare or initialize the value 
  •  it moves to the condition statement if the condition is true the statement is execute and then the increment part works and increase the value by one
  •  then again check the condition if true then execute the statement .
  •  JVM executes the statements under the for loop braces until the condition being false   
Lets assume an example of for loop...click me..๐Ÿ˜‰๐Ÿ˜‰



What is if-else condition statement in java..?

Hello guys guys today i will tell about the if -else condition statements

now as you know that the meaning of if in hindi (เค…เค—เคฐ ) so it works on Boolean expression under if brackets if the condition is true then the statements are execute under the curly braces other wise if the condition is false else part is execute ..

now lets study the syntax of if-else condition:


if(boolean-expression)      //if the condition is true 

//executes the statements under the braces
{statements}

//if the condition is false the else part is execute
else
{statements}


Lets take an example of if-else condition part:

class remote
{public static void main(String a[])
{int a=2;

if(a==2)   //if the condition is true
{System.out.println("You entered 2");}

//if the condition is false it execute else part
else {System.out.println("You did not entered 2");}
}}

if you execute the following program then the condition will be true because the value of variable a is 2 as in the condition if you change the value it execute the else part


click to  know deep about the for loop...
















Simple Switch Example

Lets take an example of switch statement without break; :


class Week
{
      public static void main(String a[])
     {
           int day=2;
           switch(day)
           {
               case 1: System.out.println("Monday");
               case 2: System.out.println("Tuesday");
               case 3: System.out.println("Wednesday");
               case 4: System.out.println("Thursday");
               case 5: System.out.println("Friday");
               case 6: System.out.println("Saturday");
               case 7: System.out.println("Sunday");
           }
    }
}


If you execute this program the output is starting from case 2 Tuesday  and end with Sunday
 YOU know i had share you an example of ball and stairs that if you throw a ball to stairs then it falls back to you to you with each steps if their is no blocks like breaks . So tie break statements with each case condition. and then execute this program


this example is with break;

class Week
{
      public static void main(String a[])
     {
           int day=2;
           switch(day)
           {
               case 1: System.out.println("Monday");break;
               case 2: System.out.println("Tuesday");break;
               case 3: System.out.println("Wednesday");break;
               case 4: System.out.println("Thursday");break;
               case 5: System.out.println("Friday");break;
               case 6: System.out.println("Saturday");break;
               case 7: System.out.println("Sunday");
           }
    }
}

execute it and then see the difference.


Follow me to  more learn basics with examples..๐Ÿ˜„๐Ÿ˜„๐Ÿ˜„๐Ÿ˜„๐Ÿ˜„๐Ÿ˜„




Basic Rules applied on Switch statements !!

First of all keep it in mind that java is securest language and inspired by real life .So first , lets imagine your childhood as you play with your self, as throwing a ball against stairs and it will have been come back step by step .....๐Ÿ™‚๐Ÿ™‚


  • The first rule that you keep it in mind is that every switch statements variable must be Integer, convertible Integers(byte,char,short) .

  • The break statement is used with end of case statement .If you don't use the break statement then the sub case value is execute after the true case value .

  • Default statement is must be tie in the body of switch.It is like else part if the no. of case value is not true then the default statement is executed.

Lets take an example of switch statements : Switch_Examples 



What is Switch Statement in java....?

Switch statement is the another part of condition type statements other than if-else condition statements.In technical language  "A Switch statement allows a variable to be tested for equality against a list of values.  "

The syntax of Switch statements looks like that:


switch(expression)
{
case value: //statements
//another value

case value: //statements

default: //optional statements like else part
}

Click on the  link to know about the: Rules applied on switch statements ๐ŸŒ๐ŸŒ๐ŸŒ

16 Jan 2017

How to handle Airhtmetic Exception divide by zero..??

Hello friends today i will show you how to handle Arithmetic Exception using try and catch block:


First of all , i will write a  program with Arithmetic Exception error :]
in this example i will assign three variable a, b and c. We divide a with b (0) , the int data type used to store the values .

class remote
{
    public static void main(String s [])
    {
        int a=10, b=0, c;
        c=a/b;

        System.out.println(c);
        c=a+b;

        System.out.print(c);
    }
}


 if you compile this program you cannot get any error but if you execute this program you will see the error:
     
         Exception in thread "main" java.lang.ArithmeticException: / by zero
        at remote.main(remote.java:6)



to handle this exception you need to use try catch block :
 


class remote
{
    public static void main(String s [])
    {
        int a=10, b=0, c;
        try
        {
           c=a/b;
            System.out.print(c);
        }
        catch(ArithmeticException m)
        {
       
        System.out.println("You cannot divide "+a+" by "+b);

        }
       
        c=a+b;

        System.out.print(c);
    }
}
you have solved the exception error . Now you think why you we use the exception handling case because of stop statement if we get any exception in any line , so we use this 

.

If you have any problem in this program you can comment on this blog and thank you for reading this blog...๐Ÿ˜ƒ๐Ÿ˜ƒ๐Ÿ˜ƒ๐Ÿ˜ƒ๐Ÿ˜ƒ๐Ÿ˜ƒ



















13 Jan 2017

What is Tampering and Impersonation...??

This is another problem after Eavesdropping. Tampering is the modifying  someone's message illegally after reading it. I mean to say that in Tampering not only reads the data but also modifying it is called Tampering. Encryption and Decryption is the one and only solution for stops all the illegal working on the Internet.


The third problem is Impersonation means a person hides their original identity and act as somebody else to make transactions and other things like misusing of confidential information. This is due to a person disguising(เคฐूเคช เคฌเคฆเคฒเคจा ) himself as another person on Internet is called Impersonation.

What is Eavesdropping

Eavesdropping means reading others message illegally  on Internet. For example , reading others email message is turn into this category. But there is Solution also, the encryption and decryption method is the way to protect the message from  unauthorized people .

In this method the message is converted to unreadable form from readable form.Converting the data into unreadable format is called Encryption, and getting back to original format is called Decryption.

While Encryption the data we use a key or password to secure and only known person can decrypt  it by using that key and the third person cannot open this email or message without key.Encryption and Decryption is done by Java technology.

12 Jan 2017

Why Pointers are eliminated in Java..?

There are main common Problems reasons why pointer is not used in java:

  1. Pointers are lead to confusion for a programmer.
  2. Pointers may crash a program easily, for example, when we add two pointers, the program crash immediately. The same thing could also happen when we forgot to free the memory allotted to a variable and re-allot it to some other variable. 
  3. Pointers break security. Using pointers , harmful programs like Viruses , Trojans and other hacking programs are developed.
Because of the above reason , Pointer have been avoided in Java

Waht are the Features of Java..??

The main basic features of java Every programmer must know the basic feature of java are:

Simple: Java is simple programming language rather than saying that this is one of the feature of java.When java is developed, they wanted it to be simple because it has to work on electronic devices, where less memory is available.

The question arises how Java is made simple? First of all, the difficult concept of c and c++ are omitted(เค›ोเฅœे เค—เค ) in Java. One of the concept  in C and C++ is Pointer, which is very difficult for learners and programmers, has been eliminated in Java. The second thing is Java developer maintained the same syntax as C and C++ in Java, so that a person who knows C and C++ can learn easily Java.

Object-oriented: Java is an Object-oriented programming language. This means Java programs uses Objects and classes.

Distributed: Information is distributed on various computers on a network. Using Java, we can write programs, which capture information and distribute it to the clients. This is possible because Java can handle the protocols like TCP/IP and UDP.

Robust(Strong): Java programs are strong and they don't crash easily like C or C++ program. This is due to two main reasons :

  1.            Java has excellent exception handling features.
  2.            Memory management feature 
Secure: Security problems like eavesdropping, tampering, impersonation, and virus threats can be easily destroyed . There are basic problems like :

Portability:  If a program shows same result on every different computers then that program is called Portability .

System Independence: Java's byte code is not machine dependent. It can be run on any machine , any Operating System, any Processor without any problem. That is why it is also called portable programming language.

Interpreted: Both compiler and Interpreter is used to execute program compiler is for compile the program into a class file then it is run using interpreter in JVM (Java Virtual Machine). But in other language only one compiler or interpreter is used to execute the program.

Multi-threaded: A thread represents an individual process to execute a group of statements. JVM uses several threads to execute different blocks of code.Because of multiple threaded is used in java it is called multi threaded

High Performance: The interpreter inside the JVM is very slow to execute  programs , So that is why Java Soft people have introduced JIT(Just In Time) compiler, which enhances the speed of execution.Both are working for high speed performance Interpreter and JIT.

Dynamic: Using of java we can animate the text using java applet, but before this only static text is used to be displayed in the browser. 






First Hello World Program for beginners in JAVA

First of all we  create a main class like


public class HelloWorld
 {
  public static void main(String [] arg) 
          {
      //we have to print "Hello World " in the terminal window.
  
System.out.println("Hello World");
           }
}

and save it as the class name with first capital letter as HelloWorld.java .

If we run this program the output is Hello World, to run this program open cmd in bin and type javac file_name .java and hit enter you compiled it now you can run it by typing java file_name.

11 Jan 2017

Why C/C++ is Platform Dependent or System Dependent

Today topic is about "Why C/C++ is Platform Dependent or System Dependent" :

When we write a program then it is called as a Source code , then when we compile this program contained source code, we get x.obj that contains the machine language code. The entire program machine language instructions and these instructions are understandable by microprocessors.

There are several microprocessors developed by many companies, Every microprocessors can recognize a group of instructions called the Instruction Set of the microprocessor, So different microprocessor  have different Instruction sets and can't understandable by one another .

If we generate x.exe file on a computer with Pentium processor, then that x.exe file contains machine language instructions understandable to Pentium only. If we try it another computer with another processor except Pentium processor, it cannot execute.

This condition is also applied on OS of computer because different OS stores Instructions in different form. For example, the instruction for addition of two values by Windows  may be add a,b; whereas the same instruction may be stored by UNIX as; a add b, if we generate x.exe in X OS and Y processor then it  execute by X OS and Y processor only.



I hope you like this , if you need to know more about this you can comment for the topic and please share this as much you can  thank you for read this blog...๐ŸŒ๐ŸŒ๐ŸŒ๐ŸŒ