0% found this document useful (0 votes)
14 views43 pages

Chapter 04

Uploaded by

sally D.allah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views43 pages

Chapter 04

Uploaded by

sally D.allah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Chapter 4

Java Language Fundamentals


 Develop code that declares classes, interfaces,
and enums, and includes the appropriate use
of package and import statements
 Explain the effect of modifiers

 Given an example of a class and a command-


line, determine the expected runtime behavior
 Determine the effect upon object references
and primitive values when they are passed
 into methods
 Recognizethe point at which an object
becomes eligible for garbage collection, and
determine what is and is not guaranteed by
the garbage collection system.
 Given the fully-qualified name of a class that is
deployed inside and/or outside a JAR file,
construct the appropriate directory structure
for that class
 Organizing Your Java Application
 Passing Arguments into Methods
 Using Access Modifiers
 Understanding Usage Modifiers
 Modifiers: The Big Picture
 Understanding Garbage Collection in Java
 A Java application is composed of a set of files generally
distributed in a directory structure
• Source code: .java
• Class file: .class
 The compiler searches for a class file when it
encounters a reference to a class in a .java file
 The interpreter, during runtime, searches the .class
files
 Both the compiler and the interpreter search for the
.class files in the list of directories listed in the
classpath variable
 When executing a Java application, the JVM
loads the class, and invokes the main() method
of this class
 The
method main() must be declared public,
static, and void
Asource file may have one or more classes.
Only one class (matching the file name) at
most may be declared public
A .java file name should match the name of a
class in the file. If one of those classes is declared
public, then the name of the .java file must
match the name of the public class
 There can be only one public class at maximum
in a source file
 The compiler generates a file with extension
.class corresponding to each class in the source
file that is compiled
 The name of the .class file matches the name of
the corresponding class
 Youcan bundle related classes and interfaces into
a group called a package in a directory whose
name matches the name of the package
 The
qualified name for the class is
course.Student, and the path name to it is
course/Student.java
 to import the package:
import course.Student;
 You can import all the classes in the package:
import course.*;
 It makes it easier to find and use classes.
 Itavoids naming conflicts. Two classes with the
same name existing in two different packages
do not have a name conflict, as long as they
are referenced by their fully qualified name.
 It provides access control.
 When the compiler or the interpreter
encounters a class name in your code, they
look for classes in each directory or a JAR (Java
Archive) file listed in the classpath
 Let’s assume that we put the class files in the
c:\app\class\com\netalong\course
directory, the classpath must include this
path name:
C:\app\class
 All the directories of an application can be
compressed into what is called a JAR file.
 A JAR file as a tree of directories.

 can be created with the jar command:


jar -cf myApp.jar topDir
 list directories and files in this JAR file:
jar -tf myApp.jar
 execute your application contained in the JAR file:
java -jar myApp.jar
 Organizing Your Java Application
 Passing Arguments into Methods
 Using Access Modifiers
 Understanding Usage Modifiers
 Modifiers: The Big Picture
 Understanding Garbage Collection in Java
 Thevalues of parameters are called arguments
and can be passed during a method call
 Whether the passed variable is a primitive or a
reference variable, it is always the copy of the
variable – pass by value
A primitive variable holds a data item as its
value
 When a primitive variable is passed as an
argument in a method call, only the copy of
the original variable is passed
 Any change to the passed variable in the called
method will not affect the variable in the
calling method
 An object reference variable points to an
object
 When you pass a reference variable in a
method, you pass a copy of it
 The copy of the reference variable points to
the same object to which the original variable
points  the called method can change the
object properties by using the passed
reference
 Organizing Your Java Application
 Passing Arguments into Methods
 Using Access Modifiers
 Understanding Usage Modifiers
 Modifiers: The Big Picture
 Understanding Garbage Collection in Java
 Accessmodifiers determine the accessibility
scope of the Java elements they modify
 Theaccess modifiers may be used with a class
and its members
 The
Java language offers three explicit access
modifiers, public, protected, and
private, and a default modifier
 The public modifier makes a Java element (a
class or a class member) most accessible
 The private modifier makes a Java element
(a inner class or a class member) least
accessible
A private member of a class may only be
accessed from the code inside the same class
in which this member is declared
A top-level class cannot be declared private;
it can only be public, or default
 This modifier applies only to class members
 A class member declared protected is
accessible to the following elements:
• All the classes in the same package that
contains the class that owns the protected
member.
• All the subclasses of the class that owns the
protected member.
 When you do not specify any access modifier
for an element, it is assumed that the access is
default.
 A class or a class member declared default is
accessible from anywhere in the same package
in which the accessed class exists
 A method may not be overridden to be less
accessible
 Organizing Your Java Application
 Passing Arguments into Methods
 Using Access Modifiers
 Understanding Usage Modifiers
 Modifiers: The Big Picture
 Understanding Garbage Collection in Java
 modifythe way a class or a class member is to
be used:
• final
• static
• abstract
• native
• transient
• volatile
• synchronized
 may be applied to a class, a method, or a
variable:
• the value of the variable is constant
• the class cannot be extended
• the method cannot be overridden
 can be applied to variables, methods, and a
block of code
 The static elements of a class are visible to all
the instances of the class
 Ifone instance of the class makes a change to
a static element, all the instances will see that
change
 may be applied to a class or a method
 A class that is declared abstract cannot be
instantiated
 A class must be declared abstract:
• may have one or more abstract methods
• may have inherited one or more abstract methods
from its superclass, and has not provided
implementation for all or some of them
• implements an interface, but does not provide
implementation for at least one method in the
interface.
 native: to use a method that exists outside of
the JVM
 transient: to instruct the JVM not to store the
variable when the object in which it is declared is
being serialized
 volatile: tells the accessing thread that it
should synchronize its private copy of the variable
with the master copy in the memory
 synchronized: to control access to critical
sections in the program
 Organizing Your Java Application
 Passing Arguments into Methods
 Using Access Modifiers
 Understanding Usage Modifiers
 Modifiers: The Big Picture
 Understanding Garbage Collection in Java
 Organizing Your Java Application
 Passing Arguments into Methods
 Using Access Modifiers
 Understanding Usage Modifiers
 Modifiers: The Big Picture
 Understanding Garbage Collection in Java
 When you create an object, the object is put
on the heap
 After an object in memory has been used and
is no longer needed, it is sensible to free
memory from that object: garbage collection
 Garbage collection is done automatically by
what is called the garbage collector
 automates memory management by freeing
up the memory from objects that are no
longer in use
 Make a request to the garbage collector:
System.gc();
Runtime.getRuntime().gc();
 An object is considered eligible for garbage
collection when there is no reference pointing
to it
 Thismethod will be called by the garbage
collector before deleting any object
 inherited from the Object class

You might also like