0% found this document useful (0 votes)
63 views33 pages

Chapter1 Introduction Java

The document outlines an introduction to object-oriented programming in Java, covering topics like inheritance, polymorphism, interfaces, exception handling, and accessing databases. It discusses Java's development including being platform independent and using a Java Virtual Machine. The summary provides an overview of compiling and running Java programs, the structure of a Java class with a main method, and using print statements and comments.

Uploaded by

success Success
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
63 views33 pages

Chapter1 Introduction Java

The document outlines an introduction to object-oriented programming in Java, covering topics like inheritance, polymorphism, interfaces, exception handling, and accessing databases. It discusses Java's development including being platform independent and using a Java Virtual Machine. The summary provides an overview of compiling and running Java programs, the structure of a Java class with a main method, and using print statements and comments.

Uploaded by

success Success
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 33

Object Oriented

Programming
TUNIS BUSINESS SCHOOL
I. Introduction to Java Applications; Input/Output
II. Data Types
III. Control Statements
IV. Arrays
V. Methods

Outline VI. Object-Oriented Programming


1. Inheritance
2. Polymorphism and Interfaces

VII. Generic Collections


VIII. Exception Handling
IX. Accessing Databases with JDBC
X. GUI Components

2
Java technology and environment (1/2)
Developed by James Gosling and his team at Sun
Microsystems (now a subsidiary of Oracle)
Platform independence
◦ It was designed to run on multiple hosts without any
changes.
Java’s Development Kit, or JDK,
◦ includes all tools, executables and binaries, which you can
use to compile, execute and debug your Java programs.

3
Java technology and environment (2/2)
Java Virtual Machine (JVM)
◦ The compiled code is executed by a JVM and not a real
machine.
◦Java code to be compiled just once but still be able to
execute on multiple diverse machine or platforms.
Java Standard Edition (Java SE)
◦ Development platform that enable developers to create
secure, portable, high-performance applications for the widest
range of computing platforms

4
Java technology and environment (3/3)
Java Runtime Environment (JRE)
◦ Implementation of a JVM. It loads the classes, defines and
accesses JVM memory, connects with native code etc.
◦ Includes the Java binaries and classes, which are required
to execute your Java code. It excludes the tools that are
used to develop Java programs, like a compiler or a
debugger.
For Java programming, you’d need a JDK.

5
Compile/run a program
Write it.
code or source code: The set of instructions in a program.

Compile it. (javac)


byte code: The Java compiler converts your
compile: Translate a program from one
code into a format named byte code that
language to another.
runs on many computer types.

Run (execute) it. (java)


output: The messages printed to the user by a program.

6
A Java program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
System.out.println();
System.out.println("This program produces");
System.out.println("four lines of output");
}
}
Its output:
Hello, world!

This program produces


four lines of output

console: Text box into which


the program's output is printed.

7
Structure of a Java program
public class name { class: a program
public static void main(String[] args) {
statement;
statement;
...
statement; method: a named group
} of statements
}

statement: a command to be executed

Every executable Java program consists of a class,


◦ that contains a method named main,
◦ that contains the statements (commands) to be executed.

8
System.out.println
A statement use
that prints a System.out.println
line of output System.out.println("text");
• Prints the given message as
output.
System.out.println();
• Prints a blank line of output.

on the console.

9
Names and identifiers
You must give your program a name.
◦public class MyNewCourse{
◦Naming convention: capitalize each word (e.g.
MyClassName)
◦Your program's file must match exactly
(MyNewCourse.java)
◦ includes capitalization (Java is "case-sensitive")

10
Names and identifiers
Identifier: A name given to an item in your program.
◦must start with a letter or _ or $
◦subsequent characters can be any of those or a number
◦ legal: _myName TheCure ANSWER_IS_42 $bling$
◦ illegal: me+u 49ers side-swipe Ph.D's

11
Keywords
keyword: An identifier that you cannot use because it already has a reserved meaning in Java.

abstract default if private this


boolean do implements protected throw
break double import public throws
byte else instanceof return transient
case extends int short try
catch final interface static void
char finally long strictfp volatile
class float native super while
const for new switch
continue goto package synchronized

12
Syntax
syntax: The set of legal structures and commands that can be used in a particular language.
◦ Every basic Java statement ends with a semicolon ;
◦ The contents of a class or method occur between { and }

syntax error (compiler error): A problem in the structure of a program that causes the compiler
to fail.
◦ Missing semicolon
◦ Too many or too few { } braces
◦ Illegal identifier for class name
◦ Class and file names do not match
...

13
Syntax error example
1 public class Hello {
2 pooblic static void main(String[] args) {
3 System.owt.println("Hello, world!")_
4 }
5 }
Compiler output:
Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
Hello.java:3: ';' expected
}
^
2 errors

◦ The compiler shows the line number where it found the error.
◦ The error messages can be tough to understand!

14
Strings
string: A sequence of characters to be printed.
◦ Starts and ends with a " quote " character.
◦ The quotes do not appear in the output.

◦ Examples:
"hello"
"This is a string. It's very long!"

Restrictions:
◦ May not span multiple lines.
"This is not
a legal String."

◦ May not contain a " character.


"This is not a "legal" String either."

15
Escape sequences
escape sequence: A special sequence of characters used to represent certain special characters
in a string.
\t tab character
\n new line character
\" quotation mark character
\\ backslash character

◦ Example:
System.out.println("\\hello\nhow\tare \"you\"?\\\\");
◦ Output:
\hello
how are "you"?\\

16
Comments
comment: A note written in source code by the programmer to describe or clarify the code.
◦ Comments are not executed when your program runs.

Syntax:
// comment text, on one line
or,
/* comment text; may span multiple lines */

Examples:
// This is a one-line comment.

/* This is a very long


multi-line comment. */

17
Using comments
Where to place comments:
◦ at the top of each file (a "comment header")
◦ at the start of every method (seen later)
◦ to explain complex pieces of code

Comments are useful for:


◦ Understanding larger, more complex programs.
◦ Multiple programmers working together, who must understand each other's code.

18
Comments example
/* Junior Student, IT320, Fall 2022
This program prints lyrics about ... something. */

public class BaWitDaBa {


public static void main(String[] args) {
// first verse
System.out.println("Bawitdaba");
System.out.println("da bang a dang diggy diggy");
System.out.println();

// second verse
System.out.println("diggy said the boogy");
System.out.println("said up jump the boogy");
}
}

19
Tools
Java Development Kit (JDK)
• http://www.oracle.com/technetwork/java/javase/downloads

The NetBeans IDE


• https://netbeans.apache.org/download/

20
Input/Output
Input/output
Input : any information that is needed by your program to complete its
execution.
• taken from the command line
• read from a file
• etc.

Output : any information that the program must convey to the user.

• prints a string of characters as output.


• print graphics
• create sound

22
Console input
Using console • System.console().readLine()

Using scanner • java.util.Scanner and System.in


class
• BufferedReader, InputStreamReader,
Using buffer and System.in
23
Console input: using console
◦System.console().readLine()
◦easy to use but does not work with IDE (such Eclipse or
Netbeans).
◦You need to use command line window for it to work
Example Execution

24
Console input: using scanner
java.util.Scanner and System.in

Steps
1. Use the System.in object to create a Scanner object.
2. Display a prompt to the user for the desired data.
3. Use the Scanner object to read a line of text from the user.
4. Process the input received from the user.

25
Console Input: Using scanner

26
Console Input: Using scanner

• Finds and returns the next complete token


from the scanner.

next() • It can read the input only till the space.


• It can’t read two words separated by space.
• Also, next() places the cursor in the same
line after reading the input.

27
Console Input: Using scanner

• Returns a line of text as typed by the user.


• It advances the scanner past the current line
and returns the input that was skipped.
nextLine() • It reads input including space between the
words (that is, it reads till the end of line n).
• Once the input is read, nextLine() positions
the cursor in the next line.

28
Console input :using scanner
 Used to read different data types
◦ read string input

◦ read byte input

29
Console input :using scanner
 Used to read different data types
◦ read short input

◦ read int input

30
Console input :using scanner
 Used to read different data types
◦ read long input

◦ read float input

31
Console input :using scanner
 Used to read different data types
◦ read double input

◦ read boolean input

32
Console input: Buffer Reader
◦BufferedReader, InputStreamReader, and System.in

33

You might also like