0% found this document useful (0 votes)
36 views

3 Fundamentals Java

This document provides an overview of the Java programming language and its fundamentals. It discusses Java's history and design, how Java code is compiled and executed, how to write a simple Java program, basic syntax and naming conventions, and primitive data types in Java like booleans, integers, and floating-point numbers. It also covers important concepts like how static methods and variables work, importing other code libraries, and the process of compiling and running a Java application.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

3 Fundamentals Java

This document provides an overview of the Java programming language and its fundamentals. It discusses Java's history and design, how Java code is compiled and executed, how to write a simple Java program, basic syntax and naming conventions, and primitive data types in Java like booleans, integers, and floating-point numbers. It also covers important concepts like how static methods and variables work, importing other code libraries, and the process of compiling and running a Java application.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Overview and

Language
Fundamentals

• Java Overview
• Writing Your first Java Program
• Language Fundamentals
(Summary)

Claudia Niederée, Joachim W. Schmidt


Software Systems Institute
OOAD 1999/2000

[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

OOAD99/00 -STS-Overview and Language Fundamentals


2

1
Java is Designed For:

• A fresh start (no backwards compatibility)


• Pure OOP: C++ syntax, Smalltalk style
• Improvements over C++, near guarantee that you
can’t run a bad program
• Internet programming : probably can’t create viruses,
programmability at the browser end, easier
client/server programming
• Larger, more complex programs; smaller teams, less
time
• There is the speed issue

OOAD99/00 -STS-Overview and Language Fundamentals


3

How does Java generally work?


• Compilation creates bytecode
• Platform-independent
• Interpreted
• Bytecode is either:
– interpreted statement by statement => "slow"
– once compiled to a program in machine language
=> "fast"

OOAD99/00 -STS-Overview and Language Fundamentals


4

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

Java- JIT*- Machine-


Interpreter Compiler code PC
for PC

* JIT = Just In Time


OOAD99/00 -STS-Overview and Language Fundamentals
5

Writing Your First Java Class


class Cell {
private int value = 0; Data member

void setValue(int newValue){


this.value = newValue; Class
} definition
int getValue( ) {
Methods
return this.value;
}
...
}

OOAD99/00 -STS-Overview and Language Fundamentals


6

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).

OOAD99/00 -STS-Overview and Language Fundamentals


7

Writing a Java Program


import java.util.*;
class Cell {
int value = ...
void printValue(){
System.out.println(“Date: “ + new Date());
System.out.println(“Value: “+ this.value);
}
public static void main(String [] args){
Cell cell1 = new Cell();
cell1.setValue(4);
cell1.printValue();
}
}

OOAD99/00 -STS-Overview and Language Fundamentals


8

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

OOAD99/00 -STS-Overview and Language Fundamentals


9

Using Other Components


• Bring in a library of components using import
keyword
• Can specify specific element in library:
import utility.MyTools;
• Can specify entire library:
import java.util.*;

OOAD99/00 -STS-Overview and Language Fundamentals


10

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.

OOAD99/00 -STS-Overview and Language Fundamentals


11

Data

• In Java, almost everything is an object.


• Objects are described by reference types like the
type String, classes or arrays
• Some simple data types, however, are frequently
used as programming basis.
• These so-called primitive types are built-in types,
implying a behaviour different from reference types

OOAD99/00 -STS-Overview and Language Fundamentals


12

6
Primitives

• Built-in types: not object handles, but variables on the


stack like C.
boolean, char (Unicode), byte, short, int, long,
float, double
• Consequences for bindings and comparisons.
• Size of each data type is machine independent.

OOAD99/00 -STS-Overview and Language Fundamentals


13

Binding and Comparing Primitives


• Binding a variable to a primitive value means copying this value
into the variable's storage cell.
int a = 100;
int b = 75; a 100 b 75
• Assigning a new value to an existing variable means replacing
the original value by a copy of the new value.
a = b;
• A comparison is done by comparing the bits of the copied
values. a 75 b 75
a == b;

OOAD99/00 -STS-Overview and Language Fundamentals


14

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: &&, ||

OOAD99/00 -STS-Overview and Language Fundamentals


15

Ternary if-else
int a,b;
...
b = a >= 0 ? a : -a;

• Does the same as


int a,b;
...
if (a >= 0) b = a; else b = -a;

• A ternary if-else always results in a value


• Can also be used for side-effects
• Shouldn't be used too often for the sake of readable code

OOAD99/00 -STS-Overview and Language Fundamentals


16

8
Controlling Program Flow
• Variable declaration and assignment
• Using operations on primitive data types
• Iteration
• Conditionals

OOAD99/00 -STS-Overview and Language Fundamentals


17

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.

OOAD99/00 -STS-Overview and Language Fundamentals


18

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

• Variables that are used but not initialized cause compile-time


errors.

OOAD99/00 -STS-Overview and Language Fundamentals


19

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 */

OOAD99/00 -STS-Overview and Language Fundamentals


20

10
if-else
int a;
...
if (a >= 0)
System.out.println("Already positive");
else {
a = -a;
System.out.println("Turned positive");
}

• The statements can either be simple (terminated by


semicolon) or compound in braces.
• else is optional

OOAD99/00 -STS-Overview and Language Fundamentals


21

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");
}

OOAD99/00 -STS-Overview and Language Fundamentals


22

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.

OOAD99/00 -STS-Overview and Language Fundamentals


23

for
int [] a = ...;
int sum = 0;

for (int i = 0; i < 10; i ++) {


sum = sum + a[i];
}

• performs intialization before first iteration, conditional testing at


the beginning of each iteration and some form of stepping at the
end of each iteration
• all three parts are optional

OOAD99/00 -STS-Overview and Language Fundamentals


24

12
while
int i = 0;
while (i < 10) {
System.out.println(i);
++i;
}

• Tests a condition and executes the loop-statements as long as


the condition is true.

OOAD99/00 -STS-Overview and Language Fundamentals


25

Nobody is perfect ...


• The following program code contains the typical
syntax errors a programmer has to cope with.
• Some appear time and again : - (
• Training to find them is useful ...

OOAD99/00 -STS-Overview and Language Fundamentals


26

13
import java.util.*;

public class goodMorning {


public void main(String[] args) {
int counter;
System.out.println(new Date())
System.out.println("Could do some sports...");
for[int i = 1; i <= 20; i++] {
if (i % 3 = 0) {System.out.println("Too tired!");continue;};
counter = (i % 2 != 0 ? counter + i : counter);
if (i % 2 == 0) System.out.print("Bowing my knees, ");
else System.out.print("Sitting up, ");
switch(i)
case 1: System.out.println("once!"); break;
case 2: System.out.println("twice!"); break;
default: System.out.println(i + " times!");
};
System.out.println("Wow, I am great!");
System.out.println("I did " + counter + " sit-ups!");
};
}
OOAD99/00 -STS-Overview and Language Fundamentals
27

14

You might also like