Java Basic Programs

List of Operators in Java

Arithmetic Operators
Addition +
Subtraction –
Multiplication *
Division /
Remainder(Module) %
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Ternary or Conditional Operators

Adition operator :-

package techlearn.in;

public class AddingNumbers {

public static void main(String[] args) {
    int i=9;
    int j=3;
    int result = i+j;
    System.out.println("Result is :" + result);
}

}

Result is :12

Subtraction in Java

package seleniumtool.com;

public class Subtraction {

public static void main(String[] args) {
    int i= 20;
    int j= 10;
    int k= i-j;
    System.out.println("Result=" + k);
}

}

Result=10

Multiplication in Java

package seleniumtool.com;

public class Multiplication {

public static void main(String[] args) {
    int i= 2;
    int j= 3;
    int k= i*j;
    System.out.println("Result=" + k);
}

}

Result=6

Division in java

package seleniumtool.com;

public class Division {

public static void main(String[] args) {
    int i= 12;
    int j= 3;
    int k= i/j;
    System.out.println("Result=" + k);
}

}

Result=4

Modulus operator in Java

package seleniumtool.com;

public class Modulus {

public static void main(String[] args) {
    float i= 10f;
    float j= 3f;
    float k= i/j%100;
    System.out.println("Result=" + k);
}

}

Result=3.3333333

==============================
Java Variables

Variables are containers for storing data values.

In Java, there are different types of variables, for example:

1) String – stores text, such as “Hello Techlearn”. String values are surrounded by Double Quotes.
2) int – stores integers (whole numbers), without decimals, such as 12345 or -12345
3) float – stores floating point numbers, with decimals, such as 18.55 or -18.55
4) char – stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by Single Quotes
5) boolean – stores values with two states: true or false

Note:
String is Class

int, float, char, boolean are keywords.

naming convention in java with example

SNo. Name Convention Example

  1. package name should be start with in lowercase letter java, lang, sql, util etc
  2. class name should start with uppercase letter and be a noun String, System, Firefox, DataInput, Thread
  3. interface name should start with uppercase letter and be an adjective Runnable, Remote, ActionListener
  4. method name should be start with in lowercase letter, then from second word onwards, each new word starts with a uppercase letter. method’s name ends with a simple pair braces() print(),readLine(),test(),collegeName()
  5. variable name should be start with in lowercase letter, then from second word onwards, each new word starts with a uppercase letter. It is same like as for method, but variable names not have any pair braces in after variable name end. print,readLine,test,collegeName
    6 constants name should be written in all uppercase letters PI, MAX_VALUE,MAX_PRIORITY
    7 keywords all keywords should be written in all lowercase letters class,public,static,void,do-while,for,break,etc..In the Java programming language, a keyword is one of 50 reserved words.

=================================
Control statements in java
Def : Control statements are used for flow of execution and give better control for the programmer on the flow of exection. Without control statements programmers can not able to write better programs.

List of Control Statements

if else statement
do while loop
while loop
for loop
nested loop
for-each loop
switch statement
break statement
continue statement
return statement
1) if-else statement :

package seleniumtool.com;

public class IfElseStatement {

public static void main(String[] args) {

    int i=9;
    int j=5;

    if(i<j){
        System.out.println("i is greater than j");
    }
    else if(i==j){
        System.out.println("i is equal to j");
    }
    else {
        System.out.println("i is less than j");
    }
}

}

Result :
i is less than j

do..while

package seleniumtool.com;

class DoWhile
{
public static void main(String args[])
{
int i=3;
do
{
System.out.println(+i);
i++;
}
while(i<9);
}
}

Output :
3
4
5
6
7
8

While loop in Java

package seleniumtool.com;

public class WhileDemo
{
public static void main(String args[])
{
int i=4;
while(i<10)
{
System.out.println(+i);
i++;

    }
}

}

Output :
4
5
6
7
8
9

for loop in Java

package seleniumtool.com;

public class ForLoop {

public static void main(String[] args) {
    System.out.println("***Decrement***");
    for (int i=10; i>=0; i--){
        System.out.println(i);
    }

    System.out.println("***Increment***");
    for (int j=0; j<=10; j++){
        System.out.println(j);
    }
}

}

Output :-
Decrement
10
9
8
7
6
5
4
3
2
1
0
Increment
0
1
2
3
4
5
6
7
8
9
10

nested loop in Java :-

package seleniumtool.com;

public class NestedLoop {

public static void main(String[] args) {
    int r=10;   // r represents row
    for (int i=1;i<=r;i++)   // i represents row number
    {
        for(int s=1;s<=i;s++)  // s represents number of stars
        {
            System.out.print(" * ");
        }
        System.out.println();
    }
}

}

Output :-

    • *
  • * *
  • * * *
  • * * * *
  • * * * * *
  • * * * * * *
  • * * * * * * *
  • * * * * * * * *
  • * * * * * * * * *

for-each loop in Java

package seleniumtool.com;

public class ForEach {

public static void main(String[] args) {

    int array[]= {15,-5,7,0,-4,13,-18}; // declare an array with 7 elements

   for (int i : array){   // for-each to retrive elements from array

       System.out.println(i); // i represent each element of arry
   }
}

}

Output :-
15
-5
7
0
-4
13
-18

switch statement in Java

package seleniumtool.com;

import java.util.Scanner;

public class SwitchStatement {

public static void main(String[] args) {

    int i;
    System.out.println("Enter any number (1 to 7) :");
    Scanner s=new Scanner(System.in);
    i=s.nextInt();
    switch(i)
    {
    case  1:
        System.out.println("Today is Monday");
        break;
    case  2:
        System.out.println("Today is Tuesday");
        break;
    case  3:
        System.out.println("Today is Wednesday");
        break;
    case  4:
        System.out.println("Today is Thursday");
        break;
    case  5:
        System.out.println("Today is Friday");
        break;
    case  6:
        System.out.println("Today is Saturday");
        break;
    case  7:
        System.out.println("Today is Sunday");
    default:
        System.out.println("Only enter value 1 to 7");
    }
}

}

Output :-
Enter any number (1 to 7) :
4
Today is Thursday

break statement in Java :-

package seleniumtool.com;

public class Break {

public static void main(String[] args) {
    int [] numbers = {0, 1, 2, 3, 4, 5};

      for(int x : numbers ) {
         if( x == 4 ) {
          break;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

Output :-
0
1
2
3

continue statement in Java :-

package seleniumtool.com;

public class Continue {

public static void main(String[] args) {
    int [] numbers = {0, 1, 2, 3, 4, 5};

      for(int x : numbers ) {
         if( x == 3 ) {
                continue;
         }
         System.out.println( x );

      }

}

}

Output :-
0
1
2
4
5

return statement :-

package seleniumtool.com;

public class returnStatement {

public static void main(String[] args) {
int a= 3;
System.out.println("Before return");
if (a>2) return;
System.out.println("After return");

}

}

Output :-
Before return

=============================