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

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!

Impensando acerca de las referencias en Java

Fue hace ya algún tiempo que pase un rato discutiendo con algunos compañeros acerca de si existe o no el paso por referencia; el discurso fue mucho hacia que en Java el comportamiento, en el supuestamente pasamos por referencia un objeto y por valor los objetos primitivos creo mucha polémica. Para ubicarnos en contexto veamos el siguiente ejemplo. public static void main(String[] args) { int value = 10; changeValue(value); System.out.println("value = " + value); User user = new User(); Name name = new Name(); user.setName(name); name.setName("jsanca"); name.setLastName("XXX"); user.setPassword("123queso"); System.out.println("user: " + user.getName().getName() + ", " + user.getName().getLastName() + ", " + user.getPassword()); changeValue1(user); System.out.println("user: " + user.getName().getName() + ", " + user.getName().getLastName() + ", " + user.ge...