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

Lecture 2 - Java Basics

The document discusses Java basics including variables, data types, operators, literals, and statements. It covers primitive data types like int, float, boolean, and char. It also discusses declaring and assigning variables, arithmetic and logical operators, and basic control flow statements like if/else and loops.

Uploaded by

Mtoi Tv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture 2 - Java Basics

The document discusses Java basics including variables, data types, operators, literals, and statements. It covers primitive data types like int, float, boolean, and char. It also discusses declaring and assigning variables, arithmetic and logical operators, and basic control flow statements like if/else and loops.

Uploaded by

Mtoi Tv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

University of Dar es Salaam

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;

when you declare it


University of Dar es Salaam
Variable Names/Identifiers
• Variable names in Java can start with a letter, an underscore (_), or a
dollar sign ($)
• They cannot start with a number.
• Java uses Unicode character set
• Can use accented characters
• Java is case sensitive
University of Dar es Salaam
Assigning Values to Variables
• Once a variable has been declared you can assign value
• Use the assignment operator

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.

byte values short values


University of Dar es Salaam
Exercise
• What is the data type of the following values?
University of Dar es Salaam

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

● Integer division result in an integer


● For integers, the result type of most operations is an
int or a long
University of Dar es Salaam
Order of Arithmetic Operations
University of Dar es Salaam
Exercise
• What are the resulting values for the following?
University of Dar es Salaam
Boolean Operators
University of Dar es Salaam
Operator Precedence
• Operations are performed in this order (from top to bottom)
University of Dar es Salaam
Exercise
• What are the resulting values for the following?
University of Dar es Salaam

Type conversion and casting


Combining different data types
University of Dar es Salaam
Type Conversion
• When Java performs arithmetic (+ - * / %) on two values, both values
must be the same data type.
• If your code contains 2 + 3, then Java sees that your adding int + int
and produces an int result (5)
University of Dar es Salaam
Automatic Type Conversion
• But, if a and b are different types, Java will try to promote one of the
values to make them the same type. This is called type conversion
• Type conversion may also occur when you call a method that expects
another type.
• Example: Math.sqrt(2)
University of Dar es Salaam
Automatic Type Conversion
• Takes place when two data types are automatically converted. This
happens when:
• The two data types are compatible
• When assigning a smaller data type value to a bigger data type.
• In Java, the numeric data types are compatible
• Java always promotes byte and short values to int
• But no automatic conversion is supported from numeric type to char
or boolean.
• This is done to prevent errors
• Also, char and boolean are not compatible with each other.
University of Dar es Salaam
Automatic Type Conversion
• If you do arithmetic on different data types, Java promotes one
argument to the type with the widest range.
University of Dar es Salaam
Type Casting
• Type casting is when you assign a value of one primitive data type to
another type.
• In Java, there are two types of casting:
• Widening Casting (automatically) - converting a smaller type to a larger type
size byte -> short -> char -> int -> long -> float -> double
• Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
• Widening casting is done automatically when passing a smaller size
type to a larger size type:
• Narrowing casting must be done manually by placing the type in
parentheses in front of the value
University of Dar es Salaam

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

Methods and parameters


Breaking down a program
University of Dar es Salaam
Methods
• Big programs are built out of small methods
• Methods can be individually developed, tested and reused
• The user of the method does not need to know how it work
University of Dar es Salaam
Methods
University of Dar es Salaam
Method Parameters
University of Dar es Salaam
Multiple Parameters
University of Dar es Salaam
Return Values
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

The scanner object will receive


input from this source

After initialization, a Scanner object


provides functionality to read
values of various types
University of Dar es Salaam
Writing Output: The Standard Output
• System.out is the predefined output device
• Refers to the monitor/screen of your computer
University of Dar es Salaam
Writing Output: printf()
• Java introduced printf() in Java 1.5
• Very similar to the C version
• The format string contains normal characters and a number of
specifiers
• Specifiers start with a percent sign (%)
• Value of the appropriate type must be provided for each specifier
University of Dar es Salaam
API
• The Scanner class you have seen is part of the Java API
• API: an interface for other programs to interact with a program without
having direct access to the internal data of the program
• Documentation: https://docs.oracle.com/en/java/javase/11/docs/api/
• For Java programmers, it is very important to refer to the API documentation
regularly
• The API consists of many classes
• You do not need to know all the classes
• You will learn some more classes in this course
University of Dar es Salaam
Java API

You might also like