/*************************************************************************/ /* Prof. Dr. Carsten Vogt */ /* TH Koeln, Fakultaet IME */ /* http://www.nt.th-koeln.de/vogt */ /* */ /* Das Programm demonstriert den Umgang mit den elementaren Zahlentypen. */ /*************************************************************************/ import java.io.*; public class Zahlen { /* Deklaration einer Zahlenkonstanten (static, also auf Class-Ebene!) */ static final double pi = 3.14159265; public static void main(String args[]) { /* Deklaration von Zahlenvariablen der verschiedenen Typen mit Initialisierung */ byte byteZahl = (byte) -128; // explizite Umwandlung nach 'byte', da Ganzzahlkonstanten vom Typ int sind short shortZahl = (short) -0b101; // binäre Konstante int intZahl = 0x1A; // hexadezimale Konstante long longZahl = 0777; // oktale Konstante float floatZahl = (float) 10.75; double doubleZahl = -1.7e-3; // entspricht "1.7 mal 10 hoch -3" = 0.0017 /* Ausgabe der Konstanten- und Variablenwerte nach Initialisierung */ System.out.println(); System.out.println("pi = "+pi); System.out.println("byteZahl = "+byteZahl); System.out.println("shortZahl = "+shortZahl); System.out.println("intZahl = "+intZahl); System.out.println("longZahl = "+longZahl); System.out.println("floatZahl = "+floatZahl); System.out.println("doubleZahl = "+doubleZahl); System.out.println(); /* Division von ganzen Zahlen und von Gleitkommazahlen */ System.out.println("7 / 3 = " + (7/3)); System.out.println("7.0 / 2 = " + (7.0/3)); System.out.println(); /* Modulo-Operator % */ System.out.println("7 % 3 = " + (7%3)); System.out.println("-7 % 3 = " + (-7%3)); System.out.println("7 % -3 = " + (7%-3)); System.out.println("-7 % -3 = " + (-7%-3)); System.out.println(); /* Ueberlauf bei Berechnung */ System.out.print("Das Quadrat von "+intZahl+" ist: "); intZahl = intZahl * intZahl; System.out.println(intZahl); System.out.println(); /* Shifts */ System.out.println("26 >> 2 = " + (26>>2)); System.out.println("-8 >> 2 = " + (-8>>2)); System.out.println("6 << 2 = " + (6<<2)); System.out.println("-2 << 2 = " + (-2<<2)); System.out.println(); /* Typumwandlungen */ System.out.println("7/3 = " + (7/3)); System.out.println("(float) 7 / 3 = " + ((float)7/3)); System.out.println("7/(float)3 = " + (7/(float)3)); System.out.println("(float) (7/3) = " + ((float)(7/3))); System.out.println(); /* Anwendung von Standardfunktionen aus der Klasse Math */ System.out.println("sqrt(9) = "+Math.sqrt(9)); System.out.println("pow(3,4) = "+Math.pow(3,4)); System.out.println(); } }