KEMBAR78
An Overview Of Java | Object Oriented Programming | PDF
An Overview of Java
P.R@CSE-HIT
Object-Oriented Programming
Object-oriented programming is at the core of Java. In
fact, all Java programs are object-oriented—this isn’t
an option the way that it is in C++.
OOP is so integral to Java that you must understand its
basic principles before you can write even simple Java
programs.
Therefore, this chapter begins with a discussion of the
theoretical aspects of OOP.
P.R@CSE-HIT
A First Simple Program
P.R@CSE-HIT
P.R@CSE-HIT
public static void main()
public static void main(String orgs[])
P.R@CSE-HIT
Compiling the Program
 To compile the Example program, execute the compiler, javac,
specifying the name of the source file on the command line, as
shown here: C:>javac Example.java
 The javac compiler creates a file called Example.class that
contains the bytecode version of the program.
 To actually run the program, you must use the Java interpreter,
called java. To do so, pass the class name Example as a
command-line argument, as shown here: C:>java Example
P.R@CSE-HIT
When Java source code is compiled, each individual
class is put into its own output file named after the
class and using the .class extension.
When you execute the Java interpreter as just shown,
you are actually specifying the name of the class that
you want the interpreter to execute.
It will automatically search for a file by that name that
has the .class extension. If it finds the file, it will
execute the code contained in the specified class.
P.R@CSE-HIT
• The next line of code in the program is shown here:
class Example {
• This line uses the keyword class to declare that a new
class is being defined. Example is an identifier that is
the name of the class.
• The entire class definition, including all of its members,
will be between the opening curly brace ({) and the
closing curly brace (}).
A Closer Look at the First Sample Program
P.R@CSE-HIT
 The next line of code is shown here:
public static void main(String args[]) { }
• What is public?
• What is static?
• What is void?
• What is main()?
• What is String?
• What is args[]?
P.R@CSE-HIT
P.R@CSE-HIT
When we are writing only :- public static void main()
When we are writing only :- public static void main(String orgs[])
P.R@CSE-HIT
When we are writing only :- static void main(String args[])
When we are writing only :- public void main(String args[])
P.R@CSE-HIT
When we are writing only :- public static void main()
P.R@CSE-HIT
 The public keyword is an access specifier, which allows the
programmer to control the visibility of class members.
 When a class member is preceded by public, then that member
may be accessed by code outside the class in which it is
declared.
 The opposite of public is private, which prevents a member from
being used by code defined outside of its class.
 In this case, main() must be declared as public, since it must be
called by code outside of its class when the program is started.
P.R@CSE-HIT
The keyword static allows main( ) to be called
without having to instantiate a particular
instance of the class.
This is necessary since main( ) is called by the
Java interpreter before any objects are made.
P.R@CSE-HIT
The keyword void simply tells the compiler that
main( ) does not return a value.
As you will see, methods may also return
values. If all this seems a bit confusing, don’t
worry. All of these concepts will be discussed in
detail in subsequent chapters.
P.R@CSE-HIT
As stated, main( ) is the method called
when a Java application begins.
Keep in mind that Java is case-sensitive.
Thus, Main is different from main.
P.R@CSE-HIT
In main( ), there is only one parameter, albeit a
complicated one.
String args[ ] declares a parameter named args, which
is an array of instances of the class String. (Arrays are
collections of similar objects.)
Objects of type String store character strings.
In this case, args receives any command-line
arguments present when the program is executed.
P.R@CSE-HIT
One other point: main( ) is simply a starting place for
your program.
A complex program will have dozens of classes, only
one of which will need to have a main( ) method to get
things started.
When you begin creating applets — Java programs
that are embedded in Web browsers—you won’t use
main( ) at all, since the Web browser uses a different
means of starting the execution of applets.
P.R@CSE-HIT
The next line of code is shown here. Note that it occurs
inside main( ). System.out.println("This is a simple Java
program.");
This line outputs the string “This is a simple Java
program.” followed by a new line on the screen.
Output is actually accomplished by the built-in println(
) method.
In this case, println( ) displays the string which is
passed to it. As you will see, println( ) can be used to
display other types of information, too.
P.R@CSE-HIT
The line begins with System.out. While too complicated
to explain in detail at this time.
Briefly, System is a predefined class (present in java.lang
package) that provides access to the system, and .out is
the output stream that is connected to the console.
The println method is a member of the out object,
which is a static data member of System class.
P.R@CSE-HIT
System is a built-in class present in java.lang package.
out is an object of PrintStream class defined in System
class. out is declared as public, static and final.
println() is a method of PrintStream class.
The println() method is called with out object.
The out object is called with System class.
So, we are calling println() with out object and out
object with System class.
P.R@CSE-HIT
 What is the difference between #include and import statement?
 #include directive makes the compiler go to the c/c++ standard
library and copy the code from the header files into the program.
As a result the program size increases, thus wasting memory and
processor’s time.
 Import statement makes the JVM go to Java standard library,
execute the code there, and substitute the result into the
program. Here no code is copied and hence no waste of memory
and processor time. It’s an efficient mechanism.
P.R@CSE-HIT
What is a Unicode System?
Unicode System is an encoding standard that provides
an unique number for every character, no matter what
the platform, program or language is.
Unicode uses 2 bytes to represent single character.
Any character from any language can be encoded
successfully. (Helpful in browser)
P.R@CSE-HIT
P.R@CSE-HIT
P.R@CSE-HIT
P.R@CSE-HIT
P.R@CSE-HIT
P.R@CSE-HIT
P.R@CSE-HIT
P.R@CSE-HIT
P.R@CSE-HIT

An Overview Of Java | Object Oriented Programming

  • 1.
    An Overview ofJava P.R@CSE-HIT
  • 2.
    Object-Oriented Programming Object-oriented programmingis at the core of Java. In fact, all Java programs are object-oriented—this isn’t an option the way that it is in C++. OOP is so integral to Java that you must understand its basic principles before you can write even simple Java programs. Therefore, this chapter begins with a discussion of the theoretical aspects of OOP. P.R@CSE-HIT
  • 3.
    A First SimpleProgram P.R@CSE-HIT
  • 4.
  • 5.
    public static voidmain() public static void main(String orgs[]) P.R@CSE-HIT
  • 6.
    Compiling the Program To compile the Example program, execute the compiler, javac, specifying the name of the source file on the command line, as shown here: C:>javac Example.java  The javac compiler creates a file called Example.class that contains the bytecode version of the program.  To actually run the program, you must use the Java interpreter, called java. To do so, pass the class name Example as a command-line argument, as shown here: C:>java Example P.R@CSE-HIT
  • 7.
    When Java sourcecode is compiled, each individual class is put into its own output file named after the class and using the .class extension. When you execute the Java interpreter as just shown, you are actually specifying the name of the class that you want the interpreter to execute. It will automatically search for a file by that name that has the .class extension. If it finds the file, it will execute the code contained in the specified class. P.R@CSE-HIT
  • 8.
    • The nextline of code in the program is shown here: class Example { • This line uses the keyword class to declare that a new class is being defined. Example is an identifier that is the name of the class. • The entire class definition, including all of its members, will be between the opening curly brace ({) and the closing curly brace (}). A Closer Look at the First Sample Program P.R@CSE-HIT
  • 9.
     The nextline of code is shown here: public static void main(String args[]) { } • What is public? • What is static? • What is void? • What is main()? • What is String? • What is args[]? P.R@CSE-HIT
  • 10.
  • 11.
    When we arewriting only :- public static void main() When we are writing only :- public static void main(String orgs[]) P.R@CSE-HIT
  • 12.
    When we arewriting only :- static void main(String args[]) When we are writing only :- public void main(String args[]) P.R@CSE-HIT
  • 13.
    When we arewriting only :- public static void main() P.R@CSE-HIT
  • 14.
     The publickeyword is an access specifier, which allows the programmer to control the visibility of class members.  When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared.  The opposite of public is private, which prevents a member from being used by code defined outside of its class.  In this case, main() must be declared as public, since it must be called by code outside of its class when the program is started. P.R@CSE-HIT
  • 15.
    The keyword staticallows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made. P.R@CSE-HIT
  • 16.
    The keyword voidsimply tells the compiler that main( ) does not return a value. As you will see, methods may also return values. If all this seems a bit confusing, don’t worry. All of these concepts will be discussed in detail in subsequent chapters. P.R@CSE-HIT
  • 17.
    As stated, main() is the method called when a Java application begins. Keep in mind that Java is case-sensitive. Thus, Main is different from main. P.R@CSE-HIT
  • 18.
    In main( ),there is only one parameter, albeit a complicated one. String args[ ] declares a parameter named args, which is an array of instances of the class String. (Arrays are collections of similar objects.) Objects of type String store character strings. In this case, args receives any command-line arguments present when the program is executed. P.R@CSE-HIT
  • 19.
    One other point:main( ) is simply a starting place for your program. A complex program will have dozens of classes, only one of which will need to have a main( ) method to get things started. When you begin creating applets — Java programs that are embedded in Web browsers—you won’t use main( ) at all, since the Web browser uses a different means of starting the execution of applets. P.R@CSE-HIT
  • 20.
    The next lineof code is shown here. Note that it occurs inside main( ). System.out.println("This is a simple Java program."); This line outputs the string “This is a simple Java program.” followed by a new line on the screen. Output is actually accomplished by the built-in println( ) method. In this case, println( ) displays the string which is passed to it. As you will see, println( ) can be used to display other types of information, too. P.R@CSE-HIT
  • 21.
    The line beginswith System.out. While too complicated to explain in detail at this time. Briefly, System is a predefined class (present in java.lang package) that provides access to the system, and .out is the output stream that is connected to the console. The println method is a member of the out object, which is a static data member of System class. P.R@CSE-HIT
  • 22.
    System is abuilt-in class present in java.lang package. out is an object of PrintStream class defined in System class. out is declared as public, static and final. println() is a method of PrintStream class. The println() method is called with out object. The out object is called with System class. So, we are calling println() with out object and out object with System class. P.R@CSE-HIT
  • 23.
     What isthe difference between #include and import statement?  #include directive makes the compiler go to the c/c++ standard library and copy the code from the header files into the program. As a result the program size increases, thus wasting memory and processor’s time.  Import statement makes the JVM go to Java standard library, execute the code there, and substitute the result into the program. Here no code is copied and hence no waste of memory and processor time. It’s an efficient mechanism. P.R@CSE-HIT
  • 24.
    What is aUnicode System? Unicode System is an encoding standard that provides an unique number for every character, no matter what the platform, program or language is. Unicode uses 2 bytes to represent single character. Any character from any language can be encoded successfully. (Helpful in browser) P.R@CSE-HIT
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.