1autor: eder chavez acha. public int wrong(){ int i; return i+5; } las variables si se tienen que...

22
JAVA CLASE 3 1 AUTOR: EDER CHAVEZ ACHA

Upload: enrique-godoy-maldonado

Post on 03-Feb-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 1

JAVACLASE 3

Page 2: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 2

public int wrong(){int i;return i+5;

}

• Las variables si se tienen que inicializar o marca el siguiente error al momento de compilar:

¿Cuándo no de inicializan?

CORRECCION VARIABLES

Ejemplo_1.java:13: variable x might not have been initi alized

System.out.println(x);

Page 3: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 3

Automáticamente cambia el tipo de un dato a otro cuando es pertinente.

Por ejemplo:• Pasar de int a long

void moldeos() {int i = 100;long l = (long)i;long l2 = (long)200;

}

MOLDEO DE OPERADORES “casting”

Page 4: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 4

• Pasar de int a float

void moldeos() {

int i = 100;float f = (float)i;

}RESULTADO=100.0

MOLDEO DE OPERADORES “casting”

Page 5: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 5

MOLDEO DE OPERADORES “casting”

• Pasar de char a int

void moldeos() {

char c='A';int n;n=(int)c;

}RESULTADO=65

Page 6: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 6

MOLDEO DE OPERADORES “casting”

• Pasar de int a String

void moldeos() {

String s= (String)i;}RESULTADO:Ejemplo_3.java:34: inconvertible

typesfound : intrequired: java.lang.String String s= (String)i;

void moldeos() {

Int i=64;String s=""+i;

}RESULTADO: 64

Page 7: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 7

MOLDEO DE OPERADORES “casting”• Pasar de String a int

void moldeos() {

String s=“hola";int d=Integer.parseInt(s);

}RESULTADO:Exception in thread

"main"java.lang.NumberFormatException: For input string: “hola"

at java.lang.NumberFormatException.forInputString(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Ejemplo_3.main(Ejemplo_3.java:39)

void moldeos() {

String s="10";int

d=Integer.parseInt(s);}RESULTADO: 10

Page 8: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 8

MOLDEO DE OPERADORES “casting”

• Pasar de int a byte//rango de byte -128 <-> 127

byte b;int in=256;b=(byte)in;

RESULTADO=0

256 128 64 32 16 8 4 2 1

1 0 0 0 0 0 0 0 0

|--------------------------------TRUNCA--------------------------------|

Page 9: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 9

ERROR

• Error de precisión.

void moldeos() {

System.out.println(1-0.1-0.1-0.1);}RESULTADO=0.7000000000000001

Page 10: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 10

Estructura general:

Ejemplo:Int c[]; //declara el arregloc= new int[4]; //crea el arreglo----------------------------------------------------float[] diametros={1.1.f,2.2f,3.3f,4,4f};

ARREGLOS

Tipo nombre_variable [];

Page 11: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 11

0 0 0 0 0 c[0] c[1] c[2] c[3] c[4]

ARREGLOS

c= new int[5];

Inicializar:

• Numerios->0• Booleanos->false• Clases->null

Page 12: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 12

Tipo elemento Valor inicialByte 0int 0float 0.0Fchar ‘\u0000’Referencia a objeto nuloshort 0long 0Ldouble 0.0dboolean falso

ARREGLOS

Valor inicial.

Page 13: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 13

Conjunto de datos el mismo tipo, organizado en 2 o mas columnas y 1 o mas renglones(matriz o tabla).

• ¡Arreglos de arreglos!

ARREGLOS BIDIMENCIONALES

Tipo [][]nombre_variable=new Tipo [Renglones][Columnas];

Page 14: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 14

• Ejemplo

int [][] mints=new int [3][4];

ARREGLOS BIDIMENCIONALES

1 2 3 4

91 92 93 94

2001 2002 2003 2004

Page 15: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 15

ARREGLOS BIDIMENCIONALES

1

2

3

4

2001

2002

2003

2004

91

92

92

94

Page 16: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 16

ARREGLOS BIDIMENCIONALES

1

2

3

2001

2002

91

92

92

94

Page 17: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 17

• Ejemplo

int [][] mints={{1,2,,3},{91,92,93,94},{2001,2002}}; ------------------------------------------------------------------int b[][]; b=new int[2][];b[0]=new int[5];b[1]=new int[3];

ARREGLOS BIDIMENCIONALES

Page 18: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 18

ARREGLOS MULTIDIMENCIONALES

3 Dimensiones.

int equipos[][][]=new int [4][4][4];4

4

4

Page 19: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 19

+= x += y; x = x + y;

-= x -= y; x = x - y;

*= X *= y; x = x * y;

/= x /= y; x = x +/y;

%= x %= y; x = x % y;

&= x &= y; x = x & y;

|= x |= y; x = x +|y;

^= x ^= y; x = x ^ y;

<<= x <<= y; x = x << y;

>>= x >>= y; x = x >> y;

>>>= x >>>= y; x = x >>> y;

OPERADORES DE ASIGNACION

Page 20: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 20

CONDICIONALES

if(<condición>){<sentencia>*

}

if(<condición>){<sentencia>*

}else{<sentencia>*

}

Page 21: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 21

CONDICIONALES

switch(<variable>){case<valor>:

<sentencia>*break;

case<valor>:<sentencia>*break;

default:<sentencia>*break;

}

Page 22: 1AUTOR: EDER CHAVEZ ACHA. public int wrong(){ int i; return i+5; } Las variables si se tienen que inicializar o marca el siguiente error al momento de

AUTOR: EDER CHAVEZ ACHA 22

for(<valor_inicial>;<condición>;<incremento>){<sentencia>*

}

while(<condición>){<sentencia>*

}

do{<sentencia>*

} while(<condición>);

CICLOS