Java Programming Examples

Java programming is a language that is still dominating the android world. There are almost tons of famous android applications written in Java. Some world-famous applications uses Java are Google, Amazon, LinkedIn, Uber, Spotify etc.

I know as a programmer, to learn a language efficiently. We need to take practicals as more as possible. Therefore, I've written around 1000 codes in Java here, separated in more than 100 Java program articles.

Since there are a lot of programs, I've written in Java. And of course, all those programs can not be included in a single page article. Therefore I've divided those programs in separate articles.

This is basically the home page of Java programming examples. All the program written here are well-tested and executed using/under one of the most famous Java IDE named Apache NetBeans IDE. This series of Java programming examples, are designed, while taking care of beginners too. So that, in most of the article, first I've included the basic version of the code, then written the complete version of the same program.

And I assure you, after practicing all the programs written here, with yourself. You'll feel much better than ever before. Therefore it becomes important to experiment all the Java programs written here. As much you practice, as more you build your skill towards Java.

Simple Java Programs for Beginners

Here are the list of some simple Java programs written for beginners. So if you're a beginner Java programmer, then must practice these Java programs, before visiting the other Java programs. These Java programs may boost your knowledge in the field of Java, so that, you can easily get started.

List of Top 100 Java Programs

Here are the list of top 100 Java programs. The series of Java programs provided here, is not limited to these list of Java programs. You'll get much more than this. But, these are the list of common programs, that are almost available in most of the projects and assignments.

  1. Java Program to Get Input from User
  2. Java Program to Find Sum of Digits of a Number
  3. Java Program to Calculate Grade of Student
  4. Java Program to Interchange the Digits of a Number
  5. Java Program to Find Largest of two Numbers
  6. Java Program to Find Largest of three Numbers
  7. Java Program to Find HCF & LCM of Two Numbers
  8. Java Program to Convert Temperature from Fahrenheit to Celsius
  9. Java Program to Convert Temperature from Celsius to Fahrenheit
  10. Java Program to Find Quotient and Remainder
  11. Java Program to Convert Days into Seconds
  12. Java Program to Count Number of Digits in a Number
  13. Java Program to Add Two Binary Numbers
  14. Java Program to Calculate Discount and Price to Paid
  15. Java Program to Compute Courier Charge to Send the Parcel
  16. Java Program to Find Telephone Bill
  17. Java Program to Find Simple Interest
  18. Java Program to Find Compound Interest
  19. Java Program to Print ASCII Values
  20. Java Program to Print Fibonacci Series
  21. Java Program to Check Palindrome or Not
  22. Java Program to Check Armstrong or Not
  23. Java Program to Generate Armstrong Numbers
  24. Java Program to Find nCr and nPr
  25. Java Program to Convert Decimal to Binary
  26. Java Program to Convert Decimal to Octal
  27. Java Program to Convert Decimal to Hexadecimal
  28. Java Program to Convert Binary to Decimal
  29. Java Program to Convert Binary to Octal
  30. Java Program to Convert Binary to Hexadecimal
  31. Java Program to Convert Octal to Decimal
  32. Java Program to Convert Octal to Binary
  33. Java Program to Convert Octal to Hexadecimal
  34. Java Program to Convert Hexadecimal to Decimal
  35. Java Program to Convert Hexadecimal to Binary
  36. Java Program to Convert Hexadecimal to Octal
  37. Java Program to Pattern of Stars
  38. Java Program to Pattern of Alphabets
  39. Java Program to Pattern of Numbers
  40. Java Program to Print Pyramid of Stars
  41. Java Program to Print Pyramid of Alphabets
  42. Java Program to Print Pyramid of Numbers
  43. Java Program to Print Diamond Pattern
  44. Java Program to Print Floyd's Triangle
  45. Java Program to Print Pascal's Triangle
  46. Linear Search Program in Java
  47. Binary Search Program in Java
  48. Java Program to Add two Numbers using Pointers
  49. Java Program to Find Largest Element in an Array
  50. Java Program to Find Smallest Element in an Array
  51. Java Program to Find the Reverse of an Array
  52. Java Program to Insert an Element in an Array
  53. Java Program to Delete an Element from an Array
  54. Java Program to Merge two Arrays
  55. Bubble Sort Program in Java
  56. Selection Sort Program in Java
  57. Insertion Sort Program in Java
  58. Java Program to Find Common Elements between Two Arrays
  59. Java Program to Count Even and Odd Numbers in an Array
  60. Java Program to Add two Matrices
  61. Java Program to Subtract two Matrices
  62. Java Program to Transpose Matrix
  63. Java Program to Multiply two Matrices
  64. Java Program to Find Length of a String
  65. Java Program to Compare two Strings
  66. Java Program to Copy String
  67. Java Program to Concatenate String
  68. Java Program to Reverse a String
  69. Java Program to Delete Vowels from String
  70. Java Program to Delete Word from String
  71. Java Program to Find the Occurrence of a Character in String
  72. Java Program to Find Occurrence of a Word in String
  73. Java Program to Find Occurrence of Each Character
  74. Java Program to Find Occurrence of Each Word
  75. Java Program to Count the Number of Repeated Characters
  76. Java Program to Count the Number of Repeated Words
  77. Java Program to Capitalize Each Word in a String
  78. Java Program to Count Vowels and Consonants
  79. Java Program to Extract Numbers from String
  80. Java Program to Count the Number of Words in a String
  81. Java Program to Remove Spaces from String
  82. Java Program to Sort a String
  83. Java Program to Convert Uppercase to Lowercase
  84. Java Program to Convert Lowercase to Uppercase
  85. Java Program to Swap two Strings
  86. Java Program to Check Anagram or Not
  87. Java Program to Check Balance Parentheses
  88. Java Program to Check Password Strength
  89. Java Program to Generate Random Numbers
  90. Java Program to Read a File
  91. Java Program to Write to a File
  92. Java Program to Read & Display the Content of a File
  93. Java Program to Copy the Content of a File
  94. Java Program to Append the Text to a File
  95. Java Program to Merge two File
  96. Java Program to List Files and Folders in a Directory
  97. Java Program to Delete a File
  98. Java Program to Print Time & Date
  99. Java Program to Get Local and Public IP Address
  100. Java Program to Shutdown Computer

Now before starting the series of Java programming examples, let's first take a look at some of the basic Java programs along with its explanation.

Java Program Example No.1

The Java program written below, is the form of a very basic and simplest program that can be written in Java. Let's see the code, and its output. Then its explanation, to get some idea about, what is going behind the code written in Java.

public class fresherearth
{
   public static void main(String[] args)
   {
      System.out.println("Hey, Java is Fun!");
   }
}

This program produces the output as shown in the snapshot given below:

Java Programming Examples

In above program, the following code:

public class fresherearth

tells that a class named fresherearth is defined using the keyword class. This class is declared public, using the public keyword. Here public is an access modifier. Declaring the class as public, means the class fresherearth is accessible to all. And the following code:

public static void main(String[] args)

refers to the definition of main() method. Every Java program must have this method. The execution of the program, starts from within the main() method.

Before the method, main(), the keyword void refers to the return type of the method. Declaring method void as its return type, means method does not return any value. And of course, the we did not want from main() method to return any value :).

Using the method as static type, means that we need not to create an object to invoke the method. And of course, the main() method is to be called without any object, therefore it is defined using static. And the keyword public is again an access modifier.

And the String[] args indicates to an array of a sequence of strings that are passed to the method main(). This happens, when the program is executed. For example, if you execute the Java program using/via the command line:

java fresherearth Hey, Java is Fun!

Then, the array will store: ["Hey,", "Java", "is", "Fun!"].

And finally, inside the main() function, the code System.out.println() is used to print content on the output screen. For example, in above program, as I've passed a string "Hey, Java is Fun!". Therefore this string gets printed on the output screen.

The above program, can also be written as:

public class fresherearth
{
   public static void main(String[] args)
   {
      String str = "Hey, Java is Fun!";
      System.out.println(str);
   }
}

You'll get the same output as of previous program's output.

Now in short if I say, then every Java program contains a main() method, inside the class, with name, same as Java application or program's source code name, that is:

public class fresherearth
{
   public static void main(String[] args)
   {
      // your Java codes goes here
   }
}

where fresherearth is the name of the class. In your case, the only change may occur, is this name. Other things would be same. And in the section // your Java codes goes here, you'll write your Java code to do the job, whatever you want. Now let's take a look at some more Java program examples, given below.

Java Program Example No.2

This is the second version program written in Java. This program uses loop to execute a statement, multiple times.

public class fresherearth
{
   public static void main(String[] args)
   {
      for(int i=0; i<10; i++)
         System.out.println(i);
   }
}

The output produced by this Java program will be:

Java Programs

As already told, that the execution of the Java program, starts from main() method. Therefore, inside the main() method, there is a for loop defined. The for loop starts its execution, in a way that, the first statement gets executed at initially and only at once. Therefore, using the first statement, that is:

int i=0;

a variable i gets defined, which is of int type. And initialized with a value 0. Now while entering into the body of the loop, every time the compiler checks the condition of the loop. That is, the second statement (condition-checking) gets executed.

i<10;

now the variable i gets replaced with its value. Since its value for now is 0, therefore 0<10 evaluates to be True. Since the condition evaluates to be True, therefore program flow goes inside the loop. Now inside the loop, the statement:

System.out.println(i);

gets executed. That prints the value of i on output screen. Therefore 0 gets printed on the output. When the statement of the loop, gets executed, then compiler goes to the third statement of the loop, that is:

i++

the value of i gets incremented by 1. So i=1 now. As already told, before entering into the loop, the condition-checking statement (second statement), gets evaluated. Since i<10 or 1<10 again evaluates to be True, therefore program flow again goes inside the loop, and prints the new value of i. That is, 1 this time. This process continues, until the condition evaluates to be False.

Java Program Example No.3

Now let's create another program in Java, that receives the name of a user, and prints some string on the output, along with the entered name:

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      String name;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter Your Name: ");
      name = scan.nextLine();
      
      System.out.println("\nHey,");
      System.out.println(name);
      System.out.println("Are you excited ?");
   }
}

The snapshot given below shows the sample run of above Java program, with user input James as name:

Java Programs list

Because in Java, to scan input from user, we need to user the Scanner class. And the Scanner class is defined inside the java.util package. Therefore, before using the Scanner class to scan the input, I've imported the package, and then defined an object scan of the Scanner class to scan input from user. Don't worry, you'll learn all the things, step by step, in this series of Java programs examples.

In above program, the \n used in the second System.out.println() statement, is an escape sequence. That inserts a newline to the output screen, so that the next output things, will get printed from next line. Therefore, the string Hey gets printed, one line after the user input. Also from above program, the last three System.out.println() statements can be written as:

System.out.println("\nHey,\n" +name+ "\nAre you excited ?");

That is, in above statement:

"\nHey,\n" +name+ "\nAre you excited ?"

is comprises of three strings, the first one is:

"\nHey,\n"

which is the string itself. And the second one is:

name

which is indirectly equals to "Joseph" entered by user, according to the sample run given above. Since the entered string is stored in name variable. Therefore, where ever you write name inside the main() method, it evaluates to be "Joseph". And the third one is:

"\nAre you excited ?"

which is itself a string. And using the + operator, I've added all these three strings. So the output will be same as of previous program's sample run.

Java Program Example No.4

This program does not receives any input from user. This program only declares four variables of int type. And initialized some values to the three variables, and the summation of all three variable's values gets initialized to the fourth variable. The value of fourth variable, gets printed on the output screen using again System.out.println() statement.

public class fresherearth
{
   public static void main(String[] args)
   {
      int a, b, c, res;
      a = 10;
      b = 20;
      c = 30;
      res = a+b+c;
      System.out.println(res);
   }
}

The output produced by above Java program will be:

60

Java Program Example No.5

This is the same program as of previous. The only difference is, the value of three variables, are received by user at run-time of the program:

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      Scanner s = new Scanner(System.in);
      System.out.print("Enter Three Numbers: ");
      int a = s.nextInt();
      int b = s.nextInt();
      int c = s.nextInt();
      int res = a+b+c;
      System.out.println("\nResult = " +res);
   }
}

The output produced by above Java program, with user input 10, 20, and 30 as three numbers, is shown in the snapshot given below:

java examples

Java Program Example No.6

Here is the same program as of previous, except, this program works with both int and float type values. That is, if user enters a whole number, or a real-number, this program works for both.

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      Scanner s = new Scanner(System.in);
      System.out.print("Enter Three Numbers: ");
      float a = s.nextFloat();
      float b = s.nextFloat();
      float c = s.nextFloat();
      float res = a+b+c;
      System.out.println("\nResult = " +res);
   }
}

Here is its sample run with user input 10.43, 20.53, and 30.53:

java examples list

Java Program Example No.7

Here is another program written in Java, that receives two values of different types.

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      String name;
      int n;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter Your Name: ");
      name = scan.nextLine();
      System.out.print("Enter the Value of n: ");
      n = scan.nextInt();
      
      System.out.println("\nPrinting \"" +name+ "\", " +n+ " times.");
      for(int i=0; i<n; i++)
         System.out.println(name);
   }
}

The sample output produced by above Java program with user input Mark as name or string and 10 as value of n is shown in the snapshot given below:

Java Programming examples with output

Above seven Java program examples are written to show you the demo, that how the program can be written in Java. Now from next, one by one, you'll get much more programs written in Java in different-different ways.

Other Language Examples

Java Online Test


« Java Tutorial Next Program »