KEMBAR78
Introduction to Java Programming | PPT
Introduction to
Java Programming
Prepared By: Dr. Rosemarie S. Guirre
2
“When a programming
language is created that
allows programmers to
program in simple
English, it will be
discovered that
programmers cannot
speak English.”
- Anonymous
3
GET STARTED WITH
JAVA PROGRAMMING
JRE (Java Runtime) is needed
for running Java programs.
JDK (Java Development Kit),
which includes JRE plus the
development tools (such as
compiler and debugger), is
need for writing as well as
running Java programs.
1. DOWNLOAD THE COMPILER
http://www.oracle.
com/technetwork/ja
va/javase/download
s/index.html
4
Under "Java Platform,
Standard Edition" ⇒
"Java SE 8ux" ⇒ Click
the "JDK Download"
button.
5
1. DOWNLOAD THE COMPILER
Check "Accept
License Agreement".
6
1. DOWNLOAD THE COMPILER
Choose your operating platform,
e.g., Windows x86 for 32-bit
Windows OS or Windows x64 for
64-bit Windows OS. You can check
whether your Windows OS is 32-bit
or 64-bit via "Control Panel" ⇒
System ⇒ Under the "System
Type".
7
1. DOWNLOAD THE COMPILER
2. INSTALL JDK AND JRE
Run and install the
downloaded installer.
8
9
3: Include JDK's "bin" Directoryin the PATH
1.Control Panel ⇒ System ⇒
Advanced system settings
2. Switch to "Advanced" tab
⇒ Environment Variables
3. In "System Variables",
scroll down to select
"PATH" ⇒ Edit
10
3: Include JDK's "bin" Directory in the PATH
In "Variable value"
field, INSERT "c:Program
FilesJavajdk1.8.0_xxbin" (Replace xx with
the upgrade number and VERIFY that this is
your JDK's binary directory!!!) IN FRONT of
all the existing directories, followed by a
semi-colon (;) which separates the JDK's
binary directory from the rest of the
existing directories.
DO NOT DELETE any existing entries;
otherwise, some existing applications may
not run.
11
3: Include JDK's "bin" Directory in the PATH
It should be like this:
12
3: Include JDK's "bin" Directory in the PATH
13
4: verify the JDK installation
It should be like this:
14
5: verifythe JDK version
It should be like this:
15
Getting Started with Java
Programming
A Simple Java
Application
Compiling Programs
Executing Applications
16
Creating
simple program
Note: open notepad or
notepad c++
17
Structure of Java programs
public class <name>
{ public static void main(String[] args)
{ <statement(s)>;
}
}
Every executable Java program consists of a
class...
– that contains a method named main...
that contains the statements to be executed
The program with a class named RoseJava,
whose main method executes one statement
named System.out.println
18
A Simple Java Program
public class RoseJava
{ public static void main(String[] args)
{ System.out.println(“I LOVE JAVA");
}
}
This would be in a text file named RoseJava.java
19
The command line
To run a Java program using your
Command Prompt:
 change to the directory of
your program
cd
 compile the program
javac RoseJava.java
 execute the program
java RoseJava
source code
(RoseJava.java)
compile
byte code
(RoseJava.class)
execute
output
THIS WILL BE THE OUTPUT
20
21
More Definitionscode or source code: The sequence
of instructions in a particular
program.
–The code in this program instructs the
computer to print a message of I
LOVE JAVA on the screen.
output: The messages printed to
the computer user by a program.
console: The text box or window
onto which output is printed.
22
Compiling and Running
Compiler: a program that
converts a program in one
language to another
language
–compile Java to
bytecode
Bytecode: a language for
an imaginary cpu
23
Compiling and Running
Interpreter: converts one
instruction or line of code
from one language to another
and then executes that
instruction
–When java programs are run
the bytecode produced by the
compiler is fed to an interpreter
that converts it to machine code
for a particular CPU
24
Anatomy of a Java Program
=>Comments
=> Package
=> Reserved words
=> Modifiers
=> Statements
=> Blocks
=> Classes
=> Methods
=> The main
method
25
Comments
In Java, comments are preceded
by two slashes (//) in a line, or
enclosed between /* and */ in
one or multiple lines. When the
compiler sees //, it ignores all
text after // in the same line.
When it sees /*, it scans for the
next */ and ignores any text
between /* and */.
26
Package
The second line in the program
specifies a package name,
GuirreJava2020, for the class
RoseJava.
jdk compiles the source code in
RoseJava.java, generates
RoseJava.class, and stores
RoseJava.class in the
GuirreJava2020 folder.
27
Reserved Words
Reserved words or
keywords are words
that have a specific
meaning to the
compiler and cannot
be used for other
purposes in the
program.
28
Reserved Words
For example, when the
compiler sees the word
class, it understands
that the word after
class is the name for
the class. Other
reserved words in
Example 1.1 are public,
static, and void.
29
Reserved Words
30
Reserved Words
31
ModifiersJava uses certain reserved words
called modifiers that specify the
properties of the data, methods, and
classes and how they can be used.
Examples of modifiers are public and
static. Other modifiers are private,
final, abstract, and protected. A
public datum, method, or class can
be accessed by other programs. A
private datum or method cannot be
accessed by other programs.
32
StatementsA statement represents an
action or a sequence of actions.
The statement
System.out.println(“I LOVE
JAVA") in the program in
Example 1.1 is a statement to
display the greeting “I LOVE
JAVA" Every statement in Java
ends with a semicolon (;).
33
Blocks
A pair of braces in a program forms
a block that groups components of a
program.
Class Block
Method
Block
34
Classes
The class is the essential Java
construct. A class is a template or
blueprint for objects. To program
in Java, need to understand
classes and be able to write and
use them. For now, though,
understand that a program is
defined by using one or more
classes.
35
Methods
What is System.out.println?
It is a method: a collection
of statements that performs
a sequence of operations to
display a message on the
console. It can be used even
without fully understanding
the details of how it works.
36
MethodsIt is used by invoking a
statement with a string
argument. The string argument
is enclosed within parentheses.
In this case, the argument is “I
LOVE JAVA" You can call the
same println method with a
different argument to print a
different message.
37
main Method
The main method provides the
control of program flow. The Java
interpreter executes the application
by invoking the main method.
The main method looks like this:
public static void main(String[] args)
{
// Statements;
}
38
Strings and string literals
string: A sequence of text
characters (not just letters) that can
be printed or manipulated in a
program.
literal: a representation of a value
of a particular type
–String literals in Java start and
end with quotation mark
characters
"This is a string"
39
Details about Strings
A string literal may not
span across multiple lines.
"This is not
a legal String."
A string may not contain a "
character. ' is OK
"This is not a "legal" String
either."
"This is 'okay' though."
40
Details about Strings
A string can represent
certain special characters by
preceding them with a
backslash  (this is called an
escape sequence).
–t tab character
–n new line character
–" quotation mark character
– backslash character
41
The exit Method
Use Exit to terminate the program and
stop all threads.
NOTE: When the program starts, a
thread is spawned to run the program.
When the showMessageDialog is
invoked, a separate thread is spawned
to run this method. The thread is not
terminated even you close the dialog
box. To terminate the thread, you have
to invoke the exit method.
42
Practice Program 1:
Class: RoseInfo.java
WAP that will generate the
following output.
Name:FN MN LN
Course:
Year and Section:
OOP Instructor:
Expected Grade:
43
Practice Program 2
Class: RoseEscape.java
WAP that will generate the
following output.
C YS
 // /  //  /// 
'
"""
C:
Maam Rose is BeAuTiFuL.
ANSWER TO
PRACTISE
PROGRAM
44
45
Answer to Practice Program 1
System.out.print("Name tt:");
System.out.println(" ROSEMARIE SIBBALUCA GUIRRE");
System.out.print("Coursett:");
System.out.println("BACHELOR OF SCIENCE IN COMPUTER ENGINEERING");
System.out.print("Year and Section:");
System.out.println("5A");
System.out.print("OOP Instructor :");
System.out.println("DR. JEAN JANN JOZIAH S. GUIRRE ");
System.out.print("Expected Grade :");
System.out.println("1.00 ");
45
46
Answer to Practice Program 2
System.out.println ("ROSEMARIE SIBBALUCA GUIRRE");
System.out.println (" BSCOE 5A");
System.out.println (" /  //  /// ");
System.out.println ("  /  //  /// ");
System.out.println ("'");
System.out.println(""""");
System.out.println("C:");
System.out.println("Maam Rose t is BeAuTiFuL.");

Introduction to Java Programming

  • 1.
    Introduction to Java Programming PreparedBy: Dr. Rosemarie S. Guirre
  • 2.
    2 “When a programming languageis created that allows programmers to program in simple English, it will be discovered that programmers cannot speak English.” - Anonymous
  • 3.
    3 GET STARTED WITH JAVAPROGRAMMING JRE (Java Runtime) is needed for running Java programs. JDK (Java Development Kit), which includes JRE plus the development tools (such as compiler and debugger), is need for writing as well as running Java programs.
  • 4.
    1. DOWNLOAD THECOMPILER http://www.oracle. com/technetwork/ja va/javase/download s/index.html 4
  • 5.
    Under "Java Platform, StandardEdition" ⇒ "Java SE 8ux" ⇒ Click the "JDK Download" button. 5 1. DOWNLOAD THE COMPILER
  • 6.
  • 7.
    Choose your operatingplatform, e.g., Windows x86 for 32-bit Windows OS or Windows x64 for 64-bit Windows OS. You can check whether your Windows OS is 32-bit or 64-bit via "Control Panel" ⇒ System ⇒ Under the "System Type". 7 1. DOWNLOAD THE COMPILER
  • 8.
    2. INSTALL JDKAND JRE Run and install the downloaded installer. 8
  • 9.
    9 3: Include JDK's"bin" Directoryin the PATH 1.Control Panel ⇒ System ⇒ Advanced system settings 2. Switch to "Advanced" tab ⇒ Environment Variables 3. In "System Variables", scroll down to select "PATH" ⇒ Edit
  • 10.
    10 3: Include JDK's"bin" Directory in the PATH In "Variable value" field, INSERT "c:Program FilesJavajdk1.8.0_xxbin" (Replace xx with the upgrade number and VERIFY that this is your JDK's binary directory!!!) IN FRONT of all the existing directories, followed by a semi-colon (;) which separates the JDK's binary directory from the rest of the existing directories. DO NOT DELETE any existing entries; otherwise, some existing applications may not run.
  • 11.
    11 3: Include JDK's"bin" Directory in the PATH It should be like this:
  • 12.
    12 3: Include JDK's"bin" Directory in the PATH
  • 13.
    13 4: verify theJDK installation It should be like this:
  • 14.
    14 5: verifythe JDKversion It should be like this:
  • 15.
    15 Getting Started withJava Programming A Simple Java Application Compiling Programs Executing Applications
  • 16.
  • 17.
    17 Structure of Javaprograms public class <name> { public static void main(String[] args) { <statement(s)>; } } Every executable Java program consists of a class... – that contains a method named main... that contains the statements to be executed The program with a class named RoseJava, whose main method executes one statement named System.out.println
  • 18.
    18 A Simple JavaProgram public class RoseJava { public static void main(String[] args) { System.out.println(“I LOVE JAVA"); } } This would be in a text file named RoseJava.java
  • 19.
    19 The command line Torun a Java program using your Command Prompt:  change to the directory of your program cd  compile the program javac RoseJava.java  execute the program java RoseJava source code (RoseJava.java) compile byte code (RoseJava.class) execute output
  • 20.
    THIS WILL BETHE OUTPUT 20
  • 21.
    21 More Definitionscode orsource code: The sequence of instructions in a particular program. –The code in this program instructs the computer to print a message of I LOVE JAVA on the screen. output: The messages printed to the computer user by a program. console: The text box or window onto which output is printed.
  • 22.
    22 Compiling and Running Compiler:a program that converts a program in one language to another language –compile Java to bytecode Bytecode: a language for an imaginary cpu
  • 23.
    23 Compiling and Running Interpreter:converts one instruction or line of code from one language to another and then executes that instruction –When java programs are run the bytecode produced by the compiler is fed to an interpreter that converts it to machine code for a particular CPU
  • 24.
    24 Anatomy of aJava Program =>Comments => Package => Reserved words => Modifiers => Statements => Blocks => Classes => Methods => The main method
  • 25.
    25 Comments In Java, commentsare preceded by two slashes (//) in a line, or enclosed between /* and */ in one or multiple lines. When the compiler sees //, it ignores all text after // in the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */.
  • 26.
    26 Package The second linein the program specifies a package name, GuirreJava2020, for the class RoseJava. jdk compiles the source code in RoseJava.java, generates RoseJava.class, and stores RoseJava.class in the GuirreJava2020 folder.
  • 27.
    27 Reserved Words Reserved wordsor keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program.
  • 28.
    28 Reserved Words For example,when the compiler sees the word class, it understands that the word after class is the name for the class. Other reserved words in Example 1.1 are public, static, and void.
  • 29.
  • 30.
  • 31.
    31 ModifiersJava uses certainreserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. A public datum, method, or class can be accessed by other programs. A private datum or method cannot be accessed by other programs.
  • 32.
    32 StatementsA statement representsan action or a sequence of actions. The statement System.out.println(“I LOVE JAVA") in the program in Example 1.1 is a statement to display the greeting “I LOVE JAVA" Every statement in Java ends with a semicolon (;).
  • 33.
    33 Blocks A pair ofbraces in a program forms a block that groups components of a program. Class Block Method Block
  • 34.
    34 Classes The class isthe essential Java construct. A class is a template or blueprint for objects. To program in Java, need to understand classes and be able to write and use them. For now, though, understand that a program is defined by using one or more classes.
  • 35.
    35 Methods What is System.out.println? Itis a method: a collection of statements that performs a sequence of operations to display a message on the console. It can be used even without fully understanding the details of how it works.
  • 36.
    36 MethodsIt is usedby invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is “I LOVE JAVA" You can call the same println method with a different argument to print a different message.
  • 37.
    37 main Method The mainmethod provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: public static void main(String[] args) { // Statements; }
  • 38.
    38 Strings and stringliterals string: A sequence of text characters (not just letters) that can be printed or manipulated in a program. literal: a representation of a value of a particular type –String literals in Java start and end with quotation mark characters "This is a string"
  • 39.
    39 Details about Strings Astring literal may not span across multiple lines. "This is not a legal String." A string may not contain a " character. ' is OK "This is not a "legal" String either." "This is 'okay' though."
  • 40.
    40 Details about Strings Astring can represent certain special characters by preceding them with a backslash (this is called an escape sequence). –t tab character –n new line character –" quotation mark character – backslash character
  • 41.
    41 The exit Method UseExit to terminate the program and stop all threads. NOTE: When the program starts, a thread is spawned to run the program. When the showMessageDialog is invoked, a separate thread is spawned to run this method. The thread is not terminated even you close the dialog box. To terminate the thread, you have to invoke the exit method.
  • 42.
    42 Practice Program 1: Class:RoseInfo.java WAP that will generate the following output. Name:FN MN LN Course: Year and Section: OOP Instructor: Expected Grade:
  • 43.
    43 Practice Program 2 Class:RoseEscape.java WAP that will generate the following output. C YS // / // /// ' """ C: Maam Rose is BeAuTiFuL.
  • 44.
  • 45.
    45 Answer to PracticeProgram 1 System.out.print("Name tt:"); System.out.println(" ROSEMARIE SIBBALUCA GUIRRE"); System.out.print("Coursett:"); System.out.println("BACHELOR OF SCIENCE IN COMPUTER ENGINEERING"); System.out.print("Year and Section:"); System.out.println("5A"); System.out.print("OOP Instructor :"); System.out.println("DR. JEAN JANN JOZIAH S. GUIRRE "); System.out.print("Expected Grade :"); System.out.println("1.00 "); 45
  • 46.
    46 Answer to PracticeProgram 2 System.out.println ("ROSEMARIE SIBBALUCA GUIRRE"); System.out.println (" BSCOE 5A"); System.out.println (" / // /// "); System.out.println (" / // /// "); System.out.println ("'"); System.out.println("""""); System.out.println("C:"); System.out.println("Maam Rose t is BeAuTiFuL.");