Datos y Operadores JAVA
Datos y Operadores JAVA
Programming
Grade in Computer Science
Outline
[email protected] 2
Outline
[email protected] 3
1. Basic data types and variables
Literals and variables
Data
Information processed by the program
read from the keyboard
used in calculations
printed on the screen
written on a file
…
Literals
Values directly introduced in the program
Variables
Symbols whose value change during the program execution
> piece of memory with a readable name
[email protected] 4
1. Basic data types and variables
Literals
Literals
Integers
Type Example
int
long int -2147483648, 2147483647
short long -85738593L, 8593854L
byte
short -30000, 8438, -4923
Real
byte -32, 123, 39
float
double float -3.56E+30F, 8.234
[email protected] 5
1. Basic data types and variables
Integers
Integers
Signed (positive and negative integer values)
Four types: byte, short, int, long
Range is platform independent
By default integers are of type int
For a long append an L
Example
> int
123456
-156
> long
123456L
989493849859L
-284829848L
[email protected] 6
1. Basic data types and variables
Real
Example
> double
123.45
-18.23
3.14E-5
> float
123.45F
3.45E+21F
-284829848F
Characters
Enclosed between single quotes: 'a', 'A'
Escape characters: '\'', '\b', '\t', '\n', '\\', …
UNICODE 16 bits
Each characters has an equivalent numerical code, defined by
the UNICODE standard
Unicode code '\u0065' corresponds to 'A'
[email protected] 8
1. Basic data types and variables
UNICODE table
Source: http://www.ftrain.com/unicode/
[email protected] 9
1. Basic data types and variables
Strings
[email protected] 10
1. Basic data types and variables
Literals example
Compilation error
Error in Java syntax
The program cannot run
Runtime error
Error in the execution of
the program
System.out.println
Printing instruction
Comments
Notes to the code
[email protected] 11
1. Basic data types and variables
Variables
0 ?
1 25 age
2 1.80
?
height
[email protected] 12
1. Basic data types and variables
Variables
[] optional
<> compulsory
13
1. Basic data types and variables
Variable types
[email protected] 14
1. Basic data types and variables
Variable declaration and assignation
VariablesExamples.java
a
b c
x 10
a 4
b 5
a 4 a 6
c 1
[email protected] 16
1. Basic data types and variables
Variable declaration and assignments
VariablesExamples.java
Variable assignment
Variables can be assigned to
values with different types
only under certain conditions
[email protected] 17
1. Basic data types and variables
Constants
[email protected] 18
Outline
[email protected] 19
2. Arrays
Definition and creation
[email protected] 20
2. Arrays
Array use
[email protected] 21
2. Arrays
Basic array example
a
a[0] a[1] a[2] a[3] a[4] a[5]
a 0 0 0 0 0 0
a 4 0 0 0 0 0
a 4 8 15 16 23 42
Runtime error!
ArraysExamples.java
[email protected] 22
2. Arrays
Direct assignment vs. Copy
[email protected] 23
2. Arrays
ArraysExamples.java
a
300 201 202 203 204
b
24
2. Arrays
Array copy example
c 0 0 0 0 0
ArraysExamples.java
[email protected] 25
2. Arrays
Multi-dimension arrays
up-left up-
up
right
matrix
down- down-
left down
right
[1][0] [1][1] [1][2]
ArraysExamples.java
[email protected] 27
2. Arrays
Irregular arrays
Irregular arrays are arrays that have a different number of elements in each row
E.g.: A 2-dimensional array to store the names of the 1st year students, classified by
group
Syntax for accessing values is the same as for regular arrays, but we must be
careful with the size of the arrays
[email protected] 28
3.1.5. Arrays
ArraysExamples.java
[email protected] 30
3. Input and output
Printing on the screen
System.out for printing on the screen
Methods for writing on screen:
Without a line jump
System.out.print(<String>);
With a line jump
System.out.println(<String>);
Printing.java
[email protected] 32
3. Input and output
Reading from the keyboard
Use:
1. Import java.util.* package
import java.util.*;
3. Read values
Integer: int a = sc.nextInt ();
Float: float b = sc.nextFloat();
Double: double c = sc.nextDouble();
String: String s = sc.next(); (No blank spaces)
String s = sc.nextLine(); (With blank spaces)
…
[email protected] 33
3. Input and output
Reading from the keyboard
Reading.java
34
Outline
[email protected] 35
4. Comments
Definition
[email protected] 36
4. Comments
Types
[email protected] 37
4. Comments
Example
HelloWorld.java
[email protected] 38
Outline
[email protected] 39
5. Operations with data
Operators and expressions
Expressions
An expression is a combination of data by means of one or
several operators (e.g., sum)
Data can be literal values, variables, constants, and other
expressions
> calls to methods can be also included
Two numbers
+ - * / %
One number
++ --
Increasing / decreasing a variable
[email protected] 42
5. Operations with data
Arithmetic operators
ArithmeticOperators.java
[email protected] 43
5. Operations with data
Arithmetic operators
k = ++i is equivalent to
i = i + 1;
k = i;
k = i++ is equivalent to
k = i;
i = i + 1;
[email protected] 44
5. Operations with data
Arithmetic operators
k = i++ * 2 is equivalent to
k = i * 2;
i = i + 1;
k = ++i * 2 is equivalent to
i = i + 1;
k = i * 2;
ArithmeticOperators.java
[email protected] 45
5. Operations with data
Relational operators
RelationalOperators.java
[email protected] 47
5. Operations with data
Logic operators
[email protected] 48
5. Operations with data
Logic operators
LogicOperators.java
[email protected] 49
5. Operations with data
Bitwise operators
50
5. Operations with data
Bitwise operators
Source: mozilla.org
[email protected] 51
5. Operations with data
Bitwise operators
BitwiseOperators.java
[email protected] 52
5. Operations with data
Assignment operators
Change the value of the variable on the left by the result of the
operator applied on the variable and the expression on the right
<v> <op>= <exp> is equivalent to <v> = <v> <op> <exp>
= += -= *= /= %= &=
|= ^= <<= >>= >>>=
[email protected] 53
5. Operations with data
Assignment operators
AssignmentOperators.java
[email protected] 54
5. Operations with data
Parentheses
Precedence
If not specified, expressions are evaluated in a predefined order
> not directly from left to right
Similar to usual mathematical operator precedence
a = (x + y) * z; // a : 35
a = (x * z) + (y * z); // a : 35
[email protected] 55
5. Operations with data
Precedence
[email protected] 56
Outline
[email protected] 57
6. Casting between data types
Automatic promotion and explicit casting
Automatic promotion
Assigning a value of type A to a variable of type B is only allowed when A
is “bigger” than B (no information is lost in the conversion!)
integers can be assigned to floats
float <-- int
chars can be assigned to integers
int <-- char
Type casting
The programmer can enforce the conversion in the opposite direction,
from a “bigger” type to a “smaller” type (information is lost in the
conversion!)
a float can be explicitly cast to an integer
int <-- (int) float
the floating part is removed
CastingExamples.java
[email protected] 59
Outline
[email protected] 60
7. Enumerates
Definition
E.g.:
enum DayOfTheWeek {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
[email protected] 61
7. Enumerates
Definition
CostEnumExamples.java
[email protected] 62
Outline
[email protected] 63
8. Classes as data structures
Concept
Programmers can define classes their own classes and use objects in
their applications
64
8. Classes as data structures
Class definition syntax
Class definition
[modifiers] class <name of the class> {
<attributes>
}
[modifiers]
public The class can be used by any other class
abstract Objects cannot be created for this class, but subclasses are allowed
final Subclasses are not allowed
none By default, the class can be used by the classes of the same package
66
8. Classes as data structures
Conventions for classes
A class defines the attributes (or fields) of the objects that belong to
(or are members of) the class
Attributes definition
Syntax
[modifiers] <type> <name of the attribute>;
[modifiers]
public The attribute can be accessed from any other class
private The attribute cannot be accessed from any class
other than this
protected The attribute can be accessed only from this class
and its subclasses
package The attribute can be accessed from any other class
inside this package
68
8. Classes as data structures
Example
69
8. Classes as data structures
Example
70
8. Classes as data structures
Objects
71
8. Classes as data structures
Object declaration
Basic syntax
<class name> <variable name>;
E.g.:
Point2D p1;
Student stud;
72
8. Classes as data structures
Object allocation
73
8. Classes as data structures
Object allocation
E.g.:
p1.x = 2.1;
p1.y = 3.2;
System.out.println(
"Position (" + p1.x + ", " + p1.y + ")" );
74
8. Classes as data structures
75
8. Classes as data structures
Non-initialized object references and object attributes
76
8. Classes as data structures
Example
77
8. Classes as data structures
Access to referenced objects
pointer: reference
pointee: referenced object
dereference: access to referenced object
78
8. Classes as data structures
Example
79
8. Classes as data structures
Example
80
8. Classes as data structures
Direct assignment
Object assignment
Direct object assignment is similar to direct array
assignment
(An object variable is a reference to the section of
the memory where the object attributes are actually
stored.)
> If two objects are directly assigned, they point to the
same section of the memory, and consequently, to the
same object
> Changes in one reference affect the other reference
> Object copy must be performed attribute by attribute
81
8. Classes as data structures
Direct assignment
82
Outline
[email protected] 83
Summary
Data and operators
Data in Java
Basic
integers (int, long, short), real (float, double), character (char), boolean
(boolean), strings (String)
Complex
arrays ([])
[email protected] 84
Summary
Data and operators
In assignments, the type of the variable and the type of the expression
must be compatible
Explicit casting may be convenient in some cases
Beware of direct assignment of arrays and objects
[email protected] 85
Additional lectures
Data and operators
Recommended lectures
The JavaTM Tutorials. Oracle, Language Basics [link]
H. M. Deitel, P. J. Deitel. Java: How to Program. Prentice Hall,
2007 (7th Edition), Chapters 7 [link], L [link], 3 [link],
K. Sierra, B. Bates. Head First Java. O'Reilly Media, 2005 (2nd
Edition), Chapter 3 [link]
I. Horton. Beginning Java 2, JDK 5 Edition. Wrox, 2004 (5th
Edition), Chapters 2 [link], 4 [link]
B. Eckel. Thinking in Java. Prentice Hall, 2002 (3rd Edition),
Chapters 1-3 [link]
[email protected] 86
Programming – Grado en Ingeniería Informática
Authors
Of this version:
Juan Gómez Romero
87