Ir al contenido principal

Operadores en Java

En esta entrega vamos a ver algunos de los operadores con los cuales cuenta Java.

Aritméticos

Los operador aritméticos nos sirven básicamente para realizar operaciones matemáticas, del tipo de suma, resta, etc. Funcionan en todos los tipos numéricos que vimos anteriormente, como enteros, long, etc.
  • Suma
     int i = a + c + 1; i += 2;
  • Resta
     int i = a – c; i -= 2;
  • División
      int i = 4 / 2; i /= 2;
  • Multiplicación
      int i = 4 * 2; i *= 2;
  • Incremento
      ++i or i++.
  • Residuo
      int i = 4 % 2; i %= 2;
  • Decremento
      --i or i--

A continuación un ejemplo:
public static void doArithmetic() {

int i = 0;
int step = 0;

System.out.println("Initial value: " + ++step + " = " + i);

i = 2 + 2;

System.out.println(++step + " = " + i);

i += 2;

System.out.println(++step + " = " + i);

i = i - 4;

System.out.println(++step + " = " + i);

i -= 1;

System.out.println(++step + " = " + i);

i++;

System.out.println(++step + " = " + i);

i *= 2;

System.out.println(++step + " = " + i);

i += 1;
System.out.println(++step + " = " + (i % 2));
System.out.println(++step + " = " + (i / 2));

--i;

System.out.println(++step + " = " + (i % 2));
}

Operadores lógicos y algunos ejemplo de instrucciones condicionales

&, |, ^, >>, <<, !

De orden izquierdo a derecho; and,or y xor logicos, operador de shift y negación.

==, >, >=, =<, <, != , ?:

De orden izquierdo a derecho; comparador igual, mayor, mayor igual, menor igual, menor, diferente, operador ternario.

Veamos un ejemplo:

public static void doLogicalAndConditional() {


int i = 3;

int x = 3;


int j = i <<>

int m = (int) Math.pow(2.0, x) * i;


System.out.println("j = " + j + ", m = " + m);


i = m;

j = i >> x; // i * 2^x

m = i / (int) Math.pow(2.0, x);


System.out.println("j = " + j + ", m = " + m);


if (i == 3) {


System.out.println("I is equal to 3");

}


if (m != 3) {


System.out.println("I is diff to 3");

}


int a = 1;

int b = 0;

int c = 1 & 0;


System.out.println("c = " + c);


c = 1 | 0;


System.out.println("c = " + c);

c = 0 ^ 0;


System.out.println("c = " + c);


c = 1 ^ 0;


System.out.println("c = " + c);

if (!(c == 0)) {

// ... do something.

}

}


Comentarios

Entradas más populares de este blog

Pasos para remover Postgresql 8.3 en MAC OS

Tomado de: http://forums.enterprisedb.com/posts/list/1437.page In Mac OSX: (Assuming Default Locations) Via uninstaller: 1) In the installation directory, there will be a uninstall-postgresql.app file will be there, executing (double clicking) that will uninstall the postgresql installation. Manual Uninstallation: 1) Stop the server sudo /sbin/SystemStarter stop postgresql-8.3 2) Remove menu shortcuts: sudo rm -rf /Applications/PostgreSQL 8.3 3) Remove the ini file sudo rm -rf /etc/postgres-reg.ini 4) Removing Startup Items sudo rm -rf /Library/StartupItems/postgresql-8.3 5) Remove the data and installed files sudo rm -rf /Library/PostgreSQL/8.3 6) Delete the user postgres sudo dscl . delete /users/postgres

Configura tu MMS - ICE CELULAR

Hola gente, Me encontre esta para configurar en tu aparato el servicio del ICE, para enviar mensajes con Imagen: http://www.grupoice.com/esp/serv/per/cel/catal_sm/men_multi/guia.htm En mi caso yo tengo un K550i, aunque no vi mi modelo dentro de la lista que dan en el PDF, me guié con el modelo mas similar y funko. Como dato curioso si envía un mensaje a un celular sin soporte o conflagración MMS, el ICE manda un link y credenciales para ver el mensaje en: http://mms.grupoice.com/m/C?url=mmc1 upd (edit): Para que funcione los MMS debe tener instalado Internet en su Celular; el costo por tarifa fija, es de 3500 colones por mes. update (dic 29 2010) Me encontré estos otros links con mas info: http://portal.grupoice.com/wps/wcm/connect/web+content/Esp/CatTelecom/movil/Adicionales/Kolbiemail/como/ http://www.taringa.net/posts/celulares/7987334/Kolbi-Prepago-todas-la-configuraciones.html También puedes ver mas, en el thread del facebook de Kolbi http://www.facebook.com/photo.php?fbid=1720708...

Enrique Bunbury - 3 de Octubre 2009 - Heredia CR

Via periodico la nacion, me entero de este conciertazo: http://www.nacion.com/viva/2009/julio/29/viva2042768.html Ah esperar mas detalles y la venta de la entradas, "Para solo si me perdonas ..." EDIT ..... Via http://www.enriquebunbury.com/hellville.aspx (gracias a andres por su post) venta anticipada: Entradas a la venta a partir del 13 de agosto en: www.costaricaticket.com Por ahi estare anunciando, el 13 si las estan vendiendo!