Java is a high-level
programming language originally developed by Sun Microsystems and released in
1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the
various versions of UNIX.
The new J2 versions
were renamed as Java SE, Java EE and Java ME respectively. Java is guaranteed
to be Write Once, Run Anywhere.
Java is:
Object Oriented: In
Java, everything is an Object. Java can be easily extended since it is based on
the Object model.
- Platform independent: Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run.
- Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP Java would be easy to master.
- Secure: With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.
- Architectural-neutral: Java compiler generates an architecture-neutral object file format which makes the compiled code to be executable on many processors, with the presence of Java runtime system.
- Portable: Being architectural-neutral and having no implementation dependent aspects of the specification makes Java portable. Compiler in Java is written in ANSI C with a clean portability boundary which is a POSIX subset.
- Robust: Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking and runtime checking.
- Multithreaded: With Java's multithreaded feature it is possible to write programs that can do many tasks simultaneously. This design feature allows developers to construct smoothly running interactive applications.
- Interpreted: Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light weight process.
- High Performance: With the use of Just-In-Time compilers, Java enables high performance.
- Distributed: Java is designed for the distributed environment of the internet.
Example
Let's look at how to save the file, compile and run
the program.
Please follow the
steps given below:
Step 1 Open notepad and add the code as above.
Step 2 Save the file as: Hello.java.
Step 3 Open a command prompt window and go to
the directory where you saved the class. Assume it's E:\.
Step 4 Type E: enter
Step 5 Then type cd YourFolderName
Step 6 Then javac hello.java(for compilation)
Step 7 Then java hello (for run )
Step 8 You will be able to see ' Hello World '
printed on the window.
Java - Object & Classes
Java is an
Object-Oriented Language. As a language that has the Object Oriented feature,
Java supports the following fundamental concepts:
Polymorphism
Inheritance
Encapsulation
Abstraction
Classes
Objects
Instance
Method
Message Parsing
Objects in Java:
Let us now look deep
into what are objects. If we consider the real-world we can find many objects
around us, Cars, Dogs, Humans, etc. All these objects have a state and
behavior.
If you compare the
software object with a real world object, they have very similar
characteristics.
Software objects also
have a state and behavior. A software object's state is stored in fields and
behavior is shown via methods.
Creating an Object:
As mentioned
previously, a class provides the blueprints for objects. So basically an object
is created from a class. In Java, the new key word is used to create new
objects.
There are three steps
when creating an object from a class:
Declaration: A
variable declaration with a variable name with an object type.
Instantiation: The
'new' key word is used to create the object.
Initialization: The
'new' keyword is followed by a call to a constructor. This call initializes the
new object.
Example of creating an
object is given below:
Classes in Java:
A class is a blue
print from which individual objects are created.
A sample of a class is
given below:
public class Dog{
String breed;
int ageC
String color;
void barking(){
}
void hungry(){
}
void sleeping(){
}
}
Java - Basic Datatypes
There are two data
types available in Java:
- Primitive Data Types
- Reference/Object Data Types
Primitive Data Types:
There are eight
primitive data types supported by Java. Primitive data types are predefined by
the language and named by a keyword. Let us now look into detail about the
eight primitive data types.
- Byte
- Short
- Int
- Long
- Float
- Double
- Boolean
- Char
Reference Data Types:
Reference variables
are created using defined constructors of the classes. They are used to access
objects. These variables are declared to be of a specific type that cannot be
changed. For example, Employee, Puppy etc.
Class objects, and
various type of array variables come under reference data type.
Java - Variable Types
A variable provides us
with named storage that our programs can manipulate. Each variable in Java has
a specific type, which determines the size and layout of the variable's memory;
the range of values that can be stored within that memory; and the set of
operations that can be applied to the variable.
data type variable [ = value][, variable [= value]
...] ;
Here data type is one
of Java's datatypes and variable is the name of the variable. To declare more
than one variable of the specified type, you can use a comma-separated list.
Following are valid examples of variable declaration
and initialization in Java:
int a, b, c;
// Declares three ints, a, b, and c.
int a = 10, b = 10;
// Example of initialization
byte B = 22;
// initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value
of PI.
char a = 'a';
// the char variable a iis initialized with value 'a'
This chapter will
explain various variable types available in Java Language. There are three
kinds of variables in Java:
- Local variables
- Instance variables
- Class/static variables
Local variables Example
Instance variables Example
Class/static variables Example
Loop in java
Java programming
language provides the following types of loop to handle looping requirements.
For Loop
A for loop is a
repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.
A for loop is useful
when you know how many times a task is to be repeated.
Syntax:
The syntax of a for
loop is:
for(initialization; Boolean_expression; update)
{
//Statements
}
Example
While Loop
A while loop statement
in java programming language repeatedly executes a target statement as long as
a given condition is true.
Syntax:
The syntax of a while
loop is:
while(Boolean_expression)
{
//Statements
}
Example
do...while loop
A do...while loop is
similar to a while loop, except that a do...while loop is guaranteed to execute
at least one time.
Syntax:
The syntax of a
do...while loop is:
do
{
//Statements
}
while(Boolean_expression);
Example
Java - Decision Making
Java programming
language provides following types of decision making statements. Click the
following links to check their detail.
Statement
|
Description
|
An if
statement consists of a boolean expression followed by one or more
statements.
|
|
An if
statement can be followed by an optional else statement, which
executes when the boolean expression is false.
|
|
You can use
one if or else if statement inside
another if or else if statement(s).
|
|
A switch statement
allows a variable to be tested for equality against a list of values.
|
if statement in java
Example
if-else statement in java
nested if statement in java
Example
switch statement in java