Java Unit 2
Java Unit 2
Java Unit 2
System.out.println(Simpson==“Lisa”
? ”She’s our favorite” :“Doh!”);
128 64 32 16 8 4 2 1
0 1 0 0 1 1 1 0
64 8 4 2
Accentuate the positive
Computers don’t know about negative numbers
Use the first (leftmost) bit as a sign bit:
1 if negative: -5 is 11111101
0 if positive: +5 is 00000011
Bitwise is Binary
for(int i=0;i<50;i++){
if(i%13==0){
break;
}
System.out.println(“Value of i:”+i);
}
continue
Continue makes the loop to skip the current
execution and continues with the next iteration.
for(int i=0;i<50;i++){
if(i%13==0){
continue;
}
System.out.println(“Value of i:”+i);
}
return
return statement can be used to cause execution
to branch back to the caller of the method.
Labeled break,continue
Labeled break and continue statements will
break or continue from the loop that is
mentioned.
Used in nested loops.
Objects and Classes
OO Programming Concepts
Creating Objects and Object Reference Variables
Differences between primitive data type and object type
Automatic garbage collection
Constructors
Modifiers (public, private and static)
Instance and Class Variables and Methods
Scope of Variables
Use the this Keyword
Case Studies (Mortgage class and Count class)
OO Programming Concepts
Data Field
data field 1
radius = 5
... State
Method
data field n findArea
method 1
... Behavior
method n
Class and Objects
double findArea(){
return radius * radius * 3.14159;
}
}
Declaring Object Reference Variables
ClassName objectReference;
Example:
Circle myCircle;
Creating Objects
objectReference = new ClassName();
Example:
myCircle = new Circle();
Example:
Circle myCircle = new Circle();
Differences between variables of
primitive Data types and object types
c: Circle
Created using
new Circle() radius = 1
Copying Variables of Primitive Data
Types and Object Types
i 1 i 2 c1 c1
j 2 j 2 c2 c2
radius = 5 radius = 9
Garbage Collection
As shown in the previous figure, after
the assignment statement c1 = c2, c1
points to the same object referenced by
c2. The object previously referenced by
c1 is no longer useful. This object is
known as garbage. Garbage is
automatically collected by JVM.
Garbage Collection, cont
TIP: If you know that an object is no
longer needed, you can explicitly assign
null to a reference variable for the
object. The Java VM will automatically
collect the space if the object is not
referenced by any variable.
Accessing Objects
Referencing the object’s data:
objectReference.data
myCircle.radius
TestCircle Run
Constructors
Circle(double r) {
radius = r;
} Constructors are a
special kind of
Circle() { methods that are
radius = 1.0; invoked to construct
} objects.
myCircle = new Circle(5.0);
Constructors, cont.
A constructor with no parameters is referred to as
a default constructor.
Constructors must have the same name as the
class itself.
Constructors do not have a return type—not
even void.
Constructors are invoked using the new
operator when an object is created. Constructors
play the role of initializing objects.
Example : Using Classes from the
Java Library
Objective: Demonstrate using classes from the
Java library. Use the JFrame class in the
javax.swing package to create two frames; use
the methods in the JFrame class to set the title,
size and location of the frames and to display
the frames.
TestFrame Run
Example : Using Constructors
TestCircleWithConstructors Run
Visibility Modifiers and
Accessor Methods
By default, the class, variable, or data can be
accessed by any class in the same package.
public
The class, data, or method is visible to any class in any
package.
private
The data or methods can be accessed only by the declaring
class.
The get and set methods are used to read and modify private
properties.
Example :
Using the private Modifier and
Accessor Methods
TestCircleWithAccessors Run
Passing Objects to Methods
Passing by value (the value is the reference to the
object)
Example Passing Objects as Arguments
TestPassingObject Run
Passing Objects to Methods, cont.
main printAreas
method method
times
myCircle: Circle
radius = 1
Instance
Variables, and Methods
circle1:Circle 1 radius
instantiate -radius = 1
radius is an instance CircleWithStaticVariable -numOfObjects = 2
variable, and
numOfObjects is a -radius
class variable -numOfObjects
2 numOfObjects
+getRadius(): double instantiate
+setRadius(radius: double): void circle2:Circle
+getNumOfObjects(): int
+findArea(): double
-radius = 5 5 radius
-numOfObjects = 2
Example
Using Instance and Class Variables and
Method
TestCircleWithStaticVariable Run
Scope of Variables
The scope of instance and class variables is the
entire class. They can be declared anywhere inside
a class.
The scope of a local variable starts from its
declaration and continues to the end of the block
that contains the variable. A local variable must be
declared before it can be used.
The Keyword this
Use this to refer to the current object.
Use this to invoke other constructors of the
object.
Array of Objects
Circle[] circleArray = new Circle[10];
… Circle object 1
TotalArea Run
Class Abstraction
Class abstraction means to separate class
implementation from the use of the class. The creator
of the class provides a description of the class and let
the user know how the class can be used. The user of
the class does not need to know how the class is
implemented. The detail of implementation is
encapsulated and hidden from the user.
Example The Mortgage Class
Mortgage
-annualInterestRate: double
-numOfYears: int
-loanAmount: double
Mortgage
+Mortgage()
+Mortgage(annualInterestRate: double,
numOfYears: int, loanAmount: double)
+getAnnualInterestRate(): double TestMortgageClass
+getNumOfYears(): int
+getLoanAmount(): double
+setAnnualInterestRate(annualInteresteRate: double): void
+setNumOfYears(numOfYears: int): void
+setLoanAmount(loanAmount: double): void
Run
+monthlyPayment(): double
+totalPayment(): double
Example The Count Class
TestVoteCandidate Run
Java API and Core Java classes
java.lang
Contains core Java classes, such as numeric classes,
strings, and objects. This package is implicitly
imported to every Java program.
java.awt
Contains classes for graphics.
java.applet
Contains classes for supporting applets.
Java API and Core Java classes,
cont.
java.io
Contains classes for input and output
streams and files.
java.util
Contains many utilities, such as date.
java.net
Contains classes for supporting
network communications.
Java API and Core Java classes,
cont.
java.awt.image
Contains classes for managing bitmap images.
java.awt.peer
Platform-specific GUI implementation.
Others:
java.sql
java.rmi