CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment: http://muele.mak.ac.ug office: block A, level 3, department of computer science alt. office: institute of open, distance, and elearning, room 2 The Java language Basics Kizito (Makerere University) CSC 1214 February 2018 1 / 18
Overview 1 Basic Constructs Types Variables Program Control Kizito (Makerere University) CSC 1214 February 2018 2 / 18
Types Basic Constructs Types Every variable has a type, every expression has a type, and every type is strictly defined Inbuilt types: char, int, float (32 bits), double (64 bits), boolean... User defined types: Defined using the keyword class Examples: char c = a ; short t; int i = 50, m days = new int[12]; Java is strictly typed (e.g., can t assign float to int as is possible in C) Kizito (Makerere University) CSC 1214 February 2018 3 / 18
Types Types Simple Type Groups Integers (byte, short, int, long) Floating point numbers (float, double) Characters (char) Boolean (boolean) These form the basis of all other types of data you can create Kizito (Makerere University) CSC 1214 February 2018 4 / 18
Types Types Type Conversion and Casting If two types are compatible, Java performs the conversion automatically (e.g., assign int to long) We need casting for incompatible types and narrowing conversions (target-type) expression Type promotion in expressions int d = a * b / c; (a, b, c are of type byte) double d = (int)i + (float)f; Kizito (Makerere University) CSC 1214 February 2018 5 / 18
Types String Conversion and tostring() String defines valueof() Object defines tostring() Integer defines parseint(str), tobinarystring(num), tooctalstring(num)... Example: String s = "This is a test string"; static String; System.out.println(s.toString()); System.out.println(s); System.out.print(String.valueOf(3.14)); Kizito (Makerere University) CSC 1214 February 2018 6 / 18
Variables Variables Variable is the basic unit of data storage Defined by the combination of an identifier, a type, and an optional initializer type identifier [ = value][, identifier[ = value]...]; Java is case sensitive Total, total, and TOTAL are different identifiers Examples: int a, b, c; byte x = 22, y; double d = Math.sqrt(a * a + b * b); final (const in C/C++) can t modify contents final int FILE OPEN = 1; final int FILE CLOSED = 0; Kizito (Makerere University) CSC 1214 February 2018 7 / 18
Variables Variables Instance Variables and Identifiers Sometimes we choose identifiers ourselves when writing a program (such as Welcome) Sometimes we are using another programmer s code, so we use the identifiers that he or she chose (such as println) Often we use special identifiers called reserved words that already have a predefined meaning in the language A reserved word cannot be used in any other way Variables defined within a class are called instance variables They are unique to their object Instance variables can be hidden (using this to unhide them) Instance variables declared as static are, essentially, global variables Using super to unhide instance variables Kizito (Makerere University) CSC 1214 February 2018 8 / 18
Variables Identifiers Java Reserved words abstract boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while Kizito (Makerere University) CSC 1214 February 2018 9 / 18
An operator takes one or more operands and produces a result An operator is just a function, but with a different syntax Most of Java s operators can be divided into: Arithmetic Bitwise Relational Logical Kizito (Makerere University) CSC 1214 February 2018 10 / 18
Arithmetic + addition (concatenation for strings) subtraction (also unary minus) multiplication / division % modulus ++ increment + = addition assignment = subtraction assignment = multiplication assignment / = division assignment % = modulus assignment decrement Kizito (Makerere University) CSC 1214 February 2018 11 / 18
Bitwise bitwise unary NOT & bitwise AND bitwise OR ˆ bitwise XOR (exclusive OR) >> shift right >>> shift right zero fill << shift left & = bitwise AND assignment = bitwise OR assignment ˆ= bitwise exclusive OR assignment >>= shift right assignment >>>= shift right zero fill assignment <<= shift left assignment Kizito (Makerere University) CSC 1214 February 2018 12 / 18
Relational == equal to! = not equal to > greater than < less than >= greater than or equal to <= less than or equal to Kizito (Makerere University) CSC 1214 February 2018 13 / 18
Boolean Logical & logical AND logical OR ˆ logical XOR short-circuit OR && short-circuit AND! logical unary NOT & = AND assignment = OR assignment ˆ= XOR assignment == equal to! = not equal to? : ternary if-then-else Kizito (Makerere University) CSC 1214 February 2018 14 / 18
Other = assignment () parenthesis [] square brackets. dot instanceof equals()?? Kizito (Makerere University) CSC 1214 February 2018 15 / 18
Operator Precedence () [] Highest ++! / % + >> >>> << > >= < <= ==! = & ˆ &&? : = op = Lowest Kizito (Makerere University) CSC 1214 February 2018 16 / 18
Program Control Java Program Control A program is made up of one or more classes A class contains one or more methods (functions in C) A method contains program statements. A Java application always contains a method called main (just like in C) Filename should match name of the main class Execution start in the main() method of the main class When execution of a method stops, it may return a value or not (if the return type is void) public static void main(string args[]) Kizito (Makerere University) CSC 1214 February 2018 17 / 18
Program Control First Java Program import java.util.*; public class HelloDate { public static void main(string[] args) { System.out.print("Hello, it is "); System.out.println(new Date()); } } Filename should match the name of the main class (i.e., HelloDate.java) Compiling (javac HelloDate.java) and running (java HelloDate) Integrated/Interactive Development Environments (IDEs) may provide graphical interface for compiling and running (or both at a click of a single button) Sample output: Hello, it is Mon Feb 5 10:49:03 EAT 2018 Kizito (Makerere University) CSC 1214 February 2018 18 / 18
Program Control First Java Program import java.util.*; public class HelloDate { public static void main(string[] args) { System.out.print("Hello, it is "); System.out.println(new Date()); } } Filename should match the name of the main class (i.e., HelloDate.java) Compiling (javac HelloDate.java) and running (java HelloDate) Integrated/Interactive Development Environments (IDEs) may provide graphical interface for compiling and running (or both at a click of a single button) Sample output: Hello, it is Mon Feb 5 10:49:03 EAT 2018 Kizito (Makerere University) CSC 1214 February 2018 18 / 18