Professional Documents
Culture Documents
SIS2024Y Object-Oriented Programming: Introduction To Java Programming (Part 1)
SIS2024Y Object-Oriented Programming: Introduction To Java Programming (Part 1)
SIS2024Y Object-Oriented Programming: Introduction To Java Programming (Part 1)
Object-Oriented Programming
Lecture 2
Introduction to Java programming
(Part 1)
1
Agenda
• Java Programming Style Guidelines
• Comments in Java
• Variables & Constants
• Data Types
– Primitive
– Non-primitive
• Arrays
2
Activity 1
1. Real-world objects contain ___ and ___.
2. A software object's state is stored in ___.
3. A software object's behavior is exposed through ___.
4. Hiding internal data from the outside world, and accessing
it only through publicly exposed methods is known as data
___.
5. ___is the representation of all the essential features of an
object, which means its possible states and behaviours.
6. When we use ___ , we have classes at the top of the
hierarchy and classes at the bottom of the hierarchy (or
those which get the characteristics from some other class).
3
Recap: A simple Java program
4
Java Programming Style Guidelines
1. Names representing types must be nouns and
written in mixed case starting with upper case.
• E.g. Line, AudioSystem, String
5
Java Programming Style Guidelines
4. Names representing methods/functions must
be verbs and written in mixed case starting
with lower case.
• E.g. getName(), computeTotalWidth()
6
Comments in Java
• Comments are preceded by a slash-slash (//)
and ends where the current source code line
ends.
//This is a C++ style comment in Java source code
9
Static Variable Example
10
Static Memory Representation
https://www.javatpoint.com/images/staticvariable.JPG 11
Final Keyword
• The final keyword in java is used to restrict the
user. The java final keyword can be used in many
context:
– Variable
– Method
– Class
13
Java Data Types
• Based on the data type of a variable, the
operating system allocates memory and
decides what can be stored in the reserved
memory.
14
http://www.javatportal.com/image/corejavaintalling/datatype.jpg
15
Primitive Data Types
http://www.geeksforgeeks.org/data-types-in-java/
16
Primitive Data Types
Examples
Can also be written as:
1. boolean done = true; boolean done;
done = false;
2. byte a = 126;
3. char a = 'G';
4. short s = 56;
5. int i=89;
6. long number = 600851475143L
int number = 600851475143;
7. double d = 4.355453532;
8. float f = 4.7333434f;
17
Non-Primitive Data Types
• A variable of a non-primitive type doesn't
contain the value directly; instead, it is a
reference (similar to a pointer) to an object.
• Examples:
– String
– Object
– Array
– Etc…
18
String Data Type
• String Data Type:
String module = new String("Object Oriented Programming");
s1.equals("John"); // CORRECT
s1 == s2; // INCORRECT
20
String Data Type
• A more general compareTo() method is also provided:
– if s1 > s2, it returns positive number
– if s1 < s2, it returns negative number
– if s1 == s2, it returns 0
String s1 = "John";
String s2 = "Jane";
int flag1 = s1.compareTo(s2);
String s3 = "Joan";
int flag3 = s1.compareTo(s3);
String s4 = "John";
int flag5 = s1.compareTo(s4);
21
String Data Type
• Substring:
String s1 = "listen";
String s2 = s1.substring(0, 4); // liste
String s3 = s1.substring(4, 5); // en
String s4 = s3 + s2; //enlist
1) Addition
2) Subtraction Output
3) Multiplication
4) Division
23
Arrays
• Array is a collection of similar types of data.
24
Arrays
• In Java, unlike most other programming languages,
there are three steps to actually filling out an array,
rather one:
– Declare the array.
int [] myIntArray;
class ArraySample {
}
}
} 26
Activity 3
• Modify the calculator example in Activity 2 such
that the calculation results are now stored in an
array variable. You may assume an appropriate
data type for the array.
27
Activity 4
• Now, modify Activity 3 such that another array
variable is used to stored the result type (i.e.
addition, subtraction, multiplication, division).
28
References
• https://www.javatpoint.com/static-keyword-
in-java
• http://geosoft.no/development/javastyle.html
• http://www.javatportal.com/variable-and-
datatype-in-java
• http://www.sitesbay.com/java/java-array
29