1 Introduction 2
1.1 Hello World . . . 2
2 Variables, Types 3 3 Input/Output 4 3.1 Standard I/O . . . 4
3.2 Scanner . . . 4
4 Operators 5 4.1 Boolean . . . 5
4.2 Increment/Decrement . . . 5
5 Control Structures 6 5.1 If . . . 6
5.2 Loops . . . 6
6 Standard Classes 7 6.1 Import . . . 7
6.2 Math . . . 7
6.3 Random . . . 7
6.4 String . . . 8
6.5 toString . . . 8
6.6 Wrapper . . . 9
7 Classes 10 7.1 General . . . 10
7.2 Definition . . . 10
7.3 Classes in separate files . . . 10
7.4 Overloading . . . 11
7.5 Referenced objects can be changed . . . 11
7.6 Encapsulation . . . 12
7.7 Inheritance . . . 12
7.8 Abstract classes, polymorphism . . . 13
Introduction
This note is intended to help people getting started with java. It contains some explanations and covers the topics by examples.
1.1 Hello World
//
// file Hello.java //
class Hello {
public static void main ( String[] args ) {
System.out.println("Hello World!");
} }
This program can be compiled and executed with:
javac Hello.java creates Hello.class java Hello
executes Hello.class
Variables, Types
These are the primitive data types:
byte 8 bit, -128 - +127 short 16 bit, -32768 - +32767 int 32 bit, -2E9 - +2E9
long 64 bit, -10e18 - +10e18 (long i = 3L;) float 32 bit, -3.4E38 - +3.4e38 (float x = 1.23F;) double 64 bit, -1.7e308 - +1.7e308 (double x = 123.0D;) char 16 bit (!)
boolean true, false)
The type names can be used as cast operators:
int i = (int) 1.5;
Primitive variables cannot be changes by a method.
Reference variables can have the value null.
String a = null;
if( a != null) { ...
}
Input/Output
3.1 Standard I/O
System.in System.out System,err
3.2 Scanner
import java.util.Scanner;
// loads java.io implicitly class echo
{
public static void main (String[] args) {
String line;
Scanner inp = new Scanner( System.in );
System.out.println("Enter:");
line = inp.nextLine();
System.out.println("You typed:" + line );
} }
Operators
4.1 Boolean
A == B A < B A <= B A > B A >= B A != B
&&
||
!
4.2 Increment/Decrement
i++;
i--;
Control Structures
5.1 If
class ifTest {
public static void main (String[] args) {
double x = 1.0;
if( x < 0) {
System.out.println(" x < 0");
}
else if (x == 0.) {
System.out.println(" x == 0");
} else {
System.out.println(" x > 0");
} } }
5.2 Loops
class ifTest {
public static void main (String[] args) {
double x = 1.0;
if( x < 0) {
System.out.println(" x < 0");
}
else if (x == 0.) {
System.out.println(" x == 0");
} else {
System.out.println(" x > 0");
} } }
Standard Classes
The examples in this section can be compiled and executed by:
$ javac mathTest.java && java mathTest cos 60.0 = 0.5000000000000001
The string ’mathTest’ has to be replaces by the actual class name.
6.1 Import
// import a single class import java.util.Random;
// import all classes of a package import java.util.*;
6.2 Math
class mathTest {
public static void main (String[] args) {
double degree = 60.;
System.out.println(" cos " + degree + " = " + Math.cos( Math.toRadians( degree)));
} }
$ javac mathTest.java && java mathTest cos 60.0 = 0.5000000000000001
6.3 Random
import java.util.Random;
public class randTest {
public static void main ( String[] args ) {
Random rand = new Random();
int i = 10;
while ( i > 0 ) {
// other methods: nextFloat(), nextDouble(), nextGaussian()
6.4 String
The string class (package java.lang.String) contains these functions (and others):
public char charAt( int index ) public String concat( String str )
public boolean endsWith( String suffix ) public boolean equals( Object anObject )
str1.equals( str2)
public boolean equalsIgnoreCase( String anotherString ) public int indexOf( int ch )
public int indexOf( String str ) public int length()
public boolean startsWith( String prefix ) public String substring( int beginIndex )
public String substring( int beginIndex, int endIndex ) public String toLowerCase()
public String toUpperCase() public String trim()
Example:
//
class Beispiel {
public static void main ( String[] args ) {
java.lang.String str;
str = new String( "ein test string");
String str1 = new String( "noch ein string");;
System.out.println( "string = " + str + " length " + str.length());
System.out.println( "upperCase-str1 = " + str1.toUpperCase());
} }
6.5 toString
Produces a string representation of an object.
import java.awt.*;
class toStringTest {
The wrapper classes, package java.lang, create an object.
primitive type wrapper type
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
Example:
Integer i = new Integer(123);
Classes
7.1 General
• static members belong a class not the opject.
• private visible from within the object.
• public visible from outside the object.
• If a member is neither declared private or public, it is public to the package.
7.2 Definition
class Hello {
void say() {
System.out.println( "this is Hello.say");
} }
class classTest {
public static void main (String[] args) {
Hello a = new Hello();
a.say();
} }
7.3 Classes in separate files
Here is a main programm that uses code which is stored in a different file:
// file classTest.java class classTest
{
public static void main (String[] args) {
classHello a = new classHello( "moin");
System.out.println( "this is the Constructor, classHello.Hello");
}
void say() {
System.out.println( "this is classHello.say: " + this.msg);
} }
This command compiles both files and executes main().
$ javac classTest.java classHello.java && java classTest
7.4 Overloading
class classHello {
private String msg;
classHello( String a) {
this.msg = a;
}
public void say() {
System.out.println( "this is classHello.say: " + this.msg);
}
// overloading a function public void say( String str) {
System.out.println( "this is classHello.say: " + str);
} }
class classTest {
public static void main (String[] args) {
classHello a = new classHello( "Moin");
a.say();
String b = new String( "Guten Tag");
a.say( b);
} }
7.5 Referenced objects can be changed
class Value {
public int i = 1;
}
class Klasse {
void func( Value obj) { obj.i = 11;
return;
public static void main (String[] args) {
Value b = new Value();
Klasse a = new Klasse();
System.out.println( "Vorher " + b.i);
a.func( b);
System.out.println( "Nachher " + b.i);
} }
7.6 Encapsulation
class Value {
private int i = 1;
public void setValue( int argin){
i = argin;
}
public int getValue( ){
return i;
} }
class classTest {
public static void main (String[] args) {
Value b = new Value();
System.out.println( "Vorher " + b.getValue());
b.setValue( 2);
System.out.println( "Nachher " + b.getValue());
} }
7.7 Inheritance
class Human {
int age;
String name;
public Human( String name, int age){
this.name = name;
this.age = age;
} }
class male extends Human {
} }
7.8 Abstract classes, polymorphism
An abstract class is never instanciated. It servers the purpose of being a superclass for other classes.
abstract class Animal {
String name;
public Animal( String name){
this.name = name;
}
// abstract methods have to be implemented by subclasses public abstract String sound();
}
class dog extends Animal { public dog( String name) {
super( name);
}
public String sound() { return "wau";
} }
class cat extends Animal { public cat( String name) {
super( name);
}
public String sound() { return "miau";
} }
class classTest {
public static void main (String[] args) {
Animal a = new dog( "Rex");
Animal b = new cat( "Miezzi");
Animal c;
c = a;
System.out.println( c.name + " says " + c.sound());
c = b;
System.out.println( c.name + " says " + c.sound());
} }
Polymorphism: depending which type of animal is referenced by c, the sound() method produces a different result.
abstract classes, 13 encapsulation, 12 inheritance, 12 overloading, 11 polymorphism, 13 toString, 8