0% found this document useful (0 votes)
35 views87 pages

Datos y Operadores JAVA

This document discusses basic data types and variables in programming. It describes common data types like integers, floats, characters, strings, and boolean values. Variables are used to store and manipulate data in memory during program execution and must be declared with a type before use. The scope of a variable is the block of code in which it is declared, such as within a set of curly braces. Examples are provided of declaring, initializing, and assigning values to variables of different types in Java code.

Uploaded by

Nuaj Fj San
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views87 pages

Datos y Operadores JAVA

This document discusses basic data types and variables in programming. It describes common data types like integers, floats, characters, strings, and boolean values. Variables are used to store and manipulate data in memory during program execution and must be declared with a type before use. The scope of a variable is the block of code in which it is declared, such as within a set of curly braces. Examples are provided of declaring, initializing, and assigning values to variables of different types in Java code.

Uploaded by

Nuaj Fj San
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

Lesson 2

Data and Operators

Programming
Grade in Computer Science
Outline

1. Basic data types and variables


2. Input and output
3. Comments
4. Arrays
5. Operations with data
6. Casting between data types
7. Enumerates
8. Classes as data structures

[email protected] 2
Outline

1. Basic data types and variables


2. Arrays
3. Input and output
4. Comments
5. Operations with data
6. Casting between data types
7. Enumerates
8. Classes as data structures

[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

Characters double -2.49E+300, 3.95E+200


char char ‘a’, ‘D’, ‘\n’, ‘\\’, ‘\”’
Boolean boolean true, false
boolean String "hello world!"
String
String

[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

Real (floating point)


Two types: float, double
By default floats are of type double
For a float append an F

Example
> double
123.45
-18.23
3.14E-5
> float
123.45F
3.45E+21F
-284829848F

Special values for float and double:


Infinity (Inf), -Infinity (-Inf), not a number (NaN)
These values may appear as a result of an operation, but cannot be directly
assigned
[email protected] 7
1. Basic data types and variables
Characters

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'

Characters and integers can be interchanged in some cases


Integer value 65 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

Strings are complex data types to represent and


manage a string of characters
Enclosed between double quotes " " (shift + 2 key)
"Hello world!"
"My name is Bond"

Strings can be concatenated with the + operator


"My name is Bond"

[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

Variables store data that can be changed during the


execution of a program
Can be seen as a piece of the memory to store a piece of data
User-defined readable name for a cell of the memory
When the name (or identifier) of the variable is used in the program, the
information at the address of the variable is accessed

0 ?
1 25 age
2 1.80
?
height

[email protected] 12
1. Basic data types and variables
Variables

Variables store data that can be changed during the


execution of a program
Java is a strongly typed language: Necessary to declare a
variable before it is used and define the type of the
variable
!!!
Java Syntax for declaration of variables:
<type> identifier [=value] [, identifier[=value]…];

int, optional: definition of


name optional
char… several variables

[] optional
<> compulsory
13
1. Basic data types and variables
Variable types

Type Contains Default Size Range


boolean true or false false 1 bit NA
char Unicode character '\u0000' 16 bits '\u0000' to '\uFFFF'
byte Signed integer 0 8 bits -128 to 127
short Signed integer 0 16 bits -32768 to 32767
int Signed integer 0 32 bits -2147483648 to 2147483647
long Signed integer 0 64 bits -9223372036854775808 to
9223372036854775807
float IEEE 754 floating 0.0 32 bits ±1.4E-45 to ±3.4028235E+38
point
double IEEE 754 floating 0.0 64 bits ±4.9E-324 to
point ±1.7976931348623157E+308

String Unicode character Empty - -


string string

[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

Variable declaration Variable initialization Variable definition


Memory is allocated First value assignment Declaration + initialization
15
1. Basic data types and variables
Variable scope

Variables are not valid in a whole program


Names can be reused
Side-effects are avoided

Scope: Section of the code where the variable is


valid and can be used
The scope of a variable encompasses is the block of
code in which it is declared
A block is delimited by braces {}
Also named curly brackets

[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

Special variables whose value cannot be changed during


the execution of the program
Use final in the declaration of a variable to make it
constant:
final <type> <identifier> [= value];
Constants are used as variables
E.g.:
final double PI = 3.14;
double r = 5;
double a = 2 * PI * r;

The value of a constant can be modified only once!


Otherwise, we get a compilation error.

[email protected] 18
Outline

1. Basic data types and variables


2. Arrays
3. Input and output
4. Comments
5. Operations with data
6. Casting between data types
7. Enumerates
8. Classes as data structures

[email protected] 19
2. Arrays
Definition and creation

Arrays are collections of elements of the


same type which are collectively managed
Creation
Syntax for declaration of one-dimensional arrays
<type> [] <identifier>;
E.g.:
int [] myArray;

Syntax for initialization of one-dimensional arrays


<identifier> = new <type>[<nº of elements>];
E.g.:
myArray = new int[10];

[email protected] 20
2. Arrays
Array use

Syntax for accessing values Array elements have a default value


<identifier>[<position>]; Array elements do not have to be
initialized before using them in an
E.g.: expression
System.out.println(myArray[2]); Default values are 0 for numbers and characters,
false for booleans, null for Strings
Syntax for value assignment
identifier[<position>] = <value>;
E.g.:
myArray[3] = 28;

Syntax for multi-value assignment (only in initialization)


identifier = new <type>[] {<list of values>};
E.g.:
myArray = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Use length to get the size of an array


E.g.:
System.out.println(myArray.length)

[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

Array index out of bounds 4 8 15 16 23 42


a[6]
Accessing to a non-allocated position
a
of an array is a serious mistake
resulting in a runtime error

Runtime error!

ArraysExamples.java
[email protected] 22
2. Arrays
Direct assignment vs. Copy

Syntax for array assignment


Contents are not copied in a direct assignment! Both identifiers refers to the
same array
<identifier1> = <identifier2>;
E.g.:
myArray_1 = myArray_2;

Syntax for array copy


Contents are copied! Both identifiers refers to different arrays
Option 1: <identifier1>[index] = <identifier2>[index];
E.g.:
myArray_1[0] = myArray_2[0];

Option 2: System.arrayCopy(source_array, source_position,


destination_array, destination_position, n_elements_to_copy)
E.g.:
System.arrayCopy(myArray_2, 0, myArray_1, 0, myArray_1.length);

[email protected] 23
2. Arrays

ArraysExamples.java

a 100 101 102

b 200 201 202 203 204

100 101 102


a

200 201 202 203 204


b

a
300 201 202 203 204
b
24
2. Arrays
Array copy example

b 300 201 202 203 204

c 0 0 0 0 0

b 300 201 202 203 204

c 300 201 202 203 204

b 300 201 202 203 204

c 500 201 202 203 204

ArraysExamples.java
[email protected] 25
2. Arrays
Multi-dimension arrays

Multi-dimension arrays can be also created


Syntax for declaration of two-dimensional arrays
<type> [][] identifier;
E.g.:
int [][] my2DMatrix;

Syntax for initialization of two-dimensional arrays


identifier = new <type>[<nº elements>][<nº elements>];
E.g.:
my2DMatrix = new int[3][3];

Syntax for value assignment of two-dimensional arrays


identifier[<position>][<position>] = <value>;
E.g.:
my2DMatrix[1][2] = 17;

Syntax and use can be extended to n-dimension arrays


[email protected] 26
2. Arrays
Multi-dimension arrays example

[0][0] [0][1] [0][2]

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 declaration of irregular two-dimensional arrays is the same as for


regular arrays
<type> [][] <identifier>;
E.g.:
String [][] students;

Syntax for initialization of irregular two-dimensional arrays is different! Each


row is created with a different new instruction.
<identifier> = new <type>[<nº rows>] [];
E.g.:
students = new String[2][];
students[0] = new String[23]; // Students of grade in Computer Eng.
students[1] = new String[36]; // Students of grade in Comm. Syst.

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

[0][0] [0][1] [0][2] [0][22]


“De “Zimmerma
Andres “Aubert “Beltran
nn
Lopez, Gilbart, de la . Casado,
G.” P.” Cita, J.”
M.”
students
“Alonso “Alvarez “Amigo “Ventero
Martínez, Fernandez Herrera,  Peña,
C.M.” , P.” V.” V.M.”
[1][0] [1][1] [1][2] [1][35]
29
Outline

1. Basic data types and variables


2. Arrays
3. Input and output
4. Comments
5. Operations with data
6. Casting between data types
7. Enumerates
8. Classes as data structures

[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>);

A line jump can be achieved by writing a new line character '\n'


> println("Hi!") is equivalent to print("Hi!\n")

Strings can be concatenated with the + operator within printing


instructions
Other values with different datatypes can be appended with the +
operator
> Java automatically converts them into the corresponding string
Arrays must be printed element-by-element!
[email protected] 31
3. Input and output
Printing on the screen

Printing.java
[email protected] 32
3. Input and output
Reading from the keyboard

Scanner class can be used to read values from the keyboard

Use:
1. Import java.util.* package
import java.util.*;

2. Declare and initialize a Scanner object sc


Scanner sc = new Scanner(System.in);

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

1. Basic data types and variables


2. Arrays
3. Input and output
4. Comments
5. Operations with data
6. Casting between data types
7. Enumerates
8. Classes as data structures

[email protected] 35
4. Comments
Definition

Comments are notes to the code that are not


executed
Its very important to comment the code well:
Makes the code readable and understandable
Although we now know perfectly what it does, perhaps
within years we will have to reuse it
Perhaps other programmers reuse our code and need to
understand it
It is a good practice to introduce a comment at the
beginning of each file describing what it does

[email protected] 36
4. Comments
Types

Single line comments


Using the characters //
Everything appearing on the right is a comment,
and it is ignored by the compiler

Multiple line comments


Using the characters /* for the beginning of the
comment, and */ for the end
Everything written in between is a comment, and
it is ignored by the compiler

[email protected] 37
4. Comments
Example

HelloWorld.java

[email protected] 38
Outline

1. Basic data types and variables


2. Arrays
3. Input and output
4. Comments
5. Operations with data
6. Casting between data types
7. Enumerates
8. Classes as data structures

[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

Data symbols in an expression are called operands


operator
operand operand
a + b
Expression composition is guided by rules
For example, operands must have a concrete type to be used in an
operation
Non-initialized variables cannot be used in expressions
Compilation error
40
5. Operations with data
Operators

Operations with data


Arithmetic
Operate with numbers; the result is a number
Relational
Operate with numbers; the result is true/false
Conditional
Operate with true/false; the result is true/false
Bitwise
Operate with the binary representation of integer numbers; the result is
a number
Assignment
Perform an operation on an expression and assign the resulting value to a
variable

Expressions have a returning value


Returning values have a type
Expressions are said to have type
[email protected] 41
5. Operations with data
Arithmetic operators

Two numbers
+ - * / %

One number
++ --
Increasing / decreasing a variable

They can be used in prefix or suffix, and they have a different


precedence
Ej.:
x++ means increment x in 1
++y means increment y in 1

[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

Used for comparisons


== != > < >= <=
Have a boolean value as result (true/false)
E.g.:
boolean result;
int x = 10, y = 16;
result = x == y; // result is false
result = x <= y; // result is true

For String comparisons, use equal method


[email protected] 46
5. Operations with data
Relational operators

RelationalOperators.java
[email protected] 47
5. Operations with data
Logic operators

Used for operations between boolean values


AND: & && OR: || | NOT: !

Logic operators are usually combined with relational


operators to compose complex conditions

Result is a boolean value a AND b a OR b


b is true b is false b is true b is false
E.g.: a is true true false true true
boolean result;
int x = 10, y = 16; a is false false false true false
result = (x != 0) & (x <= y); // true
result = (x <= y) || (y > 100); // true

| is OR; || is OR “short-circuit” (same for &, &&)


> the evaluation stops when the result is known

[email protected] 48
5. Operations with data
Logic operators

LogicOperators.java
[email protected] 49
5. Operations with data
Bitwise operators

Operations on the bit-based internal representation of integer


values
~ NOT
& AND
| OR
^ XOR
>> SHIFT right
>>> SHIFT right with carry
<< SHIFT left

Have an int value as result


> short and byte are promoted to int

Ej.: x 64 0000 0000 0000 0000 0000 0000 0100 0000


int x = 64;
int y = x << 2; y 0000 0000 0000 0000 0000 0001 0000 0000

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>

= += -= *= /= %= &=
|= ^= <<= >>= >>>=

Abbreviation for an operation and a assignment


E.g.:
int x = 10, y = 2;
y += x; // y = y + x; (y : 12)
y -= ++x; // y = y – (x + 1); (y : -9)

Special abbreviation involving boolean values:


<variable> =
<logical expression> ?
<value if true> : <value if false>;

[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

Parentheses () are used when:


The order of operator application is ambiguous
We want to give higher precedence to some operators over others
We want to make the code more readable / understandable
E.g.:
int x = 3, y = 4, z = 5;
a = x + y * z; // a : 23
a = x + (y * z); // a : 23

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

1. Basic data types and variables


2. Arrays
3. Input and output
4. Comments
5. Operations with data
6. Casting between data types
7. Enumerates
8. Classes as data structures

[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

Direct assignment, no special code is required

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

Use the explicit casting operator


(<destination type>) (besides the expression to cast)
58
6. Casting between data types
Examples

CastingExamples.java

[email protected] 59
Outline

1. Basic data types and variables


2. Arrays
3. Input and output
4. Comments
5. Operations with data
6. Casting between data types
7. Enumerates
8. Classes as data structures

[email protected] 60
7. Enumerates
Definition

New data types can be created by enumeration of the allowed values


of the new type
> Create a new type named DayOfTheWeek with allowed values {Mon, Tue,
Wed, Thu, Fri, Sat, Sun}
New variables with type DayOfTheWeek can be created
These variables can store the values defined in the enumerate
Syntax
Definition
enum <type identifier> {<value 1>, …, <value n>};

E.g.:
enum DayOfTheWeek {Mon, Tue, Wed, Thu, Fri, Sat, Sun};

enum declarations must be outside of the main procedure!


Use
E.g.:
DayOfTheWeek x;
x = DayOfTheWeek.Mon;

[email protected] 61
7. Enumerates
Definition

CostEnumExamples.java

enums are similar to Strings


but enums restrict the possible values of the
“string”

[email protected] 62
Outline

1. Basic data types and variables


2. Arrays
3. Input and output
4. Comments
5. Operations with data
6. Casting between data types
7. Enumerates
8. Classes as data structures

[email protected] 63
8. Classes as data structures
Concept

An object can be seen as a data structure that represents an


entity of the domain
Object Entry #5 of an address book Object 2D point p
“Juan” (2.1, 3.2)
“Gomez Romero”
29
[email protected]

> collection of values of different types which are managed together


An object belongs to a class, where the attributes or fields of the
objects of the class are defined
Class Entry of an address book Class 2D point
> Name > x coordinate
> Surname > y coordinate
> Age
> E-mail

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

<name of the class>


valid Java identifier

E.g.: public class Point2D { … }


65
8. Classes as data structures

66
8. Classes as data structures
Conventions for classes

Only a single public class is allowed within a file.


That file should be named after the public class that
contains with the extension “.java”
Usually, an application consists of numerous .java
files
Compilation (javac) converts each class definition
(.java) into bytecode (.class)
The execution of the application starts from the
class that contains the main()
Several classes can be grouped in packages, in the
same way as classes of the Java platform
67
8. Classes as data structures
Class attributes definition

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

E.g.: public double x;

68
8. Classes as data structures
Example

69
8. Classes as data structures
Example

70
8. Classes as data structures
Objects

Classes are not directly used

Instead, once classes have been implemented,


1. Create a class with a main method
> the program begins here

2. Inside the main,


1. Declare object variables
2. Create objects (allocate memory for an object instance)
3. Operate with objects

71
8. Classes as data structures
Object declaration

1. Declare object variables


Object variables are declared as basic data type
variables
An object declaration declares a reference to the object,
not the object itself

Basic syntax
<class name> <variable name>;

E.g.:
Point2D p1;
Student stud;

72
8. Classes as data structures
Object allocation

2. Create objects (allocate memory)


Operator new creates a new object of a class (memory
for the object is allocated)
This object is assigned to a reference (variable
previously defined) of the type of the class
Basic syntax
<variable name> = <new> <class name>();
E.g.:
p1 = new Point2D();
st = new Student();

73
8. Classes as data structures
Object allocation

3. Operate with objects


Use the dot operator (.) to access to object attributes
> attributes can be seen as a collection of variables grouped in
the object

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

• Object references initial value


– The value of an object reference may be the special value null
– null means ‘not a valid reference’ and can be also used for arrays
and String
– If we try to access to the attributes of a null reference, we get a
runtime error (NullPointerException)

• Object attributes initial value


– The attributes of an object have a default value after creation with
new (0 for integers, false for boolean, null for String, etc.) –in the
same way as arrays
– An initial value (other than the default) can be assigned to object
attributes in the class declaration
– Until changed, this is the value of the attributes of any object of the
class

76
8. Classes as data structures
Example

77
8. Classes as data structures
Access to referenced objects

Pointer fun! http://cslibrary.stanford.edu/104/


(http://youtu.be/vm5MNP7pn5g)

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

1. Basic data types and variables


2. Input and output
3. Comments
4. Arrays
5. Operations with data
6. Casting between data types
7. Enumerates
8. Classes as data structures

[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 ([])

Variables are used to store values


Variable type is assigned in the variable declaration

Printing (System.out) and reading (Scanner)

[email protected] 84
Summary
Data and operators

Operators (arithmetic, relational, logical, bitwise, assignment)

Use of parenthesis when precedence is not clear or the code is


confusing

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

Use of comments in the code is fundamental

Programmers can define their own data types


Enumerators
Classes

[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

Based on the work by:


Ángel García Olaya
Manuel Pereira González
Silvia de Castro García
Gustavo Fernández-Baillo Cañas

87

You might also like