Chapter1 Introduction Java
Chapter1 Introduction Java
Programming
TUNIS BUSINESS SCHOOL
I. Introduction to Java Applications; Input/Output
II. Data Types
III. Control Statements
IV. Arrays
V. Methods
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.
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!
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
}
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.
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."
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.
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
18
Comments example
/* Junior Student, IT320, Fall 2022
This program prints lyrics about ... something. */
// 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
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.
22
Console input
Using console • System.console().readLine()
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
27
Console Input: Using scanner
28
Console input :using scanner
Used to read different data types
◦ read string input
29
Console input :using scanner
Used to read different data types
◦ read short input
30
Console input :using scanner
Used to read different data types
◦ read long input
31
Console input :using scanner
Used to read different data types
◦ read double input
32
Console input: Buffer Reader
◦BufferedReader, InputStreamReader, and System.in
33