Posts

Showing posts from November, 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 continu...

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 { ...

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       ...

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 Dem...

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           ...

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()                    ...

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 Ov...

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();              }  ...

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  ");              ...

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();         ...

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(...

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 {                       ...

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 o...

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        ...

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...