Chapter Two: Fundamental Programming Structures in Java
Chapter Two: Fundamental Programming Structures in Java
Programming (OOP)
CHAPTER TWO
Fundamental Programming
Structures in Java
• Borland JBuilder
• NetBeans Open Source by Sun
• Sun ONE Studio by Sun MicroSystems
• Eclipse Open Source by IBM
Example:
type name
• One way is to declare a variable and then assign a value to it with two
statements:
int e; // declaring a variable
e = 5; // assigning a value to a variable
• Example:
• char firstLetterOfName = 'e' ;
• char myQuestion = '?' ;
String next()
Finds and returns the next complete token from this
scanner.
boolean nextBoolean()
Scans the next token of the input into a boolean value
and returns that value.
byte nextByte()
Scans the next token of the input as a byte.
double nextDouble()
Scans the next token of the input as a double.
float nextFloat()
Scans the next token of the input as a float
10/02/22 by: A.F 33
Input in java
int nextInt()
Scans the next token of the input as an int.
String nextLine()
Advances this scanner past the current line and
returns the input that was skipped.
long nextLong()
Scans the next token of the input as a long.
short nextShort()
Scans the next token of the input as a short.
char next().charAt() scans the next token of the input
as char.
10/02/22 by: A.F 34
Comments in java
Comments are ignored by the compiler but
are useful to other programmers.
Java programming language supports three
kinds of comments:
1. Single line comment // comment text
The compiler ignores everything
from // to the end of the line.
Operation Symbol
• Compound
expressions are Addition +
formed by
combining simple Subtraction -
expressions using
Multiplication *
arithmetic operators
Division /
Modulus %
Assignment operator
Used to assign value to a variable.
E.g.
int x=34;
Logical operators
&& for the logical “and”
operator and
|| for the logical “or” operator.
Relational operators
• == (equal),
• != (not equal)
• & (“and”)
• | (“or”)
• ^ (“xor”)
• Reference Variables
• Are used to store the address of an object
• Created from Classes
• E.g. Scanner input;
10/02/22 by: A.F 45
Conditional Statements
if Conditions
if condition
If…else condition
if …else if…else condition
statement2 {
} bonus = 100;
}
• Schematically:
• E.g.
Scanner s=new Scanner(System.in);
DataInputStream str=new DataInputStream
(System.in);
\b backspace
\t tab
\n newline
\" double quote
\' single quote
\\ backslash
• Syntax Errors
• Detected by the compiler
• Runtime Errors
• Causes the program to abort
• Logic Errors
• Produces incorrect result