Basics
Basics
Unit Objectives
⚫ Write Programs to create classes and objects for
the given problem
⚫ Explain the characteristics of given java token
⚫ Explain the function of given operator with
example.
⚫ Construct the expression by using implicit and
explicit type conversion
⚫ Develop the programs using control structure to
solve the given problem
Somethings about java
⚫ James Gosling and Sun Microsystems
⚫ Oak
⚫ Java, May 20, 1995, Sun World
⚫ HotJava
⚫ The first Java-enabled Web browser
⚫ JDK Evolutions
⚫ J2SE, J2ME, and J2EE (not mentioned in the book, but
could discuss here optionally)
Why Java???
1) Simple
2) Object Oriented
3) Robust
4) Platform Independent
5) Secure
6) Multi Threading
7) Architectural Neutral
8) Portable
9) High Performance
Comparison between Java & C++
Java C++
Used to built web based applications Used to built only Desktop applications
⚫ Unlike literals they are not the things themselves, just ways of
referring to them.
1) Int x;
2) float y
3) int float;
4) int 2good;
5) int yes&no;
Constants
⚫ A constant is a variable whose value cannot change once
it has been assigned. Java doesn't have built-in support for
constants.
⚫ To define a variable as a constant, we just need to add the
keyword “final” in front of the variable declaration.
variable = value;
e.g
int a = 5;
a += 5;
Contd..
1. +=:for adding left operand with right operand and then
assigning it to variable on the left.
2. -=:for subtracting left operand with right operand and then
assigning it to variable on the left.
3. *=:for multiplying left operand with right operand and then
assigning it to variable on the left.
4. /=:for dividing left operand with right operand and then
assigning it to variable on the left.
5. %=:for assigning modulo of left operand with right operand
and then assigning it to variable on the left.
Relational Operators
⚫ These are a bunch of binary operators that are used to check for
relations between two operands including equality, greater than, less
than etc.
⚫ They return a boolean result after the comparison and are extensively
used in looping statements as well as conditional if-else statements and
so on.
⚫ The general format of representing relational operator is:
Syntax:
variable1 relation_operator variable2
Example:
A<b
Relational Operators
Operator Meaning
== Is equal to
!= Is not equal to
(a>b)?a:b
Bitwise Operators
⚫ These operators are used to perform manipulation of individual bits of
a number. They can be used with any of the integer types. They are
used when performing update and query operations of Binary indexed
tree.
⚫ ‘&’Bitwise AND operator: returns bit by bit AND of input values.
⚫ ‘|’ Bitwise OR operator: returns bit by bit OR of input values.
⚫ ‘^’ Bitwise XOR operator: returns bit by bit XOR of input values.
⚫ ‘~’ Bitwise Complement Operator: This is a unary operator which
returns the one‟s compliment representation of the input value, i.e. with
all bits inversed.
Shift Operators
⚫ These operators are used to shift the bits of a
number left or right thereby multiplying or
dividing the number by two respectively. They can
be used when we have to multiply or divide a
number by two. General format-
number shift_op number_of_places_to_shift;
Example
a=2(0020)
a<<2 (2000 i.e 8)
Contd…
⚫ <<, Left shift operator: shifts the bits of the number to the
left and fills 0 on voids left as a result..
⚫ >>, Signed Right shift operator: shifts the bits of the
number to the right and fills 0 on voids left as a result. The
leftmost bit depends on the sign of initial number.
⚫ >>>, Unsigned Right shift operator: shifts the bits of the
number to the right and fills 0 on voids left as a result. The
leftmost bit is set to 0.
instance of operator
⚫ Instance of operator is used for type checking. It can
be used to test if an object is an instance of a class, a
subclass or an interface. General format-
⚫ Syntax
object instance of class/subclass/interface
⚫ Example:
Person obj1 = new Person();
obj1 instance of Person
Decision Making and Looping
⚫ if
⚫ if-else
⚫ nested-if
⚫ if-else-if
⚫ switch-case
⚫ jump – break, continue, return
if
if (t)
return;
// Compiler will bypass every statement after return
System.out.println("This won't execute.");
Looping in Java
⚫ while : while loop is a control flow statement that allows
code to be executed repeatedly based on a given Boolean
condition. The while loop can be thought of as a repeating
if statement.
⚫ Syntax:
while (boolean condition)
{
loop statements...
}
⚫ For : for loop provides a concise way of writing the loop
structure. Unlike a while loop, a for statement consumes the
initialization, condition and increment/decrement in one line
thereby providing a shorter, easy to debug structure of looping.
⚫ Syntax:
for (initialization; testing condition;
increment/decrement)
{
statement(s)
}
⚫ For Each : Java also includes another version of for loop
introduced in Java 5. Enhanced for loop provides a simpler
way to iterate through the elements of a collection or array. It is
inflexible and should be used only when there is a need to
iterate through the elements in sequential manner without
knowing the index of currently processed element.
⚫ Syntax:
for (T element:Collection obj/array)
{
statement(s)
}
⚫ Example
String array[] = {"Ron", "Harry", "Hermoine"};