Lecture 2 - Java Basics
Lecture 2 - Java Basics
CS 234: Object-Oriented
Programming in Java
Java Basics
Variables, Types, Operations, Statements
Aron Kondoro
University of Dar es Salaam
Java Basics
Variables, Types, Operations, Statements
University of Dar es Salaam
Java Hello World
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
University of Dar es Salaam
Variables
• Locations in memory in which values
can be stored
• Name, Value, Type
• Must be declared before can be used
• Java has three kinds of variables
• instance variables
• Class variables
• local variables
• Java does not have global variables
University of Dar es Salaam
Declaring Variables
• To use any variable in a Java program, you must int myAge;
String myName;
first declare it boolean isTired;
• Variable name and type
• Variable declarations consist of a type and a int x, y, z;
variable name String firstName, LastName;
• Variable definitions can go anywhere in a method
definition int myAge, mySize, numShoes
= 28;
• You can string together variable names with the String myName = “Laura”;
same type boolean isTired = true;
• You can also give each variable an initial value int a = 4, b = 5, c = 6;
size = 14;
tooMuchCaffiene = true;
University of Dar es Salaam
Java Data Types
University of Dar es Salaam
Essential information about a data type
• What values can a data type store?
• What operations can we perform on a data type?
• For example
University of Dar es Salaam
Primitive Data Types
• Built into the system
• A primitive data type has
only a value, such as a
number.
• Are machine-independent
University of Dar es Salaam
int
• Stores whole numbers, positive or negative without decimals
Operations
Values
Special Values
University of Dar es Salaam
Rules for int operations
University of Dar es Salaam
Floating point types
• Represent numbers with a fractional part (non-integer values)
• Java has two data types for storing non-integer values
• float
• double
University of Dar es Salaam
Floating Point Data
• Use double for most applications (more accurate).
• Use float where 6-decimal digits are enough, or you need to optimise
space/performance.
University of Dar es Salaam
double
• Java uses the IEEE floating point standard
• Any number written with “.” or exponential is automatic of type
double (not float)
• If you do +, -, *, /, with int and double, the result is a double. The int
value is promoted to a double first
• *, /, and % are always done before + and -
University of Dar es Salaam
double special values
• There are 3 special values
• +Infinity, -Infinity, and NaN (not a number)
University of Dar es Salaam
Rules for numeric values
• Java has rules for how it interprets numerical values
University of Dar es Salaam
Boolean Data Type
• Java provides a Boolean data type
• Has two values: true or false
• Is used in if, while and for statements
Values Operations
University of Dar es Salaam
char
• The char data type is for character data
• Java uses 2-byte Unicode for character data
• To hold all the world’s alphabet
• Unicode: http://www.unicode.org
• You can also use char to hold special values
Values
University of Dar es Salaam
Useful char methods
University of Dar es Salaam
byte and short
• byte and short are for integer data and input/output
• byte is used for low-level input, holding character codes (as 1 byte),
and groups of "flag" bits
• byte and short are not used for arithmetic.
• Java promotes all arithmetic to the "int" data type.
Operators
Manipulating data types
University of Dar es Salaam
Operators
• Symbols that perform simple computations
• Include
• Arithmetic
• Assignments
• Increment and decrement
• Logical operations
University of Dar es Salaam
Arithmetic Operators
Literals
University of Dar es Salaam
Java Literals
• Constant values that appear directly in the program.
• Can be assigned directly to a variable.
University of Dar es Salaam
Number Literals
• Integer literals
University of Dar es Salaam
Statements
University of Dar es Salaam
Statement
• Simplest thing you can do in Java int i = 1;
import
• Forms a single Java operation java.awt.Font;
System.out.println(“
• Sometimes return values This motorcycle is a
• Are called expressions “ + color + “ “ +
make);
• Must end with a semicolon m.engineState =
• Statements can also be grouped to form a block true;
int j = 1 + 2;
• Use {}
University of Dar es Salaam
Selection statements
Conditionals
University of Dar es Salaam
Selection Statements
University of Dar es Salaam
Comparison operators
University of Dar es Salaam
Boolean operators
University of Dar es Salaam
Repetition statements
Loops
University of Dar es Salaam
Repetition Statements (Loops)
University of Dar es Salaam
Branching Statements
University of Dar es Salaam
Embedded Loops
University of Dar es Salaam
Basic Input/Output
Interacting with the outside world
University of Dar es Salaam
Reading input: The Scanner Class
Declares a variable sc of Scanner type