Keywords in java -part 1

     Keywords are reserved words in java Programming language. Each keyword represents a specific meaning. These are used for specific purpose only.

We classified the keywords based on usage and listed below.

  • 1.       Keywords based on datatypes.
  • 2.       Keywords based on loops and conditional statements.
  • 3.       Keywords based on classes and objects
  • 4.       Keywords based on try catch
  • 5.       others

1.Keywords based on datatype:

boolean

Byte

char

int

long

Float

Double

strictfp

volatile

short

 How to use these keywords in java Program:

class Example

{

 public static void main(String args[])

 {

  int a=10;

  float b = 13.3f;

  double c =45.67;

  long d =34;

  short e=5;

  boolean f = true;

  char g='j';

  byte h=7;

  System.out.println("integer a:"+a);

  System.out.println("float b:"+b);

  System.out.println("double c:"+c);

  System.out.println("long d:"+d);

  System.out.println("short e:"+e);

  System.out.println("boolean f:"+f);

  System.out.println("char g:"+g);

  System.out.println("byte h:"+h);

 }

}

Here, is the output for the program.


2. Keywords based on loop and conditional statement:

do

while

continue

break

if

else

for

switch

case

default

Example:

class Example1

{

 public static void main(String args[])

 {

   int a = 1;

   do

   {

    System.out.println(a);

    a++;

   }

   while(a<=10);

 }

}

The output is


Replace do while part by

while(a<=10)

{

    System.out.println(a);

    a++;

}

If you are using for loop, here is the code.

for( a =1;a<=10;a++)

{

System.out.println(a);

}

Next, if conditional statement code is given below.

if (a>5)

{

 System.out.println(“a is greater than 5”);

}

else

{

 System.out.println(“a is less than 5”);

}

Switch case syntax is given below.

 

switch(expression)

{

 case1: statements;

              break;

case2: statements;

              break;

……………..

……………..

default:

Statements;

}

These are the way of using keywords in java programming part1 is completed. Next, part 2 shows the remaining keywords.

No comments:

Post a Comment