Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

02 Java Basics

This document provides an overview of an introductory Java lecture at Ambo University. The lecture covers Java basics including classes, objects, inheritance, polymorphism, exception handling, and GUI programming. It discusses what computers and programming are, why Java is used, and how to run and structure a basic Java program. The lecture also covers Java elements like comments, keywords, modifiers, statements, and blocks. It provides examples of printing output, data types, expressions, operators, and variables.

Uploaded by

Tolesa Shore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

02 Java Basics

This document provides an overview of an introductory Java lecture at Ambo University. The lecture covers Java basics including classes, objects, inheritance, polymorphism, exception handling, and GUI programming. It discusses what computers and programming are, why Java is used, and how to run and structure a basic Java program. The lecture also covers Java elements like comments, keywords, modifiers, statements, and blocks. It provides examples of printing output, data types, expressions, operators, and variables.

Uploaded by

Tolesa Shore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Ambo University, Institute of Technology

Lecture 02 : Introduction to Java elements

Miressa M. Electrical and Computer


Engineering Department
Course information

Lecturer: Miressa M. (M.Sc.)

Lectures: Wednesdays 8:30 – 11:30 am

Tutorials: Tuesdays 1:30 – 3:30 pm

Lecture room: LH-5

Credit hours: 5 ECTS

Contents: – Java basics

– classes and objects

– Inheritance, Polymorphism

– Exception Handling

– GUI and Event driven programming

– Introduction to object-oriented software design

Slides 2
What is a Computer?

• A computer is an electronic device that stores and processes data.


• A computer includes both hardware and software.
– Hardware is the physical aspect of the computer that can be seen.
– Software is the invisible instructions that control the hardware and make it
work.
– Computer programming consists of writing instructions for computers to
perform.
• A computer consists of the following hardware components
– CPU (Central Processing Unit
– Memory (Main memory)
– Storage Devices (hard disk, floppy disk, CDs)
– Input/Output devices (monitor, printer, keyboard, mouse)
– Communication devices (Modem, NIC (Network Interface Card)).

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.

• The programming language we are using throughout this course is


called Java.

Slides 4
Why Java?

• Object-oriented

• Relatively simple(e.g. simpler than C++)

• Platform independent (Mac, Windows…)

• Widely used

• Runs on a “virtual machine” (JVM)

Slides 5
Slides 6
Bigger Java program!

• Java

Slides 7
Running a program

• Running the code

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:

public static void main(String[] args) {

}
• 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.

• Object reference types


– Classes, interfaces, arrays, enums, annotations

Slides 17
Primitive type summary

Signed and unsigned numbers


• Unsigned Integers: positive and 0 integers. (e.g. 0, 1, 2, … 99999…)
• Signed Integers: negative, positive and 0 integers. (e.g. …-2, -1, 0, 1,
990..)
• An 8-bit value such as 10001011 can naturally be interpreted as either
unsigned number (27 +23 + 21 + 20 = 139) or as a signed number
(−27 + 23 + 21 + 20 = −117)
Slides 18
Characters

• A char variable stores a single character

• Character literals are delimited by single quotes:


'a' 'X' '7' '$' ',' '\n'

• Example declarations:

char grade = 'A';


char terminator = ';', separator = ' ';

• Note the distinction between a primitive character variable, which holds


only one character, and a String object, which can hold multiple
characters

Slides 19
Expressions

• An expression is a combination of one or more operators and operands


• Arithmetic expressions compute numeric results and make use of the
arithmetic operators:
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
• If either or both operands used by an arithmetic operator are floating
point, then the result is a floating point.

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

• Operators can be combined into complex expressions

result = total + count / max - offset;

• Operators have a well-defined precedence which determines the order


in which they are evaluated

• Multiplication, division, and remainder are evaluated prior to addition,


subtraction, and string concatenation

• 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 increment and decrement operators use only one operand.

• The increment operator (++) adds one to its operand.

• The decrement operator (--) subtracts one from its operand.

• The statement

count++;

is functionally equivalent to
count = count + 1;

Slides 23
increment and decrement

• The increment and decrement operators can be applied in postfix form:

count++

• or prefix form:

++count

• When used as part of a larger expression, the two forms can have
different effects.

• Because of their subtleties, the increment and decrement operators


should be used with care

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

You might also like