0% found this document useful (0 votes)
14 views39 pages

CGIS IP T1 TiposVariablesOperadores

This document provides an overview of Java programming concepts including: 1. The goals of learning Java include doing useful tasks like automating math, processing data, creating objects, and drawing graphics. 2. Assignments are submitted via Codeboard and students must collaborate, write their own code, and submit every assignment. 3. Java is an object-oriented language that runs on a virtual machine and is simpler than C++ but more complex than Python. 4. The Java compiler translates source code into bytecode that can run on any Java virtual machine.

Uploaded by

Eduardo Pozo
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)
14 views39 pages

CGIS IP T1 TiposVariablesOperadores

This document provides an overview of Java programming concepts including: 1. The goals of learning Java include doing useful tasks like automating math, processing data, creating objects, and drawing graphics. 2. Assignments are submitted via Codeboard and students must collaborate, write their own code, and submit every assignment. 3. Java is an object-oriented language that runs on a virtual machine and is simpler than C++ but more complex than Python. 4. The Java compiler translates source code into bytecode that can run on any Java virtual machine.

Uploaded by

Eduardo Pozo
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/ 39

Research Group

1: Types, Variables, Operators


Goal
Learn enough Java to do something useful

Examples
– Automate mathematical operations
– Process data
– Create and play around with objects
– Draw some graphics

2 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Assignments

• View and submit via Codeboard.io


• Collaborate with others
• Write your own code
• Must submit every assignment

3 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


The Computer

Memory

Central
Processing Unit
(CPU)

Input/Output
(IO) Devices

4 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


CPU Instructions

z=x+y Read location x


Read location y
Add

Write to location z

5 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Programming Languages
• Easier to understand than CPU instructions
• Needs to be translated for the CPU to
understand it

6 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Java
• “Most popular” programming language
• Runs on a “virtual machine” (JVM)
• More complex than some others (eg. Python)
• Simpler than others (eg. C++)

7 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Java
What is Java
– Object Oriented programming language
from the 90s
– A programming tool developd by 13
people manged by James Gosling for
the *7 project
– Syntactically similar to C/C++ but much more simple
– Platform independent: ”write once, run anywhere”
– Oak à Green à Java
• Just Another Vague Acronym
• James Gosling, Arthur Van Hoff, Andy Bechtolsteim
8 www.ise.edu.es @URJC | @ISE_URJC | @jmvara
Java
Java history
– Java 1.0 -1995
– Java 1.1 - 1997
– Java 1.2 - 1998 (Playground) à Java 2
– Java 1.3 - 2000 (Kestrel)
– Java 1.4 - 2002 (Merlín)
– Java 1.5 - 2004, (Tiger) à Java 5
– Java 1.6.0 - 2006, (Mustang) à Java 6
– Java SE7 - 2011, (Dolphin) à Java 7
– Java SE8 - 2014, (Spider) à Java 8
9 www.ise.edu.es @URJC | @ISE_URJC | @jmvara
Compiling Java
Java Virtual Machine

Source Code Byte Code


javac java
(.java) (.class)

– A JVM is needed to run a Java program

10 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Compiling Java

Compilador Java

Traduce código fuente a código


intermedio.

Interprete de Java

ByteCodes interpretados
(ejecutados)

11 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Compiling Java
Bytecode
– Native language for any JVM.
– A Java program runs in any
platform

12 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


First Program

class Hello {
public static void main(String[] arguments)
{
// Program execution begins here
System.out.println("Hello
world.");
}
}

13 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Program Structure

class CLASSNAME {
public static void main(String[]
arguments) {
STATEMENTS
}
}

14 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Output

System.out.println(some String) outputs to the


console

Example:
– System.out.println(“output”);

15 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Second Program

class Hello2 {
public static void main(String[] arguments) {
System.out.println("Hello world."); // Print once
System.out.println("Line number 2"); // Again!
}
}

16 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Types
Kinds of values that can be stored and manipulated.
– boolean: Truth value (true or false).
– int: Integer (0, 1, -47).
– double: Real number (3.14, 1.0, -2.1).
– String: Text (“hello”, “example”).

17 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Variable declaration
Named location that stores a value of one particular type.
– Form:
• TYPE NAME;
– Example
String foo;

Start with lower case and use an upper case for every new “word”
– Use intuitive and significative names for variables

18 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Assignment
Use = to give variables a value.
– Computes right part of the assignment and assigns the result to the
variable on the left

foo
Example:
String foo;
foo = "IAP 6.092";

foo

“IAP 6.092”

19 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Assignment
Can be combined with a variable declaration.

Example
double badPi = 3.14;
boolean isJanuary = true;

20 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


class Hello3 {
public static void main(String[] arguments)
{
String foo = "IAP 6.092";
System.out.println(foo);
foo = "Something else";
System.out.println(foo);
}
}

21 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Operators
Symbols that perform simple computations
– Simple arithmetic

– Combined arithmetic

22 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


String Concatenation (+)
The + operator is overriden
– Conventional add operator when used with numbers
– String concatenation when used with Strings
int a = 3;
int b = 2;
int c = a + b;
// c = 5
String text = a + "000" + b;
// text = “30002“
System.out.println(c);
System.out.println(text);

23 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Operators

Relational
Logical

24 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Truth tables

A B A OR B A B A AND B A NOT A

F F F F F F F V

F V V F V F V F

V F V V F F

V V V V V V

25 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Order of Operations
Follows standard (math) rules:
1. Parentheses

2. Arithmethic operators
1. Multiplication and division
2. Addition and subtraction
3. Relational operators
4. Logic operators

26 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Operators
Which is the resut of the following expression?

27 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Operators
Which is the resut of the following expression?

10 <= 2 * 5 && 3 < 4 || !(8 > 7) && 3 * 2 <= 4 * 2 - 1

28 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Operators
Which is the resut of the following expressions?
Datos Expresión Resultado
int x = 3;
!(x<5)&&!(y>=7)
int y = 6;
int i = 22;
!((22>4)||(3<=6))
int j = 3;
int a = 34;
int b = 12; !(a+b==c)||(c!=0)&&(b-c>=19)
int c = 8;

29 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Operators
Which is the resut of the following expressions? int i = 7;
float f = 5.5F;
char c = ‘w’;

Expresión Resultado
(i >= 6) && (c == ‘w’)

(i >= 6) || (c == 119)

(f < 11) && (i > 100)

(c != ‘p’) || ((i + f) <= 10)

i + f <= 10

i >= 6 && c == ‘w’

c != ‘p’ || i + f <= 10

30 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


class DoMath {
public static void main(String[] arguments) {
double score = 1.0 + 2.0 * 3.0;
System.out.println(score);
score = score / 2.0;
System.out.println(score);
}
}

31 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


class DoMath2 {

public static void main(String[] arguments) {


double score = 1.0 + 2.0 * 3.0;
System.out.println(score);
double copy = score;
copy = copy / 2.0;
System.out.println(copy);
System.out.println(score);
}
}

32 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Hands on
Every Java program must contains at least one Class
– Java methods container
A Java file could contain several classes, but just one of them being
Public
– File name must be that of the Public class (ClassName.java)
public class PayrollApp {

public static void main(String[] args) {


int hours = 40;
double payRate = 25.0, grossPay;

grossPay = hours * payRate;


System.out.print(″Gross Pay: $″);
System.out.println(grossPay);
}
}

33 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Hands on
Defining classes
– Class definition starts with the Class keyword
– Every declaration and instruction is located between the start and end
brackets of the class
– They are referred to as the body of the class

public class PayrollApp {

public static void main(String[] args) {


int hours = 40;
double payRate = 25.0, grossPay;
Body
grossPay = hours * payRate; Class
System.out.print(″Gross Pay: $″);
System.out.println(grossPay);
}
}

34 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Hands on
The main method
– Every program needs a main method
– It is the starting point of the program
– Always the same heading
– Always invoked when the program is run
public class PayrollApp {

public static void main(String[] args) {


int hours = 40;
double payRate = 25.0, grossPay;

grossPay = hours * payRate;


System.out.print(″Gross Pay: $″);
System.out.println(grossPay);
}
}

35 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Punctuation | Indentation
Each pair of brackets identifies a code block
class DoMath2 {

public static void main(String[] arguments) {


double score = 1.0 + 2.0 * 3.0;
System.out.println(score);
double copy = score;
copy = copy / 2.0;
System.out.println(copy);
System.out.println(score);
}
}

Code inside each block is indented to ease identificaction

36 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Comments
Human readable information in the code
– Comments are intended to introduce information that will be ignored by the
compiler inside the code
– End line comment (//) int usu = 0; // el número de usuarios

– Block comment (/* … */)

– Javadoc comment (/** ...*/)


37 www.ise.edu.es @URJC | @ISE_URJC | @jmvara
Hands on
Punctuation symbols have well-defined purposes
– ; à end of instruction
– “” à Strings are located between
– {} à blocks delimitation
– () à methods arguments
– [ ] à arrays indexing
They also guide indentation and ease the identification of the different
blocks

38 www.ise.edu.es @URJC | @ISE_URJC | @jmvara


Research Group

1: Types, Variables, Operators

You might also like