This tutorial will introduce you with the Java Programming language. This tutorial is for beginners, who wants to learn Java from scratch. In this beginners Java Tutorial you will learn how to download and install JDK on your machine and then use the JDK
- 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.
- 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.
- 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 named AllEvenNum
under the java.io package.
- 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.
- 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.
- 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.
- 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.
- 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 '*'.
- 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.
- Listing out leap years between certain
period
The programming lesson will teach you the coding for finding and listing out the
leap years between two years. In the following example we have to find out the
leap years between 1990 and 2006.
- Preparing table of a number by using loop
This tutorial will teach you the methods of preparing the
table of a given number by using loop condition. As we know the loop statements
are used to repeat a statement or process multiple times according to a
specified condition.
- Finding out the prime number
This lesson of Java programming language will teach you the coding to find
out whether a given number is prime or not. Here we have used the 'for loop'
statement and given the required condition for a prime number.
- Prime Number in Java
This Java programming tutorial, we will be read how to get prime number
between 1 to given number. First of all we have to define a class "PrimeNumber".
Java OOP Concept
- OOP In Java
Object Oriented Programming or OOP is
the technique to create programs based on the real world. Unlike procedural
programming, here in the OOP programming model programs are organized around
objects and data rather than actions and logic.
Java Examples
- Exception
Handling in Example
Exception, that means exceptional errors. Actually exceptions
are used for handling errors in programs that occurs during the program
execution.
- Java
Identifier Example
In this example you will learn what is identifier and primitive data type of
a identifier. This tutorials will teach you how to use these identifier in
you java programming.
- Java - Variable, Constant and Literal in Java
Variable : You can assign the values to the variable once it has been
declared. The values of the variable can be changed anywhere in the program if
the variable is accessible in that scope. In this example we have used the
variable intvariable to illustrate this.
- Java
Read File Example
In the section of Java Tutorial you will learn how to write java program to
read file line by line. We will use the DataInputStream class to Read text
File Line by Line.
- Copy file Example
This example illustrates how to copy contents from one
file to another file. This topic is related to the I/O (input/output) of java.io
package.
- Java Write to
File Example
In the section of Java Tutorial you will learn how to write java program to
write to a file.
- Java Create
Directory Example
In the section of Java Tutorial you will learn how to write java program to
create directory.
- Deleting Directory or
File
This example illustrates how to delete the specified
file or directory after checking weather the file exists or not. This topic
is related to the I/O (input/output) of java.io
package.
- Java Directory
Listing Example
This example illustrates how to list files and folders
present in the specified directory. This topic is related to the I/O
(input/output) of java.io
package.
- Java Word Count Example
This example illustrates how to count the number of lines, number of words and number of characters in the specified file. Program takes the file name as parameter and it counts the number of words and lines present in the file.
- Applet Example in Java
Applet is a program provided by java which is designed for execution within
the web browser. Applets are mostly used for a small internet and intranet
applications because of small size and it's compatibility among almost all
web browsers.
- Boolean Expression in Java
In this example you will see that how the expression returns a boolean value
which is either true or false. This example illustrates you how to make a
boolean expression by using relational operators and how to use the returned
value of the expression.
- Math Class Example
In this example you will learn about Math class. This example
explains how you can use functions provided by the Math
class like E, PI,
round, abs, ceil,
exp, floor, IEEEremainder,
max, min, pow,
random, rint, sqrt
etc. to manipulate the mathematical operation in your program.
- String Buffer Example
In this example you will learn about StringBuffer class. This example
explains how you can use functions provided by the StringBuffer class like append,
insert, reverse, setCharAt, charAt, length, deleteCharAt, substring, delete,
capacity etc. to manipulate the string operation in your program.
- Type Casting Example
This example illustrates that what is type casting? Type Casting
refers to changing an entity of one datatype
into another. This is important for the type conversion in developing any
application.
- Java - Array in Java
Array: Array is the most important thing in any programming language.
By definition, array is the static memory allocation. It allocates the memory
for the same data type in sequence. It contains multiple values of same types.
- Copying an array to
another
In this tutorial, you will learn how to copy data from one array to
another. Here, providing you an example with code and its explanation to
given below.
- charAt() method in Java
In this section, you will get the detailed explanation about the charAt()
method of String class. We are going for using charAt() method.
- length() method in Java
In this section, you will learn how to use length()
method of the String class. We are going to using length() method.
- toUpperCase() Method In Java
Here, you will see the procedure of
converting letters of the string in uppercase letter.
- String Array in Java
In this section, you will learn how to use string
array in Java. Here, you will see how to declare a string array and the syntax
for using in the program.
- Average of Array
In this section, we will learn how to get an average of array. For this,
first of all we have to define a class name "ArrayAverage"
that has double type array to contain some values.
- Matrix Example in
Java
In Java tutorial, you will learn about array and matrix. An array is the
collection of same data type values. If we create a variable of integer type
then, the array of int can only store the int values.
- Sum of two Matrix
In this section, we are going to calculate the sum of two matrix and
containing its rows and columns. See below for better understanding to this.
- Multiplication of
two Matrix
This is a simple java program that teaches
you for multiplying two matrix to each other. Here providing you Java source
code with understanding the Java developing application program.
- Square Elements of
Two Dimensional Array
This is a simple java program for implementing the two dimensional array
program and its square. This session provide you the best explanation with
mathematical operation.
- Check Even-Odd:-Example
Now, you have to learn about the syntax of If - Else construct. How the If -
Else construct used to flow the program control as for as needed.
- Java - Break statement in java
Break: The
break statement is used in many programming languages
such as c, c++, java etc. Some times we need to exit from a loop before
the completion of the loop then we use break
- Java - Continue statement in Java
Continue: The continue statement is used in many programming languages
such as C, C++, java etc. Sometimes we do not need to execute some statements
under the loop then we use the continue statement that stops the normal flow of
the control and control returns to the loop without executing the statements
written after the continue statement.
- Java - The switch construct in Java
Switch is the control statement in java which also turns the normal flow
control of the program as per conditions. It works same as If-Else
construct. This is the difference between Switch
and If-Else construct that switch is used
for reduce the if statements.
- Switch Statement
example in Java
This is very simple Java program for implementing the switch
statement. Here, you will learn how to use the switch statement in your java
program for developing java application.
- While Loop
Loop is the control statement of any language in which whenever you want to perform the repetitious work then you
use the Loop control
statement.
- do-while Loop in Java
In some conditions in java, you need to run some statements and processes
once at least. For that you have to use the do while loop in after that check the condition then we use the do-while loop
statement.
- For Loop in Java
The for loop is the type of looping construct. It also works as while loop
construct but it provide the initialization, condition and the increment
is same written in the for construct.
- Java - Class, Object and Methods in Java
Class : Whatever we can see in this world all the things are a
object.
And all the objects are categorized in a special group. That group is termed as
a class.
- Java - Constructor in java
Constructor is the method which name is same to the class. But there are
many difference between the method (function) and the Constructor.
- Constructor Overloading in Java
Constructors are used to assign initial values to
instance variables of the class. A default constructor with no arguments will be
called automatically by the Java Virtual Machine (JVM).
- Use of "this" keyword in java
The keyword this is useful when you need
to refer to instance of the class from its method.
- Java - Threads in Java
Thread is the feature of mostly languages including Java. Threads allow the
program to perform multiple tasks simultaneously. Process speed can be increased
by using threads because the thread can stop or suspend a specific running
process and start or resume the suspended processes.
- Occurrences of elements in an Array
This section illustrates you how to calculate occurrences
of elements in an array. Occurrences means, the how many times an element occurs
in the array.
- Change
the user input to integer
Isn't it a
very tedious job to enter the values each time before the compilation in the
method itself. Now if we want to enter an integer value after the compilation of
a program and force the JVM to ask for an input, then we should use
Integer.parseInt(string str) .
- Making Tokens of a String
We have to break a String into tokens as well as
we will know how many tokens has been generated. That's what we are trying to do
in this program.
- Array List
Example in java
In this example we are going to show the use of java.util.ArrayList.
We will be creatiing an object of ArrayList class and performs various
operations like adding removing the objects.
- Converting
a Filename to a URL
A file object is used to a give a filename. Creating the File
object doesn't mean that a file exists. It may be that the does not exist.
Suppose if the file exists, first of all we need to convert the file object
in URL, for this we use a method toURL(). It returns a URL
object and throws MalformedException. After this we will convert this
URL to a file object by using getFile() method. We will read
this file by using BufferedReader object.
- Creating a
temporary file
In this program we are going to make a temporary file, which will be
deleted automatically by the garbage collector when the program ends.
- Example of Date class
We can use Date class to use current date. In this program we are
creating a Date object and by using it we are going to display the
current date and time. It implements Serializable, Cloneable,
Comparable interfaces. Its known subclasses are Date, Time,
Timestamp.
- Determining
if two Filename paths refer to the same file.
This program is going to determine that the file name refers to the
same file or not. What you need to do is to create two File object
and pass the file name in the constructor of both the File class. Now
match both the file instances. It will return false if the file paths are
not equal. To send the true value we first have to normalize both the paths,
only after that it will return true.
- Determining if a
Filename path is a file or a directory
You can solve this problem very easily. What you need to do, just
create a File object and pass some file name or a directory in it.
- Determining
if a File or Directory exist
This is a very simple
problem. You just have to determine whether a file or directory exists or
not. If it exists it returns true otherwise in the else
condition, it retunes vice-versa.
- Forcing Updates
to a File to the Disk
In this program we are going to update a file forcefully. Firstly we
have to make an object of OutputStream and pass a file name in which
you want to update in the constructor of a FileOutputStream class.
After that pass some data to the stream. The methods we are using in this
class has already been discussed in other topics.
-
Link
List Example in Java
In this example we are going to show the use of java.util.LinkedList
class. You will be creating an object of link list class and performing
various operation like adding and removing object.
-
Listing
the File System Roots
In this example we will list out all the drives in the disk. this
requires an array of File class where all the drives will get stored
by using only one method.
-
Reading
a File into a Byte Array
In this program we will read a file in the form of a byte array. We
can do it very easily. Firstly we will create an object of File class
and pass the name of the file which we want to read, in the constructor of File
class. We will use DataInputStream to read primitive data types.
-
Implementing
a Serializable Singleton
In Singeton classes only one instance will be created. We are going
to serialize the class. This can be done very easily. What we need to do is
to implement a java.io.Serializable interface. We will use a
method of Serializable interface that is readResolve().
-
Example
of Java Stack Program
Stack is like a bucket we you can enter objects and retrieve it.
Here in the example describes the methods to prepare an example of Java
stack program. The Stack
class represents a
last-in-first-out (LIFO) stack of objects. It extends class Vector
and implements Cloneable, Collection, List, Serializable interfaces.
We mostly use push and pop operations.
- Vector Example in java
In this example we are going to show the use of java.util.Vector class. We
will be creating an object of Vector class and performs various operation
like adding, removing etc. Vector class extends AbstractList and
implements List, RandomAccess, Cloneable, Serializable. The size of a
vector increase and decrease according to the program. Vector is
synchronized.
- Calculating the
checksum of a file/Adler32
Adler32: It is a error checking
technique used to check the accuracy of data while transmitting it to the
other end. Errors are checked while the data is transferring. This scheme is
as reliable as CRC32, but it is much faster to compute.
- Calculating the
Checksum of the file/CRC32
CRC: CRC means Cyclic
Redundancy Check. It is a error checking technique used to check the
accuracy of data while transmitting it to the other end. Errors are checked
while the data is transferring. CRC performs some type of calculation before
transmitting the data and send the result to the other end. The other end
repeats the same operation before accepting the data. If both the devices
get the result same, it means the transmission is error free.
- Compressing the
file into GZIP format.
This example will help to understand the concepts of GZIP. GZIP
compresses the actual size of data files. It is a file compression
utility program which can be found very easily found on internet. Its
resultant file will have a .gz extension. Its main advantage is that
it provides excellent levels of compression. It can be used where you want
to save the file in less available area. It will compress the file to the
great extent. It is a part of java.util.zip package.
- Constructing
a File Name path
This Java programming tutorial will teach you how you can construct
a file name path. By using the constructing filename path it is possible to
set dynamic path, which is helpful for mapping local file name with
the actual path of the file.
- Use of Array in
Java
This Java programming example will teach you the the concepts of arrays
so that you can write a program on array by yourself. An array works a
container object which can hold a values of same type. The length of an
array is created when the array is going to be created. Arrays can be
created in following ways. One way to create a array is by using new
operator. e.g.. : a = new int[5];. It will create an array of type
int which can store 5 integer values. The next way to define array is just
same as we have defined an array in the program below. Array should be
used, where you have to store multiple values of the same type.
For example if we have to store months of the year
then the better idea is to make an array of it.
- Example
of static method
This Java programming example will teach you the way to define a static
methods. In java we have two types of methods, instance methods and
static methods. Static methods can't use any instance variables. The this
keyword can't be used in a static methods. You can find it difficult to
understand when to use a static method and when not to use. If you have a
better understanding of the instance methods and static methods then you can
know where to use instance method and static method.
- Uncompressing the
file in the GZIP format.
This example will help you to understand the concepts of GZIP. Sometimes
it happens that when you download a file or any PDF file you get it in
compressed mode. This means that the file is compressed, but can't read it
in compressed form so, it needs to be uncompressed. There are various
uncompress ion utility program which can be found very easily found on
internet. If you are uncompressing a PDF with the extension .gz, then its
resultant file will have a .PDF extension. Its main advantage is that
it will uncompress the PDF file so that you can be able to read it. This
utility is a part of java.util.zip package which provides classes and
methods to compress and decompress the file.
- Example of a
instance variable
When a number of objects are created from the same class, the same
copy of instance variable is provided to all. Remember, each time you
call the instance the same old value is provided to you, not the updated one
. In this program we are showing that how a instance variable is called in
each instance, but the copy remains same irrespective the counter we have
used in the constructor of a class. We can't call the non-static variable in
a main method. If we try to call the non-static method in the main method
then the compiler will prompt you that non-static variable cannot be
referenced from a static context. We can call the non-static variable by
the instance of the class.
- Example of a class
variable (static variable)
This Java programming example will teach you how you can define the
static class variable in a class. When a number of objects are created from
the same class, each instance has its own copy of class variables. But this
is not the case when it is declared as static static.
- Swapping of two numbers
This Java programming tutorial will teach you the methods for writing
program to calculate swap of two numbers. Swapping is used where
you want to interchange the values. This program will help you to increase
your programming ability.
- Swap two any numbers (from
Keyboard)
This is a simple Java Oriented language program. If you are
newbie in Java programming then our tutorial and example are helpful for
understanding Java programming in the simplest way.
- Calculating the
checksum of a Byte Array by using Adler32
Adler32: It is a error checking technique used to check the
accuracy of data while transmitting it to the other end. Errors are checked
while the data is transferring. This scheme is as reliable as CRC32,
but it is much faster to compute.The byte is an 8- bit signed
primitive data type quantity. The minimum value the byte is -128 and maximum
value is +128. It is signed because it can take both negative and positive
value. The Byte class wraps a value of primitive type byte in an object. A
object of type Byte contains a single field whose type is byte.
- Calculating
the checksum of a Byte Array by using CRC32
CRC32: It is a error checking technique used to check the accuracy of
data while transmitting it to the other end. Errors are checked while the
data is transferring.
- Comparing the
File Dates
This java example will help you to compare the dates of files. In java
we have the option to compare two or more files. This is useful when we have
to compare the files. Mostly it happens that after creating a file or a
directory we forgets when it was made or last modified, and sometimes it
becomes essential to know the exact date and time of creation. By this
example you will be able to understand it.
- Getting a
absolute path
To find a file or directory it is very necessary to know the path of the
file or directory so that you can access it. If you know the path then it is
very easy to work on it. Suppose a situation where a problem comes in front
you where you don't know the path of the file, then what will you do. Then
this problem can be solved by using a method getAbsolutePath(). The
method getAbsolutePath() should be used where we don't know the exact
path of the file.
- Making
Tokens of a Java Source Code
If you are new 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 break java program into
tokens.
-
Add two big numbers
In this section, you will learn how to add two big
numbers. For adding two numbers implement two big decimal numbers then apply the Sum() method that takes both numbers.
-
String Buffer insert and append example
In this section, you will learn how to insert StringBuffer
and append data. The StringBuffer is a class that implements multiple
sequence of characters. The following program construct a string buffer that has
'0' capacity.
-
Character Comparison Example
In this section, you will learn how to compare
characters in Java. The java.lang package provides a method for comparing
two case sensitive strings. The compareTo()
method compares two strings on the basis of Unicode of each character of the given
strings.
-
Comparison character cases
ignoring case sensitiveness in
java
In this section, you will learn how to compare two strings
ignoring case sensitiveness Java provides the method that ignores
cases, it only compare the character's sequence. The compareToIgnoreCase()
method compares two strings and ignores its cases.
-
Combine String example
In this section, you will learn how to combine or merge
two strings in java. The java.lang package provides the method
that helps you to combine two strings and making into single string.
-
Compare string example
In this section, you will learn how to compare two
strings in java. The java lang package provides a method to compare two
with their case either upper and lower. The equals()
method provides the facility of comparing the two strings.
-
Replace Character in String
This example replaces a character with a specified character in a given
string.
-
Convert String to a float
In this section, you will learn how to convert string
data into a float type data. The java.lang package provides the
method that converts string data into float and also converts it into double.
-
Convert integer type data into binary, octal and
hexadecimal
In this section, you will learn how to convert an
integer type data into binary, octal and hexadecimal. The java.lang package
provides the facility for converting the data integer into binary, octal, and
hexadecimal.
-
Round two decimal places
In this section, you will learn about how to round the
figure up to two decimal places in java.
-
String length example
In this section, you will learn how to get the length
of given string. The lang package of java provides the facility for getting
the length of string. You will see in the following program for getting the
length of string by using the length() method that returns integer type
value of any given string
-
Trim String Example
In this section, you will learn how to remove the blank
spaces. For removing the blank spaces use trim()
method that removes the blank spaces and shows only string.
-
String Start with Example
In this section, you will learn how to check the given
string that start from the specified character in java. The following program checks
the started string with "Wel".
-
String End with Example
This section tells you, how to determine the given
string ends with specified string. The following program helps you to check whether the given string starts with the specified string is true or false by applying
endsWith() method and returns Boolean types values either 'true' or 'false'.
-
Passing Command Line Arguments
In this example we are going to learn how we can
pass values on command line argument .
-
Decimal Format Example
In this example we are going to format
a decimal value.
-
Delay Example
In this section we learn how we can
make a program so that there are delay between the execution of program.
-
Line Number
Reader Example
In this example we are reading
number of lines.
-
Operating System Information
In this example we are learn how we can get
information about our operation system. In this example we are getting the
OS name, its version and architecture of OS. We are using getProperty(String
key) to get the property of the OS.
-
Taking
Substring
In this example we are taking a sub string from a given string.
-
Find Current Temp
Directory
In this
example we are find the current Temp directory.
-
Find User Home Directory
In this
example we are find user home directory.
-
Write Text into File
In this example we are writing text
into file. In this example we are initialize string to write into file. We
are creating file in which we are writing string by use of write()
method.
-
Word Count
This example counts the number of occurrences of a specific word
in a string. Here we are counting the occurrences of word "you" in a
string. To count it we are using countMatches() method.
-
String
Reverse Using StringUtils
In this example we are going to reverse a given string using StringUtils
api. In this example we are reversing a string and the reversed string is
delimited by a specific character.
-
String Reverse in
Java
In this example we are going to reverse a given string.
-
Singleton Design
Pattern
This example explores how to implement a SingletonPattern on a
class in java.
-
Runtime Exec Example
In this example we are going to execute an application using java
program.
-
Get Environment
Variable Java
In this example we are getting environment variable. To get the
environment variable we use getenv() method.
-
Find Day of Month
This example explores how to find the day of a month and the day of a
week This example sets the year as 2007 and the day as 181. The example
finds the day of a month and the day of a week by using get(field_name)
method.
-
Find the Day of
the Week
This example finds the specified date of an year and a day of a
week from calendar.
-
Date Difference
This example learns how to make the difference between two dates and how
to convert milliseconds into seconds, seconds into minutes, minutes into hours,
hours into days.
-
Calendar Example
In this section, we are discussing the entire
functionality of
java.util.Calender class. In
the following code segment we are performing various operations on Date
object using its methods.
-
Date Comparison
In this section we are discussing the comparison of
two dates, whether they are equal or not, the first date comes before the other or
not, the first date comes after the other or not, by using
the equals(), before() and after() methods of the date class.
-
DateExample
In this section we are discussing about the specific date
using the Date class object, retrieving milliseconds from the Date object,
elapsed time, date contained in the Date object before and after it has change
and displaying both the values on the console.
-
DateForms
In this section we are demonstrating the retrieval of
date from the Date object in various forms.In
the following code segment we are performing various operations on Date
object
-
MonthCalender
In this section we are displaying the days of a
specific month and the calendar format of the days for this month. We are
also checking whether the year is leap or not.
-
Auto Increment and Decrement
In this example we are demonstrating the significance of
prefix and postfix operators by
incrementing and decrementing the value of a variable that how both are different with each other.
-
Randomizer
In this example we are discussing how to generate the
random numbers by using the random() method of the Math class and change them
into different random numbers by apply some mathematical operations onto them
and display 10 random numbers on the console.
-
Reverse String
In this example we are demonstrating the command line
argument and displaying it in its backward direction on the console.
-
Garbage Collection
In the example we are describing how the garbage
collector works.
-
Check Empty
String
In this example we are checking a sting object containing empty or null
value. Apache has provided two methods isBlank() and isNotBlank()
for checking the strings..
-
Access Static
Member Of The Class Through Object
Static methods are special type of methods that work
without any object of the class. Static methods are limited to calling other static
methods in the class and to using only static variables.
-
Command Line Arguments in Java Program
Java application can accept any number of arguments
directly from the command line. The user can enter command-line arguments when
invoking the application.
-
Read a string
and reverse it and then print in alphabetical order.
A string is a contiguous sequence of symbols or values,
such as a character string (a sequence of characters) or a binary digit string
(a sequence of binary values).
-
Array to Collection
List interface is a member of the Java Collection
Framework and extends Collection interface. List
interface is an ordered collection which follows insertion order, typically allow duplicate
elements. The List interface provides a special iterator, called ListIterator
for insertion, replacement and bi-directional access.
-
Copy a file to other destination and show the modification time of destination file
This is detailed java code that copies a source file to
destination file, this code checks all the condition before copy to destination
file, for example- source file is exist or not, source file is readable or not
etc.
-
Java Arrays
Tutorial
Java Arrays Tutorials and examples .
-
Java String Split Example
In this section, you will learn how to split string into different parts
using spit() function.