Java Basic
- What is Programming?
Programming or coding is a language that is used by operating systems to perform the task. We know computer understands binary languages with digits 1s and 0s.
- What is Java?
Java is a high-level object-oriented programming language developed by the Sun Microsystems. Though it is associated with the World Wide Web but it is older than the origin of Web.
- Where Java is used?
The programming language Java was developed by Sun Microsystems in the year 1995. Earlier, it was only used to design and program small computing devices but later adopted as one of the platform independent programming language.
- Java Releases
Java is developed by Sun Microsystems in 1996. The tables given below provide information about the name and time of releases for different versions of Java technology.
- Downloading JDK (Java)
In this lesson you will learn how to download java from the sun web site.
- Installing Java
In this lesson you will learn how to install and configure java on your machine.
- Understanding Java SDK Directory Structure
This section introduces the Directory and file structure of SDK (Software Development Kit).
- Writing Hello World Java program
In this lesson we will learn how to write, compile and test the Hello World java application.
Hello world (First java program)
Java is a high level programming language and it is used to develop the robust application. Java application program is platform independent and can be run on any operating System. Writing Hello World program is very simple. To write the Hello world program you need simple text editor like note pad and jdk must be install in your machine for compiling and running. Hello world program is the first step of java programming language. Be careful when you write the java code in your text pad because java is a case sensitive programming language.
For Example
hello world !=(not equal to) Hello World
Write the following code into your note pad to run the Hello World program .
| class HelloWorld { public static void main(String[] args){ System.out.println("Hello World!"); } } |
Save the file and Please remember the location where you save your file. In this example we have saved the file in the "C:\javatutorial\example" directory. The file name should be match the class name and to save the file give the .java extension. e.g. HelloWorld.java
Now open the command prompt to compile the HelloWorld.java program. Go to the directory where you have saved the file ( in our case C:\javatutorial\example>) and issue the following command to compile the program:
C:\javatutorial\example>javac HelloWorld.java
javac is a compiler in the java Language. Java compiler change the programming Language into machinery language. So that the java virtual can understand it. Once the compilation is successful a new file will be created with the name HelloWorld.class. To run the program issue the following command on command prompt:
C:\javatutorial\example>java HelloWorld
You will see the following result on the command prompt.
Hello World!
Here is the screen shot of the above steps:

In this lesson you have learned how to write and then test the Hello World! java program.
- Understanding Hello World Java Program
In this lesson we will understand the Hello World Java program.
Now you are familiar with the Java program. In the last lesson you learned how to compile and run the Java program. Before start hard programming in Java, its necessary to understand each and every part of the program. Lets understand the meaning of public, class, main, String[] args, System.out, and so on.
| public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } |
Class Declaration:
Class is the building block in Java, each and every methods & variable exists within the class or object. (instance of program is called object ). The public word specifies the accessibility of the class. The visibility of the class or function can be public, private, etc. The following code declares a new class "HelloWorld" with the public accessibility:
public class HelloWorld {
The main Method:
The main method is the entry point in the Java program and java program can't run without main method. JVM calls the main method of the class. This method is always first thing that is executed in a java program. Here is the main method:
public static void main(String[] args) {
......
.....
.....
}
{ and is used to start the beginning of main block and } ends the main block. Every thing in the main block is executed by the JVM.
The code:
System.out.println("Hello, World");
prints the "Hello World" on the console. The above line calls the println method of System.out class.
The keyword static:
The keyword static indicates that the method is a class method, which can be called without the requirement to instantiate an object of the class. This is used by the Java interpreter to launch the program by invoking the main method of the class identified in the command to start the program.
- Comparing Two Numbers
This is a very simple example of Java that teaches you the method of comparing two numbers and finding out the greater one. First of all, name a class "Comparing" and take two numbers in this class. Here we have taken a=24 and b=25, now we have to find out whether a=b, a>b or b>a.
his is a very simple example of Java that teaches you the method of comparing two numbers and finding out the greater one. First of all, name a class "Comparing" and take two numbers in this class. Here we have taken a=24 and b=25, now we have to find out whether a=b, a>b or b>a. To find out this apply if and else condition one by one. Now apply the condition "if (a=b)", if this satisfies then type that both are equal in the system class. If this doesn't satisfy, then check whether a>b by applying the "else if" condition and type the message "a is greater than b" in the system class. Again this doesn't satisfy then 'else' condition as shown in the example will show that b is greater than a.
Now compile and run the program and you will find the desired output. If you are getting any error then check the whole program thoroughly and surely you will get correct result. By compiling and running this exact program, you will find that b is greater than a.
class Comparing{ public static void main(String[] args) { int a=24, b=25; if (a == b){ System.out.println("Both are equal"); } else if(a>b){ System.out.println("a is greater than b"); } else{ System.out.println("b is greater than a"); } }} |
- Determining the largest number
This example of Java programming will teach you the coding for determining the largest number amongst three. Here we have taken three integers as x = 500, y = 70 and z = 3000.
Determining the largest number
This example of Java programming will teach you the coding for determining the largest number amongst three. Here we have taken three integers as x = 500, y = 70 and z = 3000. After defining these three integers under the class "largernumber" apply "if" and "else" conditions that can help you in finding the largest value one by one.
First check if "x>y". If this satisfies then check whether x>z or not. Again if this satisfies then write in the system class that "x is greater". Again the term "else" comes when "x" is not greater than "z". So check again, if "z" is greater than "y" or not. If this satisfies then type in the system class as "z is greater" otherwise (in the else condition) "y" is greater. Now check whether "y" is greater than "z" or not.
If "x" is not greater than "y" as per the first condition, then the condition "else" comes and now you have to check if "y>z" or not. If this satisfies then the output comes as "y is greater".
Don't get confuse and analyze every condition one by one and follow this example.
Here is the code of program:
class largernumber{ public static void main(String[] args) { int x=500, y=70, z=3000; if (x>y){ if (x>z){ System.out.println("x is greater"); } else{ if(z>y){ System.out.println("z is greater"); } else{ System.out.println("y is greater"); } } } else{ if (y>z){ System.out.println("y is greater"); } } }} |
- List all even numbers between two numbers
Here you will learn to write a program for listing out all the even numbers between two numbers. For this first create a class namedAllEvenNumunder the java.io package.
Java Even Numbers - Even Numbers Example in Java:
Here you will learn to write a program for listing out all the even numbers between two numbers. For this first create a class named
Here you will learn to write a program for listing out all the even numbers between two numbers. For this first create a class named
AllEvenNumunder the java.io package. Now use the try/catch exception to avoid any kind of input error. After this create a buffer class in which all the input data are stored and modified. Then give message as to "Enter number" in the System method. As we have to find out all the even numbers between 1 and the input number, define an integer variable 'num'. Now apply ParseInt method that parses the string character into decimal integer. Again apply forloop in which define an integer i=1 and i<= num also with an increment operator. Then apply the ifcondition that i/2=0 i.e. to find even numbers which are divided by the integer 2. In the end apply the catch exception.
Now and compile and run the program, and enter your desired number to get all even numbers between 1 and this numbers.
Here is the code of the program:
import java.io.*;class AllEvenNum{ public static void main(String[] args) { try{ BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter number : "); int num = Integer.parseInt(br1.readLine()); System.out.println("Even Numbers:"); for (int i=1;i <=num ; i++){ if(i%2==0 ){ System.out.print(i+","); } } } catch(Exception e){} }} |
- Calculate area and perimeter of a circle
The given example will teach you the method for preparing a program to calculate the area and perimeter of a circle.
The given example will teach you the method for preparing a program tocalculate the area and perimeter of a circle. First of all name a class as "CircleArea" under Java I/O package and define and integer r=o, which is the radius of the circle. Now use try exception to handle errors and other exceptional events. As we have to input the value of radius here create a buffered class with an object as 'br1'. This create a buffering character input stream that uses a default sized input buffer. The InputStreamReader here works as a translator that converts byte stream to character stream. Now type message that "Enter radius of circle" in the System.out.println method.
Now use the parseInt() method of the Integer class in order to convert from external numeric format to internal format. Now create the Math class in which all the mathematical functions are defined. This Math class can be imported from the java.lang.* package. Write the program for both the cases: radius and perimeter.
Before ending the program use the Catch mechanism that detects and catch user input errors. In the end compile and run the program and enter your desired value as radius for calculating the radius and perimeter of the circle.
Here is the code of the program:
import java.io.*;class CircleArea{ public static void main(String[] args){ int r=0; try{ BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Radius of Circle : "); r = Integer.parseInt(br1.readLine()); double area = java.lang.Math.PI*r*r; System.out.println("Area of Circle : "+area); double perimeter =2*java.lang.Math.PI*r ; System.out.println("Perimeter of Circle : "+perimeter); } catch(Exception e){ System.out.println("Error : "+e); } }} |
- Calculate factorial of any given number
This Java programming tutorial will teach you the methods for writing program to calculate factorial of any given number. First of all define a class "Factorial" under the Java I/O package.
This Java programming tutorial will teach you the methods for writing program to calculate factorial of any given number. First of all define a class "Factorial" under the Java I/O package. Java I/O package has a input stream and a output stream in which input stream is used for reading the stream and memory allocating and the output stream is used for writing bytes. As in this program we are going to insert certain instruction by creating buffer reader class, it is necessary to use 'try' and 'catch' block for catching and handling exceptions during execution of the program.
Here, we have to create a buffer for the string class that can be used to instantiate a changeable object for storing and processing a string of character. The strings length and content change as soon as any object is inserted, replaced or removed from the StringBuffer object.
Now create a buffer object that inherits properties from the string object class. Now create an InputStreamReader that reads bytes and decodes them into character by using certain 'charset'. Now use the ParseInt method for converting the parses the string argument to a decimal integer and define 'a' as an integer. Take an integer variable as fact=1 and insert the message in the System method.
Now applying for loop with conditions as integer i=1(intializer), i<=a and i++ as increment operator. So output result will be like fact=fact*i.
Here is the code of the program:
import java.io.*; class Factorial{ public static void main(String[] args) { try{ BufferedReader object = new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter the number"); int a= Integer.parseInt(object.readLine()); int fact= 1; System.out.println("Factorial of " +a+ ":"); for (int i= 1; i<=a; i++){ fact=fact*i; } System.out.println(fact); } catch (Exception e){} }} |
Updated Example descriptionThis is the updated example of factorial that will evaluates the factorial of number in double which is lies in the range of double data type in java.
Here is the code of program:
import java.io.*; class Factorial1{ public static void main(String[] args) { try{ BufferedReader object = new BufferedReader( new InputStreamReader(System.in)); System.out.println("enter the number"); int a= Integer.parseInt(object.readLine()); double fact= 1; System.out.println("Factorial of " +a+ ":"); for (int i= 1; i<=a; i++){ fact=fact*i; } System.out.println(fact); } catch (Exception e){ System.out.println("Array out of bounds exaception"); } }} |
- Palindrome Number Example in Java
In this section, you will learn about the palindrome number and how to determine any number is palindrome or not. First of all we are going to read about the palindrome number.
Palindrome Number Example in Java
In this section, you will learn about the palindrome number and how to determine any number is palindrome or not. First of all we are going to read about the palindrome number. This is the number that the actual number and after reversing this, in both cases the number is same that is called palindrome number otherwise not. Brief description below:
Description of program:
With the help of this program, we are going to determine whether the given number is palindrome or not. To achieve the desired result, firstly we have to define a class named "Palindrome". After that we will ask the user to enter any integer type number and then we will reverse it. After reversing the number we will check whether the given number is palindrome or not. If the given number is larger, then it will display a message "Out of range!".
Here is the code of this program
import java.io.*;public class Palindrome { public static void main(String [] args){ try{ BufferedReader object = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter number"); int num= Integer.parseInt(object.readLine()); int n = num; int rev=0; System.out.println("Number: "); System.out.println(" "+ num); for (int i=0; i<=num; i++){ int r=num%10; num=num/10; rev=rev*10+r; i=0; } System.out.println("After reversing the number: "+ " "); System.out.println(" "+ rev); if(n == rev){ System.out.print("Number is palindrome!"); } else{ System.out.println("Number is not palindrome!"); } } catch(Exception e){ System.out.println("Out of range!"); } }} |
- Program for calculating area and perimeter of a rectangle
If you are a newbie in Java programming then our tutorials and examples will be helpful in understanding Java programming in the most simplest way. Here after reading this lesson, you will be able to write program for calculating the area and perimeter of a rectangle.
Write a program for calculating area and perimeter of a rectangle
If you are a newbie in Java programming then our tutorials and examples will be helpful in understanding Java programming in the most simplest way. Here after reading this lesson, you will be able to write program for calculating the area and perimeter of a rectangle.
First of all create a class named RecArea under Java.io package. Now define two integer variable 'l' and 'w'. As the program will be based on keyboard numerical input, it is important for every programmer to use correct data without any mistake. In this case the exception methods like try/catch mechanism helps in detecting user input errors. So before starting the functional code, enclosed it with try clause so that any error in the statement causes the execution of the catch clauses.
Now create an abstract buffer class which is the super class of all classes and represents a stream of input bytes. The InputSreamReader reads the character stream and stores it in the buffer class. Now use parseInt for both length and width of the rectangle. This is an instance of class method and is used to convert a string to an integer. Define the area as l*w and perimeter as 2*(l+w) and in the end use the catch exception.
Now compile and run the program and input the value as you see the message and get the ultimate result. If you find any kind of error, then check the whole program again.
Here is the code of the program:
import java.io.*;class RecArea { public static void main(String[] args) { int l=0; int w=0; try{ BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter length of rectangle : "); l = Integer.parseInt(br1.readLine()); System.out.println("Enter width of rectangle : "); w = Integer.parseInt(br1.readLine()); int area = l*w; System.out.println("Area of Rectangle : "+area); int perimiter = 2*(l+w); System.out.println("Perimeter: " + perimiter); }catch(Exception e){System.out.println("Error : "+e);} } } |
- Program to construct a triangle with the ?*?
This lesson in Java programming will teach you the coding for constructing a shape of triangle by using '*'.
Write a program to construct a triangle with the ?*?
This lesson in Java programming will teach you the coding for constructing a shape of triangle by using '*'. First of all make a class named 'triangle' under the Java I/O package and as we have to use the Buffer class, the application of all try and catch block is important for avoiding any kind of error. After creating BufferedReader object and input stream reader define an integer 'a' and apply parseInt method for the conversion of string into integer.
Now apply the for loop and define an integer 'i' and it should be either less than or equal to the integer "a" (the input number). Again define another integer type variable "j" in another for loop. Here in the second for loop "j" the number of times we have to print *.
Now compile and run the program and insert any number on your command window and surely you will get a triangle shape with star *. You can take any other object instead of *.
Here is the code of the program:
import java.io.*;class triangle{ public static void main(String[] args) { try{ BufferedReader object = new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter the number"); int a= Integer.parseInt(object.readLine()); for (int i=1; i<a;i++ ){ for (int j=1; j<=i;j++ ){ System.out.print("*"); } System.out.println(""); } } catch(Exception e){} }} |
- Checking whether a year is leap or not
This tutorial is going to teach you the coding for checking whether a year is a leap year or not. Here, we have taken the year 2000.
No comments:
Post a Comment