Skip to content

Instantly share code, notes, and snippets.

@bitsmuggler
Last active March 2, 2016 15:49
Show Gist options
  • Save bitsmuggler/3ce537d2478dd5880d12 to your computer and use it in GitHub Desktop.
Save bitsmuggler/3ce537d2478dd5880d12 to your computer and use it in GitHub Desktop.
Einstieg Java-Programmierung - Lösungen zum Teil Kontrolle zur Theorie

Aufgabe 1

// Narrowing Primitive Conversion
long lVar = 12000;
int iVar = (int)lVar;

Java Language Specification - Chapter 5. Conversions and Promotions

Aufgabe 2

Tabelle

Wert byte short int long float double String boolean char
2 x x x x x x x
'C' x x x x x x x
1000.00 x
0 x x x x x x x
-56 x x x x x x
-12.0 x
-435 x
true x
"abc" x
-12e50 x
"12.5 x
5e-5 x
56.0f x x

Code fürs Testing

//Value = 2
byte a = 2;
short b = 2;
int c = 2;
long d = 2;
float e = 2;
double f = 2;
String g = 2;  //doesn't work
boolean h = 2; //doesn't work
char i = 2;

//Value = 'c'
byte a = 'c';
short b = 'c';
int c = 'c';
long d = 'c';
float e = 'c';
double f = 'c';
String g = 'c';  //doesn't work
boolean h = 'c'; //doesn't work
char i = 'c';


//Value = 1000.00
byte a = 1000.00;       //doesn't work
short b = 1000.00;      //doesn't work
int c = 1000.00;        //doesn't work
long d = 1000.00;       //doesn't work
float e = 1000.00;      //doesn't work
double f = 1000.00;
String g = 1000.00;     //doesn't work
boolean h = 1000.00;    //doesn't work
char i = 1000.00;       //doesn't work


//Value = 0
byte a = 0;
short b = 0;
int c = 0;
long d = 0;
float e = 0;
double f = 0;
String g = 0;     //doesn't work
boolean h = 0;    //doesn't work
char i = 0;     


//Value = -56
byte a = -56;
short b = -56;
int c = -56;
long d = -56;
float e = -56;
double f = -56;
String g = -56;     //doesn't work
boolean h = -56;    //doesn't work
char i = -56;       //doesn't work


//Value = -12.0
byte a = -12.0;     //doesn't work
short b = -12.0;    //doesn't work
int c = -12.0;      //doesn't work
long d = -12.0;     //doesn't work
float e = -12.0;    //doesn't work
double f = -12.0;
String g = -12.0;     //doesn't work
boolean h = -12.0;    //doesn't work
char i = -12.0;       //doesn't work


//Value = -435.12
byte a = -435.12;     //doesn't work
short b = -435.12;    //doesn't work
int c = -435.12;      //doesn't work
long d = -435.12;     //doesn't work
float e = -435.12;    //doesn't work
double f = -435.12;
String g = -435.12;     //doesn't work
boolean h = -435.12;    //doesn't work
char i = -435.12;       //doesn't work

//Value = true
byte a = true;     //doesn't work
short b = true;    //doesn't work
int c = true;      //doesn't work
long d = true;     //doesn't work
float e = true;    //doesn't work
double f = true;    //doesn't work
String g = true;     //doesn't work
boolean h = true;
char i = true;       //doesn't work

//Value = "abc"
byte a = "abc";     //doesn't work
short b = "abc";    //doesn't work
int c = "abc";      //doesn't work
long d = "abc";     //doesn't work
float e = "abc";    //doesn't work
double f = "abc";    //doesn't work
String g = "abc";
boolean h = "abc";      //doesn't work
char i = "abc";       //doesn't work

//Value = -12e50
byte a = -12e50;     //doesn't work
short b = -12e50;    //doesn't work
int c = -12e50;      //doesn't work
long d = -12e50;     //doesn't work
float e = -12e50;    //doesn't work
double f = -12e50;
String g = -12e50;      //doesn't work
boolean h = -12e50;      //doesn't work
char i = -12e50;       //doesn't work

//Value = "12.5"
byte a = "12.5";     //doesn't work
short b = "12.5";    //doesn't work
int c = "12.5";      //doesn't work
long d = "12.5";     //doesn't work
float e = "12.5";    //doesn't work
double f = "12.5";    //doesn't work
String g = "12.5";
boolean h = "12.5";      //doesn't work
char i = "12.5";       //doesn't work

//Value = 5e-5
byte a = 5e-5;     //doesn't work
short b = 5e-5;    //doesn't work
int c = 5e-5;      //doesn't work
long d = 5e-5;     //doesn't work
float e = 5e-5;    //doesn't work
double f = 5e-5;
String g = 5e-5;    //doesn't work
boolean h = 5e-5;      //doesn't work
char i = 5e-5;       //doesn't work

//Value = 56.0f
byte a = 56.0f;     //doesn't work
short b = 56.0f;    //doesn't work
int c = 56.0f;      //doesn't work
long d = 56.0f;     //doesn't work
float e = 56.0f;
double f = 56.0f;
String g = 56.0f;    //doesn't work
boolean h = 56.0f;   //doesn't work
char i = 56.0f;      //doesn't work

Aufgabe 3

Die Variablendeklaration besteht aus Deklaration und Initialisierung.

Aufgabe 4

Es gibt unterschiedliche Typen von Variabeln:

  • Lokale Variabeln in Funktionen
void fun() { int n; … }

** Parameter von Methoden sind auch lokale Variabeln; sie existieren einmal pro Aufruf der Methode.

int factorial(int n) { … }
  • Objektattribute Existieren einmal pro Objekt
class Name { String vorname; … }
  • Klassenattribute Existieren einaml pro Klasse (unabhängig von der Anzahl Instanzen)
class System { static PrintStream out; … }

Konstanten (Kompilierzeit-Konstanten) werden im gegensatz zu Variabeln mit dem Schlüsselwort final als unveränderbar deklariert, etwa so:

private static final int ZEITFAKTOR = 3600;.

Durch das Schlüsselwort final kann die Klassenvariable nach dem Initialisieren nicht mehr verändert werden. Ihr kann im weiteren Programmverlauf kein neuer Wert zugewiesen werden. Zur besseren Lesbarkeit werden Konstantennamen in Java groß geschrieben.

Aufgabe 5

Unter einem Array in Java versteht man ein Feld oder Container, das in der Lage ist, mehrere Objekte vom gleichen Typ aufzunehmen und zu verwalten. Dabei wird in Java das Array als eine spezielle Klasse repräsentiert, was unter anderem mit sich bringt, dass man auf spezielle Methoden und Operationen bei Arrays zurückgreifen kann. Der Umgang mit Arrays mag gerade am Anfang etwas schwerer sein und birgt viele Fehlerquellen, nach und nach wird man das System das hinter den Arrays steht aber gut nachvollziehen können.

Ein Array kann auf unterschiedlichste Weisen deklariert oder initialisiert werden. Wichtig ist immer, dass man dabei an die eckigen Klammeroperatoren denkt, da diese ein Array auszeichnen. Anbei die Syntax für die Deklaration eines Arrays:

Typ[] Name = new Typ[Anzahl];
Typ Name[] = new Typ[Anzahl];

Aufgabe 6

Möchte man nun also ein Array das insgesamt 5-int Werte verwaltet, dann lautet die Deklaration des Arrays:

int[] meinArray = new int[5];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment