java Features Version April 19, 2013 by Thorsten Kracht
Contents 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 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 - +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)... 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 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 == B A < B A <= B A > B A >= B A!= B &&! 4.2 Increment/Decrement i++; i--; 5
Chapter 5 Control Structures 5.1 If class iftest 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 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 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 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 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 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: " + this.msg); " + str); class classtest 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;
class classtest 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 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
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 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