Lect-4 Introduction To JAVA - 2
Lect-4 Introduction To JAVA - 2
+= v += expr; v = v + expr ;
-= v -=expr; v = v - expr ;
*= v *= expr; v = v * expr ;
/= v /= expr; v = v / expr ;
%= v %= expr; v = v % expr ;
Basic Arithmetic Operators
int a = 35;
a = a >> 2;
What is the value of a now?
int a, b = 3;
a >> b + 3;
What is the value of a now?
Short Circuit logical operators
• Boolean AND and OR operators
• Example:
if (denom != 0 && num / denom > 10 )
Expressions
• An expression is a construct made up of variables,
operators, and method invocations, which are
constructed according to the syntax of the
language, that evaluates to a single value.
• Examples of expressions are in bold below:
int number = 0;
anArray[0] = 100;
System.out.println ("Element 1 at index 0: " +
anArray[0]);
int result = 1 + 2; // result is now 3
if(value1 == value2)
System.out.println("value1 == value2");
Expressions
The data type of the value returned by an expression
depends on the elements used in the expression.
The expression number = 0 returns an int because the
assignment operator returns a value of the same data type
as its left-hand operand; in this case, number is an int.
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
} Before the break
} This is after the break
Jump Statements
class BreakLoop4 {
public static void main(String args[]) {
outer: for(int i=0; i<3; i++) {
System.out.print("Pass " + i + ": ");
for(int j=0; j<100; j++) {
if(j == 10) break outer;
System.out.print(j + " ");
}
System.out.println("This will not print");
}
System.out.println("Loops complete.");
}
}
i = 100;
j = 200;
type method-name-1(parameter-list) {
… }
type method-name-2(parameter-list) {
… }
…
type method-name-m(parameter-list) {
… }
}
Example: Class Usage
class Box {
double width;
double height;
double depth;
}
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println ("Volume is " + vol);
} }
Methods
General form of a method definition:
type name(parameter-list) {
… return value;
…
}
Components:
1) type - type of values returned by the method. If a
method does not return any value, its return type must be
void.
2) name is the name of the method
3) parameter-list is a sequence of type-identifier lists
separated by commas
4) return value indicates what value is returned by the
method.
Example: Method
• Classes declare methods to hide their internal data
structures, as well as for their own internal use:
Within a class, we can refer directly to its member
variables:
class Box {
double width, height, depth;
void volume() {
System.out.print("Volume is ");
System.out.println(width * height *
depth);
}
}
Parameterized Method
• Parameters increase generality and applicability
of a method:
1) method without parameters
int square() { return 10*10; }
2) method with parameters
int square(int i) { return i*i; }
• Parameter: a variable receiving value at the time
the method is invoked.
• Argument: a value passed to the method when it
is invoked.
Constructor
• A constructor initializes the instance variables of
an object.
• It is called immediately after the object is created
but before the new operator completes.
1) it is syntactically similar to a method:
2) it has the same name as the name of its class
3) it is written without return type; the default
return type of a class
• constructor is the same class
• When the class has no constructor, the default
constructor automatically initializes all its instance
variables with zero.
Example: Constructor
class Box {
double width;
double height;
double depth;
Box() {
System.out.println("Constructing Box");
width = 10; height = 10; depth = 10;
}
double volume() {
return width * height * depth;
}
}
Parameterized Constructor
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w; height = h; depth = d;
}
double volume()
{ return width * height * depth;
}
}
Access Control: Data Hiding and
Encapsulation
• Java provides control over the visibility of
variables and methods.
• Encapsulation, safely sealing data within the
capsule of the class Prevents programmers from
relying on details of class implementation, so you
can update without worry
• Helps in protecting against accidental or wrong
usage.
• Keeps code elegant and clean (easier to maintain)
Access Modifiers: Public, Private,
Protected
• Public: keyword applied to a class, makes it
available/visible everywhere. Applied to a
method or variable, completely visible.
// Constructor
public Circle (double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
//Methods to return circumference and area
public double circumference() { return 2*3.14*r;}
public double area() { return 3.14 * r * r; }
}
Keyword this
// Initialize top-of-stack
Stack() {
tos = -1;
}
System.out.println("Stack in mystack2:");
for(int i=0; i<10; i++)
System.out.println(mystack2.pop());
}
}