Wednesday, 31 July 2019

Keywords in Java

Keywords are reserved words in java which act as a key to code and has the intent of its own use.

Rules for Keywords: 
  1. Keywords should be in lower case letters.
  2. Keywords cannot be used as names for Methods/classes.
Why do we need Keywords?
In java we perform different operations using keywords and following are the purpose and list of keywords:

Creating java files:
1. class 
2. interface
3. enum

Data types:
4. byte
5. short
6. int
7. long
8. float
9. double
10. char
11. boolean
12. void

Memory Location
13. static
14. new

Control Statements
Conditional Statements:
15. if
16. else
17. switch
18. case
19. default
Loop
20. while
21. do
22. for
Transfer
23. break
24. continue
25. return

Accessibility Modifier
26. private
27. protected
28. public

Modifiers
29. static
30. final
31. abstract
32. native
33. transient
34. volatile
35. synchronized
36. strictfp

Object Representation
37. this
38. super
39. instanceof

Inheritance - Relationship
40. extends
41. implements

Package
42. package
43. import

Exception Handling
44. try
45. catch
46. finally
47. throw
48. throws
49. assert

Unused Keywords
50. const
51. goto






Naming conventions in Java

What is the purpose of Naming conventions?

The main purpose of naming conventions is:

  1. Readability
  2. Maintainability
If the teams don't follow the naming conventions the individuals use their own conventions and that will lead to ambiguity to understand the code.

Here are the typical Java naming conventions as per standards which are handy:



Thursday, 9 November 2017

Conditional Statements

Conditional Statements:

When we need to execute a set of statements based on a condition then we need to use control flow statements. For example, if a number is greater than zero then we want to print “Positive Number” but if it is less than zero then we want to print “Negative Number”.
There are 4 Conditional if statements and 1 special Switch Statement. Listed below are the conditional statements followed by its syntax and usage.
1.      if statement
2.      nested if statement
3.      if-else statement
4.      if-else-if statement
5.      Switch case statement

1.If statement
If statement consists a condition, followed by statement or a set of statements as shown below:
               if(condition){
                Statement(s);
               }
Example Program:
public class IfStatementExample {

   public static void main(String args[]){
      int num=99;
      if( num < 100 ){
                 /* This println statement will only execute,
                  * if the above condition is true
                  */
                 System.out.println("number is less than 100");
      }
   }
}
2.Nested if statement in Java
When there is an if statement inside another if statement then it is called the nested if statement.
The structure of nested if looks like this:
if(condition_1) {
   Statement1(s);
   if(condition_2) {
      Statement2(s);
   }
}
Example Program:
public class NestedIfExample {
   public static void main(String args[]){
        int num=99;
               if( num < 100 ){
           System.out.println("number is less than 100");
           if(num > 50){
                     System.out.println("number is greater than 50");
                  }
               }
   }
}
3. If else statement in Java
The statements inside “if” would execute if the condition is true, and the statements inside “else” would execute if the condition is false.
This is how an if-else statement looks:
if(condition) {
   Statement(s);
}
else {
   Statement(s);
}
Example Program:
public class IfElseExample {
   public static void main(String args[]){
     int num=111;
     if( num < 100 ){
               System.out.println("num is less than 100");
     }
     else {
               System.out.println("num is greater than or equal 100");
     }
   }
}
4. if-else-if Statement
if-else-if statement is used when we need to check multiple conditions. In this statement we have only one “if” and one “else”, however we can have multiple “else if”. It is also known as if else if ladder. This is how it looks:
if(condition_1) {
   /*if condition_1 is true execute this*/
   statement(s);
}
else if(condition_2) {
   /* execute this if condition_1 is not met and
    * condition_2 is met
    */
   statement(s);
}
else if(condition_3) {
   /* execute this if condition_1 & condition_2 are
    * not met and condition_3 is met
    */
   statement(s);
}
.
.
else {
   /* if none of the condition is true
    * then these statements gets executed
    */
   statement(s);
}
Example Program:
public class IfElseIfExample {
   public static void main(String args[]){
               int num=1234;
               if(num <100 && num>=1) {
                 System.out.println("Its a two digit number");
               }
               else if(num <1000 && num>=100) {
                 System.out.println("Its a three digit number");
               }
               else if(num <10000 && num>=1000) {
                 System.out.println("Its a four digit number");
               }
               else if(num <100000 && num>=10000) {
                 System.out.println("Its a five digit number");                                   
               }
               else {
                 System.out.println("number is not between 1 & 99999");                                         
               }
   }
}
5. Switch case statement is used when we have number of options (or choices) and we may need to perform a different task for each choice.
The syntax of Switch case statement looks like this –
switch (variable or an integer expression)
{
     case constant:
     //Java code
     ;
     case constant:
     //Java code
     ;
     default:
     //Java code
     ;
}
Switch Case statement is mostly used with break.
Example Program:
class SwitchDemo {
    public static void main(String[] args) {
        int month = 8;
        switch (month) {
            case 1:  System.out.println("January"); break;
            case 2:  System.out.println("February"); break;
            case 3:  System.out.println("March"); break;
            case 4:  System.out.println("April"); break;
            case 5:  System.out.println("May"); break;
            case 6:  System.out.println("June"); break;
            case 7:  System.out.println("July"); break;
            case 8:  System.out.println("August"); break;
            case 9:  System.out.println("September"); break;
            case 10: System.out.println("October"); break;
            case 11: System.out.println("November"); break;
            case 12: System.out.println("December"); break;
            default: System.out.println("Invalid month.");break;
        }
    }
}




Thursday, 22 September 2016

Data Types

Data types specify:
1. Size of the memory location.
2. Range of the data that can be accommodated.
3. Type of the data stored in the system with which you want to execute the logic.


      Syntax:           Data-type  Variable-name;

Examples:

  1. byte b;
  2. short s;
  3. int x=10;
  4. long l;
  5. float f=10.32;
  6. double d;
  7. boolean t;
  8. char c='a';

Cheers,
Navatha.

Tuesday, 2 February 2016

Object and Class in Java

Let’s discuss the core thing on which we been and going to write the Java Programs. Those are OBJECT and CLASS.

Object defines the state, behavior, and identity of a subsistence is technically known as an entity.
State: represents data (value) of an object.
Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But it is used internally by the JVM to identify each object uniquely.

       Take a real life example: let’s take a Pen and relate the same with above, the pen is an object with some name say “Parker”, it has some color say “Red” these properties are termed as “State”, the pen is used for “writing” so writing is the “Behavior”  and the pen will be having a cap, refill nib etc. can be called the Identity.

A class is a group of objects that has common properties. It is a template or blueprint from which objects are created.
A class in java can contain:
·         data member
·         method
·         constructor
·         block
·         class and interface
Syntax to declare a class:
class <class_name> {
    data member;
    method;
}
Instance variable in Java

A variable that is created inside the class but outside the method is known as an instance variable. Instance variable doesn't get memory at compile time. It gets memory at runtime when an object (instance) is created. That is why it is known as an instance variable.
Method in Java

In Java, a method is like function i.e. used to expose the behavior of an object.
Advantage of Method
·                                    Code Reusability
·                                    Code Optimization
·                                   new keyword

The new keyword is used to allocate memory at runtime.

          

       


Tuesday, 27 October 2015

Basic OOPS Concepts and Advantages

Hope All are doing Great!

In this post we will discuss about basics of OOPs.

Object Oriented Programming is an approach that provides many concepts such as inheritance, abstraction, polymorphism etc.

There were other programming languages which are based on OOPs are Simula , Smalltalk before Java era where the programs are written based on Objects.

Once Java is introduced java became the irreplaceable programming language based on OOPs.

Now let’s see what the concepts of OOPs are in brief.

1. Encapsulation - let’s see what is Encapsulation?
The concept of binding the data along with its related and corresponding functions is known as Encapsulation.

We can say Class is the base for encapsulation.

2. Inheritance - let’s see what is Inheritance?
As the name suggests, inheritance means to take something that already made.
One of the most important feature of Object oriented Programming. It is the concept that is used for re usability purpose.
Getting the properties from one class object to another class object.

3. Abstraction - let’s see what is Abstraction?
Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing.

In java, we use abstract class and interface to achieve abstraction.

4. Polymorphism - let’s see what is Polymorphism?

When one task is performed by different ways i.e. known as polymorphism.

For example we are human beings having similar features but with different names.

Advantages of OOPs:
OOPs make development and maintenance easier.
OOPs provide data hiding where data access can be restricted.
OOPs provide ability to simulate real-world event much more effectively. We can provide the solution of real word problem if we are using the Object-Oriented Programming language.
In OOP, programmer not only defines data types but also deals with operations applied for data structures.

In the Next post we will discuss about Object and Class.

Cheers & Happy learning,

Navatha Kannadi.



Wednesday, 14 October 2015

History and installation of Java

Hope all doing well!

Here I start with the concepts of Core Java which are helpful for the Selenium Automation.

What is Java?
    Java is a more powerful Programming language, a high level, robust, secured and object-oriented. It is markedly known for its powerful feature WORA (Write Once Run Any Where). Hence Java has its own Run Time Environment (JRE) it is widely notable as platform independent.

History of Java:
·         Originally developed by James Gosling at Sun Micro-systems (which is now a subsidiary of Oracle Corporation) and released in 1995.
·         JDK 1.0 released in January 23, 1996.
·         Java is initially named as OAK which is said to be the symbol of Strength.
·         JAVA is the name of an island in Indonesia where first coffee was produced (called java coffee).

Java Versions:
There are many java versions that have been released. Current stable release of Java is Java SE 8
JDK Alpha and Beta (1995)
JDK 1.0 (23rd Jan, 1996)
JDK 1.1 (19th Feb, 1997)
J2SE 1.2 (8th Dec, 1998)
J2SE 1.3 (8th May, 2000)
J2SE 1.4 (6th Feb, 2002)
J2SE 5.0 (30th Sep, 2004)
Java SE 6 (11th Dec, 2006)
Java SE 7 (28th July, 2011)
Java SE 8 (18th March, 2014)

How to install Java?
First and foremost check whether java is installed in your system as follows:
Go to Command Prompt and type the command "java -version" will result the current version of java installed in your system.
If java is not installed download JDK and install it.

Java programs can be executed in multiple editors here I would like to pick Notepad and Eclipse IDE (Install Eclipse IDE here)

First Sample Java program: The following is the first sample java program just open Note pad and copy the following code:
Class First Program { 
    public static void main(String args[]){ 
     System.out.println("Hello World"); 
    } 
Executing through Notepad editor:
Save this program as First_Program.java

How to compile?
javac is the command to compile. Go to Command prompt -> set the path where you have saved the Java Program ->Type the Command javac First_Program.java

How to run?
After compilation run the program: java First Program gives you the result in command prompt.

See you in Next Post with OOPs concepts :)

Cheers & Happy Learning,
Navatha Kannadi




Keywords in Java

Keywords are reserved words in java which act as a key to code and has the intent of its own use. Rules for Keywords:  Keywords should ...