18CS45 - 2020 - 21 - Module2 - Ooc
18CS45 - 2020 - 21 - Module2 - Ooc
18CS45 - 2020 - 21 - Module2 - Ooc
Introduction to Java
• double
Numbers with decimal point
like 12.453, 3.432, 0.0000002
Math
• Unary
int x = -9;
• Regular math (+,-,*,/)
int y = 3+x;
• % modulo operator
Incrementing
• Increment and Decrement
• i++ equivalent to i = i + 1;
• Can also do ++i, which uses i before
incrementing it.
• Decrementing: i--;
Assignment
int a; //declaration – needed once
a = 10 ; // assignment … declared above
Storage area
a 10
Assignment
• =
• Example:
int n;
n = 10;
or
int n = 10; //same
Assignment
• +=
• -=
• *=
• /=
• %=
Assignment…
int a , b ; // a =? b = ?
a = 4; // a = 4 b = ?
b = 7; // a = 4 b = 7
a = b; // a = 7 b = 7
b = a; // a = 7 b = 7
a = 5; // a = 5 b = 7
Arithmetic Operators
• What is the value of –12 + 3
• An arithmetic operator is a symbol that asks for
doing some arithmetic.
int i=5;
System.out.print(“i = ” + i--);//print 5, then i becomes 4
System.out.print(“i = ” + --i);//subtract 1 from i, then print
3
Refer to decre.java
Boolean Data Type
This data type can store only two values; true and false.
Declaring a boolean variable is the same as declaring any other
primitive data type like int, float, char.
boolean response = false; //Valid
boolean answer = true; //Valid
boolean answer = 9943; //Invalid,
boolean response = “false”; // Invalid,
Refer to bool_op.java
Boolean Expressions
• boolean b
b will be either true (1) or false (0)
• Logical operations: !(not), && (and) || (or)
• boolean a,b;
a = true;
b = false;
System.out.println (“a && b is “ + (a && b));
Strings
• Not a primitive class, its actually something called a wrapper
class
• To find a built in class’s method use API documentation.
• String is a group of char’s
• A character has single quotes
– char c = ‘h’;
• A String has double quotes
– String s = “Hello World”;
• Method length
– int n = s.length;
Using Strings
public class hello{
public static void main (String [] args) {
String s = “Hello World\n”;
System.out.println(s); //output simple
string
} //end main
}//end class hello
Character data
• Characters
‘a’, ‘A’, ‘c’ , ‘?’ , ‘3’ , ‘ ’
(last is the single space)
Refer: equa.java
Conditional Operators
•Conditional OR ‘||’
return value is true if any one of x or y, is true else it is false.
System.out.println(“x||y ” + x||y); // Refer cond_or.java
The if - branching statement
• if ( x < y) { • if ( x < y ) {
x = y; x = y;
} }
else {
x = 88;
}
The ‘ if ’ construct
• The case values can be compared only for equality with the
switch expression
• The expression must be of type int, char, short, byte
• Each case value must be a constant, not a variable
• Duplicate case values are not allowed
• The default part is optional
Iteration Statements
• while
• The while loop is Java’s most fundamental loop statement. It repeats a
statement or block
• while its controlling expression is true. Here is its general form:
• while(condition) {
• // body of loop
• }
• The condition can be any Boolean expression. The body of the loop will be
executed as long
• as the conditional expression is true. When condition becomes false, control
passes to the
• next line of code immediately following the loop. The curly braces are
unnecessary if only
• a single statement is being repeated.
• do-while
do {
// body of loop
} while (condition);
• for
• a powerful and versatile construct.
for(initialization; condition; iteration) {
// body
}
• Using the Comma
Type Conversion and Casting
• byte a = 40;
• byte b = 50;
• byte c = 100;
• int d = a * b / c;
Definition:
An array is a group/collection of variables of the same type
that are referred to by a common name and an index
Examples:
• Collection of numbers
• Collection of names
• Collection of suffixes
Examples
Array of numbers:
10 23 863 8 229
Array of names:
Array of suffixes:
Allocation of memory:
variable-name = new data-type[size];
eg. marks = new int[5];
This will allocate memory of 5 integers to the array ‘marks’
and it can store upto 5 integers in it. ‘new’ is a special
operator that allocates memory.
Syntax…