Академический Документы
Профессиональный Документы
Культура Документы
Before starting to learn java concepts, let us plunge into its history and see how the
language originated.
In 1990, Sun Microsystems Inc has conceived a project to develop software for
consumer electronic devices that could be controlled by a remote. This project was
called Stealth project but later its name was changed to Green Project.
They are planning to start this project it requires a team, James Gosling is also
part of that team. His role is what technology (language) is suitable for this project.
Initially he thought C, C++ but it is system dependent language so he started
developing a new language which is completely system independent, cross plat
form. This language was initially called oak. Since this name was registered by
some other company, later its name was changed to Java.
Why the name Java? James Gosling and his team members were consuming a lot
of tea while developing this language. They felt that they were able to develop a
better language because of the good quality tea they had consumed. So the tea
also had its own role in developing this language and hence, they fixed the name
for the language as Java. Thus, the symbol for Java is tea cup and saucer.
Features of Java: Apart from being system independent language they are other
features also there
Simple
Object-oriented
Robust
Secure
System Independence
High Performances
For example a pointer which is very difficult for the programmers has been
completely eliminated from java.
Object-Oriented: Java is an object oriented programming language. This
means java programs use objects and classes.
Robust (Strong): Because of strong exception handling java is robust.
Secure: Security problems like Eavesdropping, tampering, impersonation and
virus threats can be completely eliminated by using java.
Parts of Java:
Java SE
Java EE
Java ME
Class/Object:
Object is anything that is really exists in the world is called an object.
What is not an object? Idea, dreams(These are not exits really).
Class is a model for creating objects. Class is a blue print. Class is group name.
Class is a collection of objects.
Class is an Idea.
Data type
Byte
Short
Integer
Long
Memory size
1 byte
2 bytes
4 bytes
8 bytes
Float Data Types: These data types are useful to represents numbers
with decimal point for example 3.14,0.00012,-123.001
Float f=10.2f
double d=12.3;
Data Type
memory size
Float
4 bytes
Double
8 bytes
Character Data Type: This data type represents single characters like a,b,z s,1,9
Ex: char ch=a;
Data type
Char
Memory size
2 byte
Boolean Data type: Boolean data types represent true or false size is 1 bit.
Ex: boolean b=true;
Operators:
An operator is a symbol that performs an operation. An operator acts on some
variables called operands.
Ex: a + b (a, b are operands + is a operator)
Arithmetic Operators: Arithmetic operators are useful to perform basic arithmetic
operations like addition, subtraction, multiplication, divide (+,-,*,/,%).
Ex: int c=a+b; int c=a*b;
Unary Operators: As the name indicates unary operator acts on one operand.
Unary minus (-): This operator is used to negate a given value. Negation means
converting a negative value into positive and vice versa.
Example:
int x=5;
System.out.println(-x)
Increment Operator(++): This operator is used to increment the value by 1
Writing ++ before a variable is called pre incrementationand writing ++ after a
variable is called post incrementation.
In pre incrementation, Incrementation is done first and any other operation is done
next.
In post Incrementation, all other operation are done first and incrementation is
done only at end.
small example:
int x=1;
System.out.println(x);
System.out.println(++x);
System.out.println(x);
System.out.println(x);
System.out.println(x++);
System.out.println(x);
Relational Operator(<,<=,>,>=,==,!=)
These operators are used for the purpose of comparing. For example to know which
one is bigger or whether two quantities are equal or not.
The main use of relational operators is in the construction of conditions in
statements like this
If(condition_is_true) statements to be executed.
Ex:if(a>b) or if(a<b) or if(a==b) etc.
Logical Operators(&&,||,!)
Logical operators are used to construct compound conditions. A compound condition
is a combination of several simple conditions.
Ex: if(a>b && a>c)
If(a==1 || b==1||c==1) or
If(x>y && y<z) or if(!str1.equals(str2))
!= Called Logical NOT Operator. Use to reverses the logical state of its operand. If
a condition is true then Logical NOT operator will make false.
Boolean Operators(&,|,!):
These operators act on boolean variables and produce boolean type result.
Ex: booleana,b;
a=true;
b=false;
a&b=== returnsfalse& gives true only if both are true.
a|b===returns true. | gives true if anyone is true
!a====gives false.
Bitwise operators(~,&,|,^,<<,>>,>>)
Bitwise complement Operator (~)
(-(12+1))
x&y
x|y
Bitwise xorOperator(^)
This operator performs exclusive or(xor) operation on the bits of the numbers. The
symbol is ^ which is called cap
x=10=00001010
y=11=00001011
x ^ y =00001010=11
x
x^y
It shifts the bits towards right and fills the zeros to left. And left most sign bit must
be preserved.
System.out.println("~x="+ (~x));
System.out.println("x&y"+ (x&y));
System.out.println("x/y"+ (x/y));
System.out.println("x^y"+ (x^y));
System.out.println("x<<2"+ (x<<2));
System.out.println("x>>2"+ (x>>2));
System.out.println("x>>>2"+ (x>>>2));
}
}
O/P:
~x=-11
x&y10
x/y0
x^y1
x<<240
x>>22
x>>>22
member operator(.):
instanceof Operator:
This operator is used to test if an object belongs to a class or not.
Syn: boolenvar=object instanceof class
New Operator: By using new operator only we can create object to class.
Ex: Employee e=new Employee();
Cast Operator :cast operator is used to convert one data type to another data
type.
Small example:
double x=10.54;
int y=x;
Priority of operators:
When several operators are used in statement,it is important to know which
operator will execute first and which will come next. To determine that,certain
operator precedence are followed.
First, the contents inside the braces: () and [] wiil be executed.
Next, ++ and --,
Next *,/, and % will execute
+ and will come next
Relaional operators are eecuted next.
Boolen and bitwise operators
Logical operators will come afterwards
Then ternary operator