Class 3 - 27 February To 03 March 2023
Class 3 - 27 February To 03 March 2023
Oriented
Orogramming
(OOP): JAVA
CMPG 211
Week 2
27 February 2023 to 03 March 2023
Lecturer: Dr V Malele
Office: G15
Expectations
• After engaging with the materials and activities in this study unit you
should be able to:
• Understand difference between primitive and non-primitive data types
• Use Data Types in BlueJ
• Use the Java I/O package
• Understand the use of Java Keywords
• Oracle Document
• https://www.oracle.com/jav
a/technologies/downloads/
• https://docs.oracle.com/jav
ase/8/docs/technotes/guide
s/desc_jdk_structure.html
Variables
Java Variables
• Instance Variables (Non-Static Fields)
→Technically speaking, objects store their individual states in "non-static fields", that is,
fields declared without the static keyword.
→Non-static fields are also known as instance variables because their values are unique
to each instance of a class (to each object, in other words); the currentSpeed of one
bicycle is independent from the currentSpeed of another.
• Class Variables (Static Fields)
→A class variable is any field declared with the static modifier; this tells the compiler
that there is exactly one copy of this variable in existence, regardless of how many
times the class has been instantiated.
→A field defining the number of gears for a particular kind of bicycle could be marked as
static since conceptually the same number of gears will apply to all instances.
→The code static int numGears = 6; would create such a static field.
→Additionally, the keyword final could be added to indicate that the number of gears
will never change.
Java Variables
• Local Variables
→ Similar to how an object stores its state in fields, a method will often store its
temporary state in local variables.
→ local variables are only visible to the methods in which they are declared; they are not
accessible from the rest of the class.
→ The syntax for declaring a local variable is similar to declaring a field (for example, int
count = 0;).
→ There is no special keyword designating a variable as local; that determination comes
entirely from the location in which the variable is declared — which is between the
opening and closing braces of a method.
• Parameters
• Recall that the signature for the main method of examples so far is:
public static void main(String[] args).
• Here, the args variable is the parameter to this method.
• The important thing to remember is that parameters are always classified as "variables"
not "fields".
Declaring a Variable
• In Java, the type of variable is checked at compile time.
• This is known as static typing.
• It has the advantage of catching the errors at compile time rather than at execution time.
• Variables must be declared with the appropriate data type or the program will not
compile.
• To syntax for declare a variable in Java:
(The data type) (The variable name) = (The value)
NB: Without brackets
• There are several kinds of variables:
• Member variables in a class—these are called fields.
• Variables in a method or block of code—these are called local variables.
• Variables in method declarations—these are called parameters
DIY: Variable Declaration
• Numbers and Texts
• In programming, we can directly use some fixed
values in our program. These fixed values are
called literals.
• Let's take a look at commonly used literals in Java.
1. Integers (int)
Integers are numbers without decimal parts. For
example, 5, -11, 0, 12, etc. For now, let's refer to integers
as simply int.
2. Floating-point Numbers (double)
Floating-point numbers contain decimal parts. For
example, 2.5, 6.76, 0.0, -9.45, etc. For now, let's refer to
floating-point numbers as double.
3. String (text)
In Java, texts wrapped inside double quotation marks are
called Strings.
DIY: Variable Declaration
System.out.println();
import java.io.*; System.out.prin
tln(num);
class PrintLN { System.out.prin
public static void tln(ch);
main(String[] args) System.out.prin
{ tln(str);
// Declaring System.out.prin
different datatypes tln(d);
int num = 10; System.out.prin
char ch = 'G'; tln(f);
String str = System.out.prin
“Zekethe"; tln(bool);
double d = 10.2; System.out.prin
float f = 13.5f; tln("Hello");
boolean bool = true; }
}
• See pages 573 to 575 of
Data Types
• Note:
• Every class should be called or
invoked from the Main Class for it
to give output.
• Upon creating an object of class
Demo, we have invoked two
functions with some parameters to
perform addition and subtraction
and the result will be then
displayed.
Strings
• Strings in Java are designed in such a way that they can hold a • DYI Example
sequence of characters in a single variable, unlike character public class Main {
arrays where there are separate char entities. public static void main(String[] args)
• There is no need to end strings in Java with the null character {
which is compulsory in the case of C/C++. String myStr1 = "Vusi";
String myStr2 = "Malele";
• Syntax for declaration of String:
String <string_variable_name> = “<sequence_of_characters>”; System.out.println(myStr1.compareT
Or oIgnoreCase(myStr2));