0% found this document useful (0 votes)
105 views44 pages

Introduction To Java Programming Chapter 1 & 2: Week 1

This document provides an introduction to Java programming by summarizing Java history, applications, the compiler and Java Virtual Machine (JVM). It explains that Java was created in 1991 by Sun Microsystems to enable applications to run across various devices. Java programs can be applications or applets. The compiler translates Java source code into byte code, which is executed by the JVM on different operating systems. Key elements of a Java program include classes, methods, variables, statements, comments and special characters.

Uploaded by

Shabana Tahir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
105 views44 pages

Introduction To Java Programming Chapter 1 & 2: Week 1

This document provides an introduction to Java programming by summarizing Java history, applications, the compiler and Java Virtual Machine (JVM). It explains that Java was created in 1991 by Sun Microsystems to enable applications to run across various devices. Java programs can be applications or applets. The compiler translates Java source code into byte code, which is executed by the JVM on different operating systems. Key elements of a Java program include classes, methods, variables, statements, comments and special characters.

Uploaded by

Shabana Tahir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 44

Introduction to Java Programming

Chapter 1 & 2
Week 1
Java History
• 1991 – Green Team started by Sun Microsystems
• *7 handheld controller for multiple entertainment systems
• There was a need for a programming language that would run on
various devices
• Java (first named Oak) was developed for this purpose
Introduction
• Java enabled web browser (HotJava) demonstrated at 1995 Sun World
conference
• Java is incorporated into Netscape shortly after
• Java is “cross platform”, meaning that it can run on various computer
operating systems
Java Applications and Applets
• Java programs can be of two types:
• Applications
• Stand-alone programs that run without the aid of a web browser
• Relaxed security model since the user runs the program locally
• Applets
• Small applications that require the use of a Java enabled web browser to run
• Enhanced security model since the user merely goes to a web page and the applet runs
itself
Programming Languages
Common Language Elements

• There are some concepts that are common to virtually all


programming languages
• Common concepts:
• Key words
• Operators
• Punctuation
• Programmer-defined identifiers
• Strict syntactic rules
Programming Languages
Sample Program
public class HelloWorld
{
Public static void main(String[] args)
{
String message = “Hello World”;
System.out.println(message);
}
}
Programming Languages
Sample Program

• Key words in the sample program are:


• public
• class
• static
• void
• Key words are lowercase (Java is a case sensitive language)
• Key words cannot be used as a programmer-defined identifier
• See Table 1-3 for Java key words
Programming Languages
• Semi-colons are used to end Java statements; however, not all lines of
a Java program end a statement
• Part of learning Java is to learn where to properly use the punctuation
Programming Languages
Lines vs Statements

• There are differences between lines and statements when discussing


source code
System.out.println(
message);
• This is one Java statement written using two lines
• A statement is a complete Java instruction that causes the computer
to perform an action
Programming Languages
Variables
• Data in a Java program is stored in memory
• Variable names represent a location in memory
• Variables in Java are sometimes called fields
• Variables are created by the programmer who assigns it a programmer-defined
identifier
Example: int hours = 40;

• In this example, the variable hours is created as an integer and assigned the
value of 40
Programming Languages
Variables
• Variables are simply a name given to represent a place in memory
Programming Languages
Variables
The Compiler and the Java Virtual Machine
• A programmer writes Java programming statements for a program
• These statements are known as source code
• A text editor is used to edit and save a Java source code file
• Source code files have a .java file extension
• A compiler is a program that translates source code into an executable
form
The Compiler and the Java Virtual Machine
• A compiler is run using a source code file as input
• Syntax errors that may be in the program will be discovered during
compilation
• Syntax errors are mistakes that the programmer has made that violate
the rules of the programming language
• The compiler creates another file that holds the translated
instructions
The Compiler and the Java Virtual Machine
• Most compilers translate source code into executable files containing
machine code
• The Java compiler translates a Java source file into a file that contains
byte code instructions
• Byte code instructions are the machine language of the Java Virtual
Machine (JVM) and cannot be directly executed by the CPU
The Compiler and the Java Virtual Machine
• Byte code files end with the .class file extension
• The JVM is a program that emulates a micro-processor
• The JVM executes instructions as they are read
• JVM is often called an interpreter
• Java is often referred to as an interpreted language
Program Development Process
Portability
• Portable means that a program may be written on one type of computer
and then run on a wide variety of computers, with little or no modification
• Java byte code runs on the JVM and not any particular CPU; therefore,
compiled Java programs are highly portable
• JVMs exist on many platforms:
• Windows
• Unix
• Mac
• BSD
• Linux etc.
Portability
• With most programming languages, portability is achieved by
compiling a program for each CPU it will run on
• Java provides a JVM for each platform so that programmers do not
have to recompile for different platforms
Portability
Java Versions
• The software you use to write Java programs is called the Java
Development Kit, or JDK
• There are different editions of the JDK:
• Java SE – Java2 Standard Edition
• Java EE – Java2 Enterprise Edition
• Java ME – Java2 Micro Edition
Parts of a Java Program
• A Java source code file contains one or more Java classes.
• If more than one class is in a source code file, only one of them
may be public.
• The public class and the filename of the source code file must
match.
ex: A class named Simple must be in a file named Simple.java
• Each Java class can be separated into parts.
Parts of a Java Program
• See example: Simple.java
• To compile the example:
• javac Simple.java
• Notice the .java file extension is needed.
• This will result in a file named Simple.class being created.
• To run the example:
• java Simple
• Notice there is no file extension here.
• The java command assumes the extension is .class.
Analyzing The Example
This is a Java comment. It is
// This is a simple Java program. ignored by the compiler.

public class Simple This is the class header


for the class Simple
{

This area is the body of the class Simple.


All of the data and methods for this class
will be between these curly braces.

}
Analyzing The Example
This is the method header
// This is a simple Java program. for the main method. The
main method is where a Java
public class Simple application begins.
{
public static void main(String[] args)
{ This area is the body of the main method.
All of the actions to be completed during
} the main method will be between these curly braces.

}
Analyzing The Example
// This is a simple Java program.

public class Simple


{
public static void main(String [] args)
{
System.out.println("Programming is great fun!");
}
}
This is the Java Statement that
is executed when the program runs.
Parts of a Java Program
• Comments
• The line is ignored by the compiler.
• The comment in the example is a single-line comment.
• Class Header
• The class header tells the compiler things about the class such as what other
classes can use it (public) and that it is a Java class (class), and the name of
that class (Simple).
• Curly Braces
• When associated with the class header, they define the scope of the class.
• When associated with a method, they define the scope of the method.
Parts of a Java Program
• The main Method
• This line must be exactly as shown in the example (except the args variable
name can be programmer defined).
• This is the line of code that the java command will run first.
• This method starts the Java program.
• Every Java application must have a main method.
• Java Statements
• When the program runs, the statements within the main method will be
executed.
• Can you see what the line in the example will do?
Java Statements
• If we look back at the previous example, we can see that there is
only one line that ends with a semi-colon.
System.out.println("Programming is great fun!");

• This is because it is the only Java statement in the program.


• The rest of the code is either a comment or other Java framework
code.
Java Statements
• Comments are ignored by the Java compiler so they need no semi-
colons.
• Other Java code elements that do not need semi colons include:
• class headers
• Terminated by the code within its curly braces.
• method headers
• Terminated by the code within its curly braces.
• curly braces
• Part of framework code that needs no semi-colon termination.
Short Review
• Java is a case-sensitive language.
• All Java programs must be stored in a file with a .java file extension.
• Comments are ignored by the compiler.
• A .java file may contain many classes but may only have one public
class.
• If a .java file has a public class, the class must have the same name
as the file.
Short Review
• Java applications must have a main method.
• For every left brace, or opening brace, there must be a corresponding
right brace, or closing brace.
• Statements are terminated with semicolons.
• Comments, class headers, method headers, and braces are not considered
Java statements.
Special Characters
Marks the beginning of a single line
// double slash
comment.

Used in a method header to mark the


() open and close parenthesis
parameter list.

Encloses a group of statements, such


{} open and close curly braces
as the contents of a class or a method.

Encloses a string of characters, such


“” quotation marks as a message that is to be printed on
the screen

Marks the end of a complete


; semi-colon
programming statement
Console Output
• Many of the programs that you will write will run in a
console window.
Console Output
• The console window that starts a Java application is typically known
as the standard output device.
• The standard input device is typically the keyboard.
• Java sends information to the standard output device by using a Java
class stored in the standard Java library.
Console Output
• Java classes in the standard Java library are accessed using the Java
Applications Programming Interface (API).
• The standard Java library is commonly referred to as the Java API.
Console Output
• The previous example uses the line:
System.out.println("Programming is great fun!");
• This line uses the System class from the standard Java library.
• The System class contains methods and objects that perform system
level tasks.
• The out object, a member of the System class, contains the
methods print and println.
Console Output
• The print and println methods actually perform the task of
sending characters to the output device.
• The line:
System.out.println("Programming is great fun!");
is pronounced: System dot out dot println …
• The value inside the parenthesis will be sent to the output device (in
this case, a string).
Console Output
• The println method places a newline character at the end of
whatever is being printed out.
• The following lines:
System.out.println("This is being printed out");
System.out.println("on two separate lines.");
Would be printed out on separate lines since the first statement sends a
newline command to the screen.
Console Output
• The print statement works very similarly to the println
statement.
• However, the print statement does not put a newline character at
the end of the output.
• The lines:
System.out.print("These lines will be");
System.out.print("printed on");
System.out.println("the same line.");
Will output:
These lines will beprinted onthe same line.
Notice the odd spacing? Why are some words run together?
Console Output
• For all of the previous examples, we have been printing out strings of
characters.
• Later, we will see that much more can be printed.
• There are some special characters that can be put into the output.
System.out.print("This line will have a newline at the end.\n");

• The \n in the string is an escape sequence that represents the


newline character.
• Escape sequences allow the programmer to print characters that
otherwise would be unprintable.
Java Escape Sequences
\n newline Advances the cursor to the next line for subsequent printing

\t tab Causes the cursor to skip over to the next tab stop

\b backspace Causes the cursor to back up, or move left, one position

\r Causes the cursor to go to the beginning of the current line, not


carriage return
the next line

\\ backslash Causes a backslash to be printed

\’ single quote Causes a single quotation mark to be printed

\” double quote Causes a double quotation mark to be printed


Java Escape Sequences
• Even though the escape sequences are comprised of two characters,
they are treated by the compiler as a single character.
System.out.print("These are our top sellers:\n");
System.out.print("\tComputer games\n\tCoffee\n ");
System.out.println("\tAspirin");

Would result in the following output:


These are our top seller:
Computer games
Coffee
Asprin

• With these escape sequences, complex text output can be achieved.

You might also like