java Features Version April 19, 2013 by Thorsten Kracht Contents 1 Introduction 1.1 Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2 2 Variables, Types 3 3 Input/Output 3.1 Standard I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Scanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 4 4 4 Operators 4.1 Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Increment/Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 5 5 5 Control Structures 5.1 If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 6 6 6 Standard Classes 6.1 Import . . . 6.2 Math . . . . 6.3 Random . . 6.4 String . . . 6.5 toString . . 6.6 Wrapper . . 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 7 7 7 8 8 9 Classes 7.1 General . . . . . . . . . . . . . . 7.2 Definition . . . . . . . . . . . . . 7.3 Classes in separate files . . . . . . 7.4 Overloading . . . . . . . . . . . . 7.5 Referenced objects can be changed 7.6 Encapsulation . . . . . . . . . . . 7.7 Inheritance . . . . . . . . . . . . 7.8 Abstract classes, polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 10 10 10 11 11 12 12 13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1 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 2 Chapter 2 Variables, Types These are the primitive data types: byte 8 bit, -128 short 16 bit, -32768 int 32 bit, -2E9 long 64 bit, -10e18 float 32 bit, -3.4E38 double 64 bit, -1.7e308 char 16 bit (!) boolean true, false) - +127 +32767 +2E9 +10e18 (long i = 3L;) +3.4e38 (float x = 1.23F;) +1.7e308 (double x = 123.0D;) 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) { ... } 3 Chapter 3 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 ); } } 4 Chapter 4 Operators 4.1 Boolean A A A A A A == B < B <= B > B >= B != B && || ! 4.2 Increment/Decrement i++; i--; 5 Chapter 5 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"); } } } 6 Chapter 6 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() 7 System.out.println("random " + rand.nextInt(1000) ); // numbers from 0 to 999 i = i - 1; } } } 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 { public static void main (String[] args) { Point a = new Point( 3, 4); System.out.println(" toString " + a.toString()); } } Result: $ javac toStringTest.java && java toStringTest toString java.awt.Point[x=3,y=4] 6.6 Wrapper 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); Chapter 7 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"); a.say(); } } The class classHello: // file classHello.java class classHello 10 { String msg; classHello( String a) { this.msg = a; 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: } // overloading a function public void say( String str) { System.out.println( "this is classHello.say: } } 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; " + this.msg); " + str); } } class classTest { 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 { String gender; public male( String name, int age) { super( name, age); this.gender = "male"; } } class classTest { public static void main (String[] args) { male a = new male( "Fritz", 30); System.out.println( a.name + " " + a.age + " " + a.gender); } } 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. Index abstract classes, 13 encapsulation, 12 inheritance, 12 overloading, 11 polymorphism, 13 toString, 8 14