3 Fundamentals Java
3 Fundamentals Java
Language
Fundamentals
• Java Overview
• Writing Your first Java Program
• Language Fundamentals
(Summary)
[email protected]
http:// www.sts.tu-harburg.de
Java: History
• Rapid development
• 1990: developed for use in electronic devices
• about 1995: gained popularity because of possible
use in the Internet
• by now: accepted as scalable programming language
for different purposes
1
Java is Designed For:
2
From Source Code to Execution
class InfIng01 {
public static int min(int a, int b) {
return a < b ? a : b;
} Java-
} Interpreter
UNIX-
System
Java-
Java- Byte- Interpreter
Source
Compiler code
PC
Java-
Interpreter
Internet-Browser
Macintosh
3
Naming Conventions
• Words run together, no underscores
• Intermediate words capitalized (getValue)
• Classes: first word capitalized (Cell)
• Methods and variables: first word lower case (value,
getValue)
• Constants: all caps with underscores to separate
words (like C).
4
The Keyword static
• Normally each object gets ist own data
• What if you want only one piece of data shared between all
objects of a class? (“class data“)
class StaticTest {
static int i = 47; See also
} System.out.println(...)
• What if you want a method that can be called for the class,
without an object? (“class method“)
class StaticFun {
static void incr() { StaticTest.i++; }
}
StaticFun.incr();
See also: main
5
Java Applications - Important Steps
• Create some source code within your text editor like the class
Cell
• Save it as .java - file, use the name of the (primary) class plus
the extension .java (e.g. Cell.java)
• Compile the source file:
javac Cell.java
• You get bytecode within a .class - files, for all the classes
included (Cell.class)
• Run your application by interpreting the primary class' bytecode
java Cell
• The Java interpreter looks for the main-method and executes it.
Data
6
Primitives
7
Booleans
• The primitive type used to express that something
can have exactly two different states is called
boolean.
• The two literals are true and false.
• Operations on booleans :
– Comparison: ==, !=
– Negation: !
– binary logic: &, |, ^
– short-circuit-evaluation logic: &&, ||
Ternary if-else
int a,b;
...
b = a >= 0 ? a : -a;
8
Controlling Program Flow
• Variable declaration and assignment
• Using operations on primitive data types
• Iteration
• Conditionals
Statements
• There are two general kinds of statement
– simple statements
– compound statements
• Simple statements end with a semicolon.
• Compound statements encompass several
statements and are enclosed in brackets.
• Compound statements are also called blocks.
• Blocks are important for scoping.
9
Variables
• Variables have to be declared before they are used.
int a;
• They can be assigned changing values according to their
declared type.
a = 3; a= a + 2; a = "Oh no!";
• Declaration and assignment can be done within a single
statement.
int b = 8;
• Some operations involve implicit assignments
b = ++a; a 6 b 6
Scoping
{ /* <-- Beginning of scope 1 */
int x = 12;
/* Only x available */
{ /* <-- Beginning of scope 2 */
int q = 96;
/* Cannot redefine x here! */
/* Both x & q available */
} /* <-- End of scope 2 */
/* Only x available */
/* q out of scope */
} /* <-- End of scope 1 */
10
if-else
int a;
...
if (a >= 0)
System.out.println("Already positive");
else {
a = -a;
System.out.println("Turned positive");
}
switch
char c;
...
switch(c) {
case 'a':
case 'o':
case 'u':
System.out.println("dark vowel");
break;
case 'e':
case 'i':
System.out.println("bright vowel");
break;
default:
System.out.println("no vowel");
}
11
What does switch do?
• Tests all case-conditions and executes the statement
behind the colon, if the condition is true.
• If a condition is true and the attached statement is
followed by a break, switch is finished.
• If no match occurs, the default-statement is
executed.
• switch only works on integral values like int or char.
for
int [] a = ...;
int sum = 0;
12
while
int i = 0;
while (i < 10) {
System.out.println(i);
++i;
}
13
import java.util.*;
14