02 Java Basics
02 Java Basics
– Inheritance, Polymorphism
– Exception Handling
Slides 2
What is a Computer?
Slides 3
What is programming?
• Program
– A set of instructions to be carried out by a computer.
• Program execution
– The act of carrying out the instructions contained in a program.
• Programming language
– A systematic set of rules used to describe computations in a format
that is editable by humans.
Slides 4
Why Java?
• Object-oriented
• Widely used
Slides 5
Slides 6
Bigger Java program!
• Java
Slides 7
Running a program
Slides 8
Structure of a Java program
Slides 9
Anatomy of a Java program
• 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 */.
– Example: /* This is just a comment */
• 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.
– 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 the
previous example are public, static, and void.
• Modifiers
– Java uses certain reserved words called modifiers that specify the properties
of the data, methods, and classes and how they can be used.
– ……
Slides 10
Anatomy of a Java program
• Modifiers
– 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 classes. A private datum or method cannot be accessed by
other classes.
• Statements
– A statement represents an action or a sequence of actions.
– The statement System.out.println("Welcome to Java!") in the program is a
statement to display the greeting "Welcome to Java!"
– Every statement in Java ends with a semicolon (;).
• Blocks
– A pair of braces in a program forms a block that groups components of a
program.
– Each block begins with an open brace ({ ) and ends with a closing brace (}).
– Every class has a class block that groups the data and methods of the class.
– Every class has a method block that groups the statements in the method.
– Blocks can be nested, meaning that one block can be placed within another.
Slides 11
The main method
• The main method provides the control of program flow. The Java
interpreter executes the application by invoking the main method.
• Every Java application must have a user-declared main method that
defines where the program begins.
• The main method looks like this:
}
• The system locates and runs the main method for a class when you
run a program.
• Other methods get execution when called by the main method
explicitly or implicitly.
• Must be public, static and void.
Slides 12
Names and identifiers
• Naming convention
Slides 13
System.out.println
• Display on console
Slides 14
Keywords
Slides 15
Data types
Slides 16
Java type system has two parts
• Primitives types
– int, long, byte, short, char, float, double, boolean
– The difference between the various numeric primitive types is their
size, and therefore the values they can store.
Slides 17
Primitive type summary
• Example declarations:
Slides 19
Expressions
Slides 20
Division and Remainder
• If both operands to the division operator (/) are integers, the result is an
integer (the fractional part is discarded).
– 14 / 3 equals 4
– 8 / 12 equals 0
• The remainder operator (%) returns the remainder after dividing the
second operand into the first.
– 14 % 3 equals 2
– 8 % 12 equals 8
Slides 21
Operator precedence
• Arithmetic operators with the same precedence are evaluated from left
to right, but parentheses can be used to force the evaluation order.
Slides 22
increment and decrement
• The statement
count++;
is functionally equivalent to
count = count + 1;
Slides 23
increment and decrement
count++
• or prefix form:
++count
• When used as part of a larger expression, the two forms can have
different effects.
Slides 24
Syntax
Slides 25
Syntax error example
Slides 26
More on syntax errors
Slides 27
Strings
Slides 28
Escape sequences
Slides 29
Questions
Slides 30
Answers
Slides 31
Variables
Slides 32
Declaration
Slides 33
Assignment
Slides 34
Using variables
Slides 35
Declaration/initialization
Slides 36
Assignment vs. algebra
Slides 37
Exercise
Slides 38
Assignment and types
Slides 39
Compiler errors
Slides 40
Printing a variable's value
Slides 41