Tuesday, August 20, 2019

Java programs

Java programming: Java program code consists of instructions which will be executed on your computer system to perform a task as an example say arrange given integers in ascending order. This page contains sample programs for beginners to understand how to use Java programming to write simple Java programs. These programs demonstrate how to get input from a user, working with loops, strings, and arrays. Programs are provided with the output (image file), and you can also download class file and execute it directly without compiling the source file.

Compiling and executing Java programs

Java programming software: To compile and run Java program code you need to download JDK (Java Development Kit).
To compile type: javac file_name.java where file_name is the name of the file containing Java source code.
Javac is the Java compiler which converts java code into bytecode.
To run type: java main_method_class where main_method_class is the name of the class which defines "main" method.

Learn Java through books

If you are just starting to learn Java then it is recommended to buy Java programming book. A Java book will help you to learn basic concepts easily and will act as a reference for all time.

Java programming examples

Example 1: Display message on computer screen.
  1. class First {
  2.   public static void main(String[] arguments) {
  3.     System.out.println("Let's do something using Java technology.");
  4.   }
  5. }
This is similar to a hello world Java program.
Download java programming class file.
Output of program:
Example 2: Print integers
  1. class Integers {
  2.   public static void main(String[] arguments) {
  3.     int c; //declaring a variable
  4.  
  5.   /* Using for loop to repeat instruction execution */
  6.  
  7.     for (= 1; c <= 10; c++) {
  8.       System.out.println(c);
  9.     }
  10.   }
  11. }
Output: