Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Introduction to Programming (Java)
2/12
Michal Krátký
Department of Computer Science
Technical University of Ostrava
Introduction to Programming (Java)
2008/2009
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
1/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Introduction to Java Programming Language
James Gosling, Sun Microsystems, 1990.
The original name Oak, Java in 1993.
The first version was released in 1996 (JDK 1.0).
Features:
object-oriented language
strong typed language
it includes garbage collector
it supports parallel threads
it supports exceptions
’safe’ programming language
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
2/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
JDK and IDEs
http://java.sun.com/ – JDK – Java Platform, the
current version 1.6.
http://java.sun.com/ – NetBeans, IDE.
http://www.eclipse.org/ – Eclipse, IDE.
http://jikes.sourceforge.net/ – ’quick’ compiler.
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
3/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
References
Slides for this course:
http://www.cs.vsb.cz/kratky/.
R. Szturc: Introduction to Programming (Java).
Department of Computer Science, VŠB – Technical
University of Ostrava, 2004,
http://www.cs.vsb.cz/java/index.html
B. Eckel: Thinking in Java. 2002,
http://www.mindview.net/Books/TIJ/
Sun MicroSystems: Sun Developer Network. 2007,
http://java.sun.com/
A lot of books.
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
4/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Compilation vs Interpretation of Source Code
Compiler – a source code is compiled in an executable file.
Interpreter – statements are executed in the runtime.
Java utilizes a combination of both issues.
Code (*.java file) is compiled to the byte-code (*.class file).
The class files are interpreted by the java virtual machine
(javavm).
The virtual machine is represented by the java executable
file.
Libraries (jar archives) must be defined in the CLASSPATH
variable (the variable of the OS).
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
5/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Java Platform
Platform – hardware and software environment for a
program runtime.
Java platform – a software platform over a hardware
platform.
Java API
Java Virtual Machine
Java
Platform
Operation System
Hardware
Java API – a set of classes – input/output, networking,
databases and so on.
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
6/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Example 1.1
public class Example0101
{
public s t a t i c void main ( S t r i n g [ ] args )
{
System . o u t . p r i n t l n ( " H e l l o World ! \ n " ) ;
}
}
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
7/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Compilation and Start-up of a Program
Compilation: javac Example0101.java
Start-up: java Example0101
The start-up with the redirection of the standard output into
the file file.txt: java Example0101 > file.txt
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
8/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Compiling and Running Program in an IDE
We must create a project in an IDE (Integrated
Development Environment). Source files are added into
this project.
One file is labeled as the main file. It means that the
main() method of this file starts a program.
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
9/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
HelloWorld in NetBeans
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
10/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Debugging – Efficient Finding of Bugs 1/2
Debugging is the sole efficient tool for finding of bugs in a
source code.
We can utilize a command-line debugger or an IDE’s
debugger (NetBeans, Eclipse and so on).
We add breakpoints on lines, run-time of a program is
stopped on these lines.
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
11/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Debugging – Efficient Finding of Bugs 2/2
Functions:
Step Into - a step into a method
Step Over - a step on the next line without a step into a
method
Run - program is run until the next breakpoint is reached
Step Out - a step out from the current method
We can look:
Stack Trace - a stack of called methods
values of local and global variables
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
12/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Syntax vs Semantics
Syntax – it defines constructs to be possible in the
language. Which program is or is not correct.
Semantics – it defines the meaning of constructs. E.g.,
what is executed after a statement.
Two types of errors:
An error during a compilation – it is reported by a compiler.
A syntactic error – e.g. missing ;
A semantic error – e.g. incompatible data type is assigned.
An error in run-time – it is reported by the virtual machine
during the run-time.
We catch these errors by exceptions.
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
13/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Java Language Syntax
A program consists of classes.
Each class includes data (attributes) and methods.
Methods consists of the header (name, parameters, and
return type) and body with statements.
Statements work with variables.
Performance of statements includes the statement
evaluation.
Each variable, value or expression has a data type.
The program is a sequence of lexical symbols at the lowest
level.
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
14/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Lexical Symbols
White characters nad remarks:
White space: space (SP), horizontal tab (HT), form feed
(FF), newline (LF), carriage return (CR).
Remarks: /* remark */
// remark
Basic types of lexical symbols:
identifiers: i j System9 number_of_elements
keywords: while float int public class
literals: 124 true ’d’ "hello"
delimiters: ( ) { } [ ] ; : , .
operators: + − ∗ / && = ∗ = < >>=
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
15/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Example 2.1
public class Example0201
{
public s t a t i c void main ( S t r i n g [ ] args )
{
int i = 5;
int j = i + 3;
System . o u t . p r i n t l n ( " i = " + i + " , j = " + j
+ " \n" ) ;
i n t d=0xFF ;
/ / hexadecimal v a l u e
int e=077;
/ / octant value
System . o u t . p r i n t l n ( " d= " + d + " , e= " + e
+ " \ n " ) ; / / d =255 , e=63
}
}
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
16/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Types, values, and variables
Data is stored in variables. Each variable is specified by
the name and data type (so called declaration). We
distinguish two types of data types:
Primitive data types.
A variable of primitive data type contains the one value of
the defined size:
boolean b = true; // boolean type
int i = 456; // integer
double f = 2.71828; // real number
References
Address of an object (the instance of a class) or array in
memory:
Hashtable h = new Hashtable();
int[] a = new int[20];
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
17/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Integer Data Types 1/2
Type
byte
short
int
long
char
Range
-128..127
-32768..32767
-2147483648..2147483647
-9223372036854775808..9223372036854775807
0..65535
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
Size [b]
8
16
32
64
16
18/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Integer Data Types 2/2
Operators:
Comparison operators: <, <=, >, >=, ==, ! =
Unary plus and minus: +, −
Binary arithmetic operators: +, −, ∗, /, %
Prefix and postfix operators for +1 and -1 operations,
respectively: ++, −−
Signed and unsigned bit shifts: <<, >>, >>>
A bit complement: ~
Bit operators: & (AND), | (OR), ^ (XOR)
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
19/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Assignment Operator
An expression: expr1 = expr2.
An evaluation:
Evaluation of the left side (expr1).
Evaluation of the right side (expr2).
The value of the right side is stored into the variable of the
legth side.
The value of the whole expression is the value of the right
side.
int b = c + 1;
a[i++] = x + y;
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
20/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Example 2.3
public class Example0203
{
public s t a t i c void main ( S t r i n g [ ] args )
{
int i = 5;
i ++;
// i = 6
i n t j = i − − + 3; / / i = 5 , j = 9
j = −− i + 3 ;
// i = 4 , j = 7
j += 6;
/ / j = 13
boolean b = ( j = = 1 ) ; / / b = f a l s e ;
System . o u t . p r i n t l n ( " i = " + i + " , j = " + j +
" , b= " + b + " \ n " ) ;
}
}
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
21/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Example 2.4
public class Example0204
{
public s t a t i c void main ( S t r i n g [ ] args )
{
int i = 1 ;
/ / i = 0001
i = i < < 1;
/ / i = 2 (0010)
int j = 1;
int k = i | j ;
/ / k = 3 (0011)
k = i & j ;
// k = 0
System . o u t . p r i n t l n ( " i = " + i + " , j = " + j +
" , k= " + k + " \ n " ) ;
}
}
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
22/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Example 2.5
1/2
public class Example0205
{
public s t a t i c void main ( S t r i n g args [ ] )
{
int a = ~0;
/ / a = −1 , a = 1 1 . . . 1 1
a = a < < 1;
// a = 111 ... 110
a = −1;
i n t aa = a > > 1 ; / / aa = −1
i n t ab = a > > > 1; / / ab = 2147483647
/ / ab = 0 1 1 . . . 1 1
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
23/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Example 2.5
2/2
a = 3 < < 30;
aa = a > > 1 ;
// a = 110...........0
/ / aa = 1 1 1 0 . . . . . . . . . . 0
int b = 20 , c = 3;
int d = b / c ;
// d = 6
int e = b % c ;
// e = 2;
}
}
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
24/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Real Data Types 1/2
Real number form: s m × 2exp .
Type
float
double
Type
float
double
s
{-1,1}
{-1,1}
m
0 .. 224 − 1
0 .. 253 − 1
Min Value
1.40239846e-45f
4.94065645841246544e-324
c
2006–2008
Michal Krátký
exp
-149..104
-1075..970
Size [b]
32
64
Max Value
3.40282347e+38f
1.79769313486231570e+308
Introduction to Programming (Java)
25/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Real Data Types 2/2
Operators:
Comparison operators: <, <=, >, >=, ==, ! =
Unary plus and minus: +, −
Binary arithmetic operators: +, −, ∗, /
Prefix and postfix operators for +1 and -1 operations,
respectively: ++, −−
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
26/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Example 2.6
public class Example0206
{
public s t a t i c void main ( S t r i n g args [ ] )
{
double f = 1 . 0 ;
f = f ∗ 2 0 . 0 ; / / f = 20.0
f = f / 5.0;
/ / f = 4.0
−−f ;
/ / f = 3.0
}
}
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
27/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Bool Data Type
Bool data type has two possible values: true and false.
Operators:
Comparison operators: ==, ! =
A logical complement: !
Binary logical operators: &, |, ˆ
Conditional AND and OR operators: &&, ||
Ternary conditional operator: ?:
Boolean expressions controls these constructs:
if
while
do
for
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
28/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Example 2.7
1/2
public class Example0207
{
public s t a t i c void main ( S t r i n g [ ] args )
{
boolean f l a g = t r u e ;
flag = ! flag ;
/ / flag = false
boolean bVar = t r u e ;
boolean r 1 = f l a g | bVar ;
/ / r1 = tr ue
boolean r 2 = f l a g & bVar ;
/ / r2 = f a l s e
i f ( r1 | | r2 )
/ / tr ue or f a l s e = tr ue
{
System . o u t . p r i n t l n ( " r 1 o r r 2 = t r u e " ) ;
}
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
29/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Example 2.7
2/2
double f V a l = r 1 ? 1 . 0 : 2 . 0 ;
/ / fVal = 1.0
System . o u t . p r i n t l n ( " f l a g = " + f l a g + " , bVar= " +
bVar + " , f V a l = " + f V a l + " \ n " ) ;
}
}
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
30/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Compound Assignment Operators
∗ =, / =, % =, + =, − =, <<=, >>=, & =, |= ˆ =
The meaning of the expr1 op= expr2 is the same as
expr1 = expr1 op expr2. But then expr1 is
evaluated only one times.
x *= 6; x = x * 6;
a[i++] += 3; is not equivalent with
a[i++] = a[i++] + 3;
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
31/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Casting
Default casting:
byte b ; i n t i ;
...
i = b;
b = i; is wrong.
We must use the expression: (type)expr1, which
transforms the type of the expression value at the type.
b = (byte)i;
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
32/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Priority of Operators – from the highest priority
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
()
[], postfix ++ and −−
unary + and −, ~, !, casting, prefix ++ and −−
∗, /, %
+, −
<<, >>, >>>
<, >, <=, >=, instanceof
==, ! =
&
ˆ
|
&&
||
? :
=, ∗ =, / =, % =, + =, − =, <<=, >>=, >>>=, & =, ˆ =, |=
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
33/34
Introduction to Programming (Java)
2/12
Introduction to Java Programming Language
Java Language Syntax
Primitive Data Types
Associativity of Operators
The majority of the binary operators is associative from the left.
Therefore:
a + b + c
has the same meaning as:
(a + b) + c
Some operators are associative form the right.
a = b = c
has the same meaning as: a = (b = c)
c
2006–2008
Michal Krátký
Introduction to Programming (Java)
34/34