Posts

Showing posts from 2016

continue and break statement

continue :           Continue statement is used to skip the execution of code in loop . Continue statement is used only inside the loop control(while,do-while,for) in java . continue is a keyword in java. break :        break statement is used to terminate the execution of code. break is a keyword in java. Let's see an example of continue and break statement. Without continue and break statement (Fig-1): public class ContinueBreakExample {   public static void main(String[] args)   {     for(int i=0;i<3;i++){        System.out.println(i);     }   } } Output :               0               1               2 With continue statement (Fig-2) :  public class ContinueBreakExample {   public static void main(String[] args)   {     for(int i=0;i<3;i++){        if(i==1)        {          continue;        }       System.out.println(i);     }   } } Output :               0                          2 With break statement (Fig-3) : publi

switch statement in java

switch statements is similar to if-else-if ladder in java . It execute one statement from multiple conditions. A switch statement have an optional default case which can be appear at the end of switch . Default case will execute when none of conditions are matched . Default case has no break statement . Each case must have break otherwise the flow of control will fall until a break is reached . switch works with byte,int,short,char primitive data types . It also works with Enum types , String class etc.   Syntax : switch(expression) {   case value : // statements  break;  case value :    // statements    break;    default : // statements }    Example :    public class SwitchDemoExample { public static void main(String[] args) { int month = 5; S

Nested If in java

A nested if statement is an if-else statement with another if statement as the if body or the else body . Here conditions are evaluated from top to bottom . If  1st condition is true then the fist if statement will execute and if  1st condition is false then else if condition will check and rest of the process will move on one by one . If none of the above condition is match then else part will execute .  Syntax :           If(1st condition)           {                   // execute code           }           else if (2nd condition)           {               // execute code           }           .           .           .         else if (nth condition)         {           // execute code         }        else         {             //  this part will be executed if none of the conditions in 1st condition,               // 2nd condition,…nth condition are true.         }   Example :   public class DemoNestedIf   {      public static void main(String args[])      {

If else statement in java

If else statement is a most important part in decision making in Java . If else statement executes depend on boolean expression . If boolean expression is true the if statement will execute otherwise else . Syntax :               if(expression is true){                       //  execute statement                 }                else{                        //  execute statement                     } Example :             public class DemoTest {                  public static void main(String args[]) {                     int i = 10;                     if( i > 8 ) {                         System.out.print("Execute if statement");                    }else {                        System.out.print("Execute else statement");                    }                }             } Output :                  Execute if statement Find us :         Facebook : @apnaandroid         Google+   : Apna Java         YouTube : Android & Java Tutorial

Difference between while and do-while statements in Java

1.   while loop is an entry controlled loop and do-while loop is an exit controlled loop. 2.  while loop first check the condition and then execute statements body .      do-while loop first execute statements body and then check the condition. 3.  do-while loop execute the statement body at least once but while loop not. 4.  Syntax of while loop :                                          while(condition){                                              // statements                                           }      Syntax of do-while loop :                                          do{                                                 // statements                                               }while(condition);          5. Example of while loop :                                   public class LoopExample                                     {                                         public static void main(String[] args)                                           {

Loop control in Java

Loops are used for execute a statement or a group of statement more than one times . Loops are 1. while loop :                                  Execute a statement or group of statement depend on condition .      If condition is true . Let's see an example                         public class LoopControl                           {                              public void whileloop()                                  {                                        int i=0;                                         while(i<10){    // here check the condition , if value less                                                   i++;       // than 10 then statement will execute .                                               System.out.println(i);                                    }                                }                               public static void main(String[] args)                               {                                  LoopControl obj = new LoopCo

Modifier in Java

Image
There are two types of modifiers available in java . A) Access Modifiers B) Non-Access Modifiers . Access modifiers are four types . These are 1) Default 2) private 3) public 4) protected . Java provides number of non-access modifiers.  1) Default : Visible to the package . No modifiers needed .  2) private : Access within the class .  3) public : Access all over the world .  4) protected : Access to the package and all sub-classes . Find us :         Facebook : @apnaandroid         Google+   : Apna Java         YouTube : Android & Java Tutorial

Constructor Overloading in Java

Constructor Overloading means when a class contain more than one method whose names are same as class name and parameter list is different . Here is an example .    public class ConstOverloadDemo    {        ConstOverloadDemo(String str)       {          System.out.print(str);       }      ConstOverloadDemo(String name ,int i)      {         System.out.print("Name : "+name+" , "+"Age : "+i);      }      public static void main(String[] args)       {           ConstOverloadDemo myObject = new ConstOverloadDemo("Constructor   Overloading : ");    ConstOverloadDemo myObject1 = new ConstOverloadDemo("Joes",25);   } } Output : Constructor Overloading : Name : Joes , Age : 25 Find us :         Facebook : @apnaandroid         Google+   : Apna Java          YouTube : Android & Java Tutorial

Constructor in Java

Constructor is a special type of method for create a instance of class. Constructor  name is same as class name . It has no return type . There are two types of constructor in java . These are a) Default Constructor b) Parameterized Constructor. Here is an example   Default Constructor :       public class ConstDemo       {          ConstDemo()           {             System.out.print("Default Constructor");           }          public static void main(String[] args)             {                 ConstDemo demo = new ConstDemo();              }       }        Output : Default Constructor    Parameterized Constructor :     public class ConstDemo       {          ConstDemo(String str)           {             System.out.print(str);           }          public static void main(String[] args)             {                 ConstDemo demo = new ConstDemo("Parameterized Constructor");              }       }     Output : Parameterized Con

Method Overriding in Java

Method Overriding means when a method is exists in super class and same method name is exists in sub class or derived class . Here is an example Without Method Overriding :                    public class Demo                     {                           void display()                            {                               System.out.println("without method override  ");                            }                      }                   public class DemoTest extends Demo                    {                           public static void main(String[] args)                        {                            DemoTest myTest = new DemoTest();                            myTest.display();                          }                  }                Output : without method override Method Overriding :                     public class MethodOverridingDemo                        {                              void display()               

Method Overloading in Java

When a method have same name but their no. of arguments and type of arguments is different in a class is called method overloading. Here is an example 1. No. of Arguments    public class MethodOverloadingDemo    {       void multiplication(int x,int y)         {             System.out.println("x*y : "+(x*y));         }         void multiplication(int x,int y,int z)         {             System.out.println("x*y*z : "+(x*y*z));         }        public static void main(String[] args)       {           MethodOverloadingDemo myObject = new MethodOverloadingDemo();             myObject.multiplication(10,12);  // print x*y : 120             myObject.multiplication(10,12,15); // print x*y*z : 1800      } } 2. Type of Arguments     public class MethodOverloadingDemo      {          void substact(int x,int y)           {              System.out.println("x-y : "+(x-y));           }           void substact(double x,double y)        

Operators in Java

Operators are the symbol that is used to perform operations . There are many types of operator available in java. Operators are as follows    1.Assignment Operators :                                              Assignment operators are used to assign a value in         in variable .If a value exists in variable then new value will be overridden in      variable . Here is an example .                                  public class Demo         {           public Demo(){               int x;                   x=10;  // assigned value in x                System.out.println("before override the value of x is : " + x);  // print 10                   x=20;                System.out.println("after override the value of x is : " + x);   // print 20            }               public static void main(String args[]) {                 Demo demo=new Demo();                }         } 2.Arithmetic Operators :                                           Arithm

OOPs concept in java

The full form of OOPs is Object Oriented Programming System. Objects means the real word entity such as table,chair,blackboard etc. Object Oriented Programming is a methodology to design a program using class and objects . The object oriented paradigm supports the following principles.   Class : A class is a template for creating different objects which defines its                properties and behaviors. A class can contain fields and methods to              describe the behavior of an object.                   Example :                   public class First {                       int a=10;                       int b=12;                                           public int getTotal() {                          return (a + b );                       }                   }   Object : Any entity that has state and behavior is known as an object.           Example :                   public class First {                       int a=10;                       int

Variables in Java

A variable is the name that reserved space in memory . Variables in java are three types . 1. Local Variable :            a) A variable which is declared inside the method of a class            b) Local variable can be access within the method not outside the method.            c) There is no default value for local variable . That means it should be                declared and initialize before first use .            d) Access  modifiers can not be used for local variable . 2. Instance Variable :           a) A variable which is declared inside the class but outside the method.           b) Instance variables are created  when a object is created of a class using              " new " keyword .           c) Access  modifiers can  be used for Instance variable .           d) Instance variable has default value . Like boolean is false , int is '0' 3. Static Variable :           a) A variable which is declared as static is called static variable .     

Data Types in Java

Image
Data types represents to store the different types of value in variable.There are two types of data types available in Java . 1.Primitive data types 2.Non-primitive data types 1. Primitive data types :            Primitive data types are used to store single value at a time in variable .   Example : int i;                 i=0; // valid                 i=0,1; // invalid    Data types         Size(bits)   Size(byte)     byte                       8             1     char                      16           2     short                     16           2     int                         32           4     long                      64           8     float                      32           4     double                  64           8     boolean                  1           - 2. Non-primitive data types :     String , Array etc Find us :         Facebook : @apnaandroid         Google+   : Apna Java         YouTube : Android & Java Tutorial

What is Java

Java is an object-oriented language. The source code of java files (files with a .Java extension) are compiled into a bytecode (files with a .class extension), which can then be executed by a Java interpreter.The syntax of Java is largely influenced by C++. Java is platform independent, that means once the source code is compiled into bytecode then this bytecode will execute on any machine. It's robust and secure. Java was developed by Sun Microsystems and released in 1995. Hello World program in Java : public class MyFirstProgram {    public static void main(String []args) {       System.out.println("Hello World");    } } After execute this program there are two file will create , one is "MyFirstProgram.java" and another one is "MyFirstProgram.class" . Here "MyFirstProgram.java" is the file of source code and "MyFirstProgram.class" is the bytecode file . This  file(.class extension) will execute on any machine , doe