CORE JAVA: Before starting to learn java concepts, let us plunge ...

oatmealbrothersΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 4 μήνες)

50 εμφανίσεις



CORE JAVA:


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 ot
her 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 t
hey 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

Simple:
how java made simple?

Difficult concepts in C/C++ have been omitted in java

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


Java SE:

(Java Standard Edition)


By using Java SE we can develop standalone applications.

Java EE:
(Java
Enterprise Edition)


By using Java EE we can develop web applications.

Java ME: (
Java Mobile Edition
)


By using Java ME we can develop mobile applications.


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.




First Step towards Java Programming

To explain smal
l java programming and comments

Single line comments: These comments are for making a single line as comments.
These comments start with double slash symbol // and after this, whatever is
written till end of the line is taken as a comment.

For example, //

this is my comment of one line.

Multi line comments: These comments are used for representing several lines as
comments. These comments start with /* and end with */. In between /* and */,
whatever is written is treated as acomments. For example,

/*
------
---------
*/


Class Demo{


Public static void main (String args[]){

System.out.println(“welcome to”);


}}

java program structure:

Java library
------
packages
-------
classes and interfaces
----
methods


Naming Conventions in Java

When we are writing java programs we should follows some rules

Class name start with capital letters then from second word onwards each new
word start with Capital letter.

Ex: Class MindQInstitute(don’t give spaces in between words)

Method names st
art with small letters after that each word starts with capital
letters.

Ex: calculateInterest(inta,Int b)

Variable declaration is same as method declaration.

Ex: intpercMarks;


Constants represent fixed values that cannot be altered.

Ex:
final
int

NUMBER_OF_HOURS_IN_A_DAY = 24;



Data Types in Java


We know we need variables to store the data. Internally, a variable represents a
memory location which holds data. When we want to use a variable in a program,
we should first declare it as


int x;


Her
e we declaring that x is a variable, which can store int(integer) type data.

this means int is representing the nature of data to be stored

into x. int is also
called a data type. For example x can store an integer number like 125 as

x=125;


Here x is a variable and =represents that the value 125 is stored into x. This value
125 stored into x is also called variable.




Integer Data Types



These data types represents Integer numbers that is numbers without
fractional parts or decimal points

for example,125,
-
22234,0,1022, etc

Ex: int a=12,int b=
-
12112,int c=0;

yte b=


Data type

Memory size

B
yte

1 byte

S
hort

2 bytes

Integer

4 bytes

L
ong

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

Memory
size

Char

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 u
nary operator acts on one operand.



Unary minus (
-
)



Increment Operator (++)



Decrement Operator (
--
)

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
incrementation
and 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.o
ut.println(x);

System.out.println(x++);

System.out.println(x);


Decrement operator(
--
): This operator is used to decrement the value of a variable
by 1.

Assignment operator(=):

This operator is used to store some value into a variable.

ex:x=5;

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 (~)

This operator gives complement form of a given number. Which is pronounced as
tilde.

If int x=10. Find the ~x it gives
-
11(it adds 0ne and converts _ve no)

~12====
-
13 (
-
(12+1))

Bitwise and Operator (&)

T
his operator performs and operation on the individual bits of the numbers. The
symbol for this operator is &, Which is called ampersand

x=10=00001010

y=11=00001011

x& y =00001010=10

x y

x&y

0

0

0

0

1

0

1

0

0


1

1

1


Bitwise oroperator (|)

This
operator performs or operation on the bits of the numbers. The symbol is pipe
symbol.

x=10=00001010

y=11=00001011

x | y =00001010=11

x y

x|y

0

0

0

0

1

1

1

0

1

1

1

1



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 y

x^y

0

0

0

0

1

1

1

0

1

1

1

0

Bitwise Left Shift Operator(<<(double less than))

This operator shits the bits of the number towards left

a specified number of
positions. If we write x<<n, the meaning is to shift the bits of x towards left n
positions.

Ex:

X=10=00001010 x<<2 will be 00101000=40

Shifting the value of x towards left 2 positions will make the leftmost 2 bits to be
lost. And fi
lls the zeros to right.

Bitwise Right Shift Operator (>>):

This operator shifts the bits of the number towards right a specified number of
positions .>> shits the bits towards right and also preserve the sign bit, which is
left most bit. sign bit represent
s the sign of the number. Sign bit 0 represents a
positive number and 1 represents a negative number. So after performing >>
operation on appositive no, we get a positive value in the result also. If right
shifting done on a negative, again we get a negati
ve value only.

If x=10=00001010, then calculate x>>2 value will be 0000 0010=2

It shifts the bits towards right and fills the zeros to left. And left most sign bit must
be preserved.


Bitwise Zero Fill Right shit operator(>>>)

This operator also shifts t
he bits of the number towards right a specified number of
positions. But it stores 0 in the sign bit. Since, It always fills 0 in the sign bit, it is
called zero fill right shift operator. If we >>> on a positive number, it gives same
as that of >>. But in

case of negative numbers, the output will be positive, sine
sign bit is replaced by a 0.

The main difference between >> and >>> .In >> it protect the sign bit.

Whereas>>> it always fills the zero’s.


Below example covers all the bitwise Operators:

public
class Bits {

public static void main(String[] args) {

bytex,y;

x=10;

y=11;


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.ou
t.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


Ternary Operator or Conditional Opertor(?:):

This operator is called ternary operator because it acts on 3 variables .the other
name for

this operator is conditional operator, since it represents a conditional
statement.

Syntax:

Variable = expression1? expression2:expression3

This means that first of all, expression1 is evaluated. if it is true, then expression2
value is stored into the va
riable. If a expression1 is false, then expression3 value is
stored into the variable. It means:

If(exp1 is true)

variable=exp2

Else variable=exp3

Ex:

int
max
;

int

a=10;

int

b=12;


max=(a<b)? a : b;

member operator(.):


i
nstanceof Operator
:

This operator is

used to test if an object belongs to a class or not.

Syn: boolenvar=object instanceof class

Ex: Employee e=new Employee();

boolean b=einstanceof Employee;


New Operator:

By using new operator only we can create object to class.

Ex: Employee e=new Employe
e();


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 firs
t 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