Java is an Object Oriented
Programming language developed by Sun Microsystems. Modeled after C++, the Java language was
designed to be small, simple and portable across platforms and operating
systems both at source and the binary level.
Java is a programming language,
suited to design software to work in conjunction with the Internet. It is an object oriented programming
language. Java is best known for its
capability to run on World Wide Web pages.
Netscape Navigator and Internet Explorer browsers can download a java
program from web page and run it locally on web on the user’s system.
These programs are called
Applets; they appear in a web page like images.
It can be interactive, takes user input, responds it and presents
ever-changing content. They are used to
create animation, figures, games forms etc. to immediate respond to input of
the reader.
Applets are downloaded over World
Wide Web as HTML pages, graphics, Web sites etc. They are written with java language, compiled
into a form to run as a program, and placed on Web Server.
Features of Java
Java is platform independent
Java has the capability to run
the same program on different platforms and operating systems. Java is platform independent at source and
binary level. Platform independence is a
program’s capability of moving easily from one computer system to another.
At the source level Java’s
primitive data types have consistent sizes across all development
platforms. Java’s foundation class
libraries make it easy to write code that can be moved from platform to
platform without the need to rewrite it to work with that platform.
Java binary files are also
platform independent and can run on multiple problems without the need to
recompile the source. This is because
Java binary files are in the form called bytecodes.
Bytecodes are a set of instructions
that look like some machine codes, but that is not specific to any one
processor. The Java development has two
parts; a Java compiler and a Java interpreter.
Compiler takes your Java program and instead of generating machine code
from your source files it generates bytecodes.
To run a Java program, you run a
program called a bytecodes interpreter, which in turn executes your
java program. You
can either run the interpreter by itself, or for-applets there is a bytecodes interpreter
built into HotJava and other java-capable browsers that runs the applet for
you.
Java programs are saved as text
files before compiling. Which are
created on any platform that supports java.
Source is called as source code,
is the set of programming statements which are entered into a text editor when
creating a program. Source code is
compiled into byte code to run by java virtual machine. Java applications, can only run on a system
where java virtual machine has been installed.
Java is object oriented
Like most object-oriented
programming languages, java includes a set of class libraries that provide
basic data types, system input and output capabilities and other utility
functions. These basic classes are part
of java development kit, which also has classes to support networking, common
internet protocols and user interface toolkit functions. Java also inherits many of its OOP concepts
from C++ and other OOPS languages also.
Java is easy to learn
It is smaller and simpler than
comparable languages. It is easier to
write, compile debug and also to learn.
Java applications are stand alone java programs; they don’t require web
browsers to run the programs.
Java source files are saved as
plain text files, can be created with any text editor or word processor saved
as plain text. For example Open notepad;
or in DOS Environment type edit and write or use Ms-Word. Save the java file with .java extension.
Example 1 (Hello.Java)
Invoke notepad and write
following Application source code.
class Hello{
public static void
main(String[]arguments){
system.out.println(“Hello
World!!”);
}
}
Save the above file with
Hello.java. Thus java source files must
be saved with .java extension. They are
compiled into byte code, which creates class files with .class extension.
Now compile the above code on the
Java compiler as follows:-
C:\jdk1.3\bin\> javac
Hello.java
If code do not have any error
then the java class file will be created and After successful compilation the
output can be viewed with following command:
C:\jdk1.3\bin\> java Hello
Objects and classes
Object oriented programming is
modeled on the observation in real world objects; which are made up of many
kinds of smaller objects.
A class is template used to
create multiple objects with similar features.
Classes embody all features of a particular set of objects. A class library is a group of classes
designed to be used with programs.
Attributes are individual things
that differentiate one class of objects from another and determine the appearance,
state and other qualities of that class.
Attributes of a class objects
also can include information about an objects state. In a class attributes are defined by
variables. They can be considered
analogous to global variables for each object of that class, which can have
different value for its variables, called as instance variables.
Instance variable is an item of
information that defines an attribute of one particular object. Object class defines what kind of attribute
it is, and each instance stores its own value for that attribute. Instance variables also called object
variables. Each class attribute has a
single corresponding variable that can be changed in an object by changing
variables value.
Instance variable can be given a
value while creating the object that stay constant through out the objects
life.
Class variable is an item of
information that defines an attribute of entire class.
Behavior of a class of objects
determines what object of that class to do change their attributes and also
what they do when other objects ask them to do something.
Method are group of related
statements in a class of objects that act on themselves and an other classes
and objects, used to accomplish specific task.
Creating a java File
Example 1
Open a Text Editor and write the
following code:-
class Jab{
String color;
String sex;
boolean hungry;
int age;
void feedJab( ){
if (hungry==true){
System.out.println(“Ya Hoo--!”);
hungry=false;
}else
System.out.println(“No I am Not
Hungry”);
}
void showAttributes( ){
System.out.println(“This is a
“+sex + “ “+ color + “Jab.”);
if (hungry==true)
System.out.println(“Jab is
Hungry”);
else
System.out.println(“The Jab is
Full”);
}
public static void main(String
arguments[])
{
Jab j= new Jab( );
j.color=”pale Yellow”;
j.sex=”male”;
j.hungry =true;
System.out.println(“Calling
showAttributes...”);
j.showAttributes( );
System.out.println(“.........”);
System.out.println(“Feeding The
Jabs..”);
j.feedJab( );
System.out.println(“......”);
System.out.println(“Calling showAttributes...”);
j.showAttributes( );
System.out.println(“......”);
System.out.println(“Feeding The
Jabs..”);
j.feedJab( );
}
}
Save the file with Jab.java and
compile it with the following command:-
à javac Jab.java
After successful compilation run
the program with following command:-
à java Jab
Statements and Expressions
A statement is the simplest
thing, you can do in Java. A statement
forms a single java operation. All the
following are java expression:
int k=0;
import Java.io;
Statements sometimes return the
value these kind of statements are called as expressions. The most important thing to remember about
Java statement is that each one ends with the semicolon (;).
Variables and Data Types
A variable is a location in
memory in which values can be stored.
They have a name, a type and a value.
Before you can use a variable, you have to declare it. After it is declared you can then assign
value to it. Java has three kinds of
variables:
Example 2
class Var{
public static void main(String args[]){
int a =10, b = 20;
int c =a +b;
System.out.println("c = "+ c);
}
}
Instance variables
Instance variables are those that
hold data for an instance of a class.
Class variables
Class variables hold data that is
shared among all the instance of a class.
Example 3
class ScopeDemo{
int p;
ScopeDemo( ){
p=90;
methodScope( );
classScope( );
methodScope( );
classScope( );
}
void methodScope( )
{
System.out.println("Method
Scope");
int p=9001;
System.out.println("p =
"+p);
p++;
System.out.println("p =
"+p);
System.out.println("
");
}
void classScope( )
{
System.out.println("Class
Scope");
System.out.println("p =
"+p);
p++;
System.out.println("p =
"+p);
System.out.println("
");
}
public static void main(String []
args){
new ScopeDemo( );
}
}
Above program indicates the scope
of the method.
Local variables.
Local variables are declared and
used inside method definition.
Variable names in Java can begin
with a letter, an underscore( _ ), or a dollar sign( $ ). A letter is defined as ‘A’ – ‘Z’, ‘a’ –
‘z’. variable names can not begin with a
number. Symbols like ‘+’ or ‘@’ can not
be used inside the variable names. All
the characters in the name of a variable are significant and case is also
significant.
Java language uses the Unicode
character set. Unicode is a character
set definition that not only offers characters in ASCII character set, but also
several million other characters for representing most international alphabets.
In addition to the variable name,
each variable declaration must have a type.
The variable type can be of the eight basic primitive data types. Six of them are number types (four integers
and two floating point types); one is the character type, and one is the
Boolean type for truth values. They are
called primitive because they’re built into the system and are not actual
objects, which makes them more efficient to use.
Comments
Comments are the non executable
statements in any program. Java has
three kinds of comments.
à For multi-line comments Java uses /* and */.
à Double slashes (//) can be used for a
single line of comment. All the text up
to the end of the line is ignored by the compiler while executing the program.
à The final type of comment begins with /** and ends with */.
Literals
Literal is a programming language
term, which essentially means that what you type is what you get.
Number Literals
Number literals can be
categorized into two sub-types: Integers and Floating Point numbers.
Boolean Literals
Boolean Literals consist of the
keyword ‘True’ or ‘False’.
Character Literals
Character Literals are expressed
by a single character surrounded by single quotes: ‘a’ ‘3’ and so on. Characters are stored as 16 bit Unicode
characters.
String Literals
A combination of characters is a
string. Strings in Java are instances of
the class String. Strings are not simple
arrays of characters. Because string
objects are real objects in Java, they have methods that enable you to modify
strings very easily. String literals
consist of a series of characters inside double quotes.
“Hello “
“ “ // Empty String.
String can contain character
constants such as new line, tab and Unicode characters.
Expressions and Operators
Expressions are statements that
return a value. Operators are special
symbols that are commonly used in expression.
Arithmetic
Java has five operators for basic
arithmetic
Ø
+
Ø
-
Ø
*
Ø
/
Ø
%
Addition, Subtraction,
Multiplication etc are the basic operators.
Integer division results in a integer.
Modulus(%) gives the remainder once the operands have been divided.
Assignment
Variable assignment is a form of
expression. In fact because one
assignment expression results in a value, you can string them together like
this.
A=b=c=0;
The right side of an assignment
expression is evaluated before the assignment takes place. This means that expression such as
a=a+1;. In this the value is added to
’a’ and the new value is reassigned to ‘a’.
The other shorthand assignment
operators are:-
Expression Meaning
x += y x = x+y
x
-= y x
= x-y
x *= y x = x*y
x
/= y x
= x/y
Incrementing and Decrementing
In Java ++ and - - operators are
used to increment or decrement a value by 1.
For example a++ increments the value by 1 and a – decrements the value by 1. These
increment or decrement operators can be prefixed or postfixed.
For example:-
a = b++;
a = ++b;
in the above example the first
expression the value of b is first assigned to a and then the variable b is
incremented. In the second expression
first variable b is incremented and the value of b is assigned to a.
Comparisons
Java has several expressions for
testing equality and magnitude. All
expressions return a Boolean value.
Operator Meaning Example
= = Equal x = = 3
! = Not equal x != 4
< Less than x < 5
> Greater than x >6
Logical Operators
Expressions that result in
Boolean values can be combined by using logical operators that represent the
logical combination AND, OR, XOR, and logical NOT.
For AND combination, use either
the ‘&’ or ‘&&’. The
expression will be true only if both the operands tests are true, else if one
of the operand test is false the expression results in false. For OR expression, use either | or ||. OR expression results in true if either one
of the operand is true, if both the operands are false then the expression is
results in false. For NOT, use the !
operator with the single expression argument.
The value of the NOT expression is the negation of the expression.
Bitwise Operators in Java
Operator Meaning
& Bitwise
AND
| Bitwise
OR
^ Bitwise
XOR
<< Left
shift
>> Right
shift
>>> Zero
fill right shift
<<= Left
shift assignment(x = x <<y)
>>= Right
shift assignment(x=x>>y)
>>>= Zero
fill right shift assignment(x=x>>>y)
x&=y AND
assignment(x=x & y)
x | = y OR
assignment (x= x | y)
Operator Precedence
Operator precedence determines
the order in which expression is evaluated.
[ ] . ( )
(function call) Left
to right
! ~ ++ -
+(unary) -
(unary)( ) (cast) new Right to
left
* / % Left
to right
+ - Left
to right
<< >> >>> Left
to right
< <= > >= Left to
right
= = != Left
to right
& Left
to right
^ Left
to right
| Left
to right
&& Left
to right
| | Left
to right
?: Left
to right
= += -= *= /= %= &= |= <<= >>=
>>>= Right to left
String Arithmetic
Strings are sequence of
characters, such as “Hi”. Java does not
have a built-in string type. Instead,
the standard java library contains a predefined class.
String e = “ “ ; // empty string
String wish = “Hello “;
Java allows you to use the ‘+’
sign to join two strings together.
String morn = “Good”;
String wish =”Morning”
String message = morn +” “+wish;
Above code will make the values
of string variable message “Good Morning”.
We can also extract substring from a larger string with the substring
method of the String class.
String message = “Good Morning”;
String s1 =
message.substring(0,4);
The length of the string can be
find with the length( ) function. And to
find whether two strings are equal or not equals method is used.
String message = “Good Morning “;
Int I = message.length( );
The above statement returns the
length for the string message.
String s1 =”Hello”;
s.equal(t);
The above statement returns true
if String var t contains “Hello” else it returns false.
No comments:
Post a Comment