Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Overview Objects Classes Syntax
Objects
A “real, existing, item” “Instance” of a class
Classes Class Specifies attributes and methods for an object Encapsulated data Template Create objects from Instantiate
Quiz Define “class” in the context of OO design. Define “object” in the context of OO design.
Syntax Computers only do what they are told! You have to be priciest what and how you tell a computer what to do! Exactly!
Syntax Syntax diagram Start point Term Identify Path Termination = Expression
Syntax Variable A box to put things in Can change! nUsefullBox 25
Syntax private int nVar; protected floatfVar; StringsText; ExpressionIdentifier;
Syntax Identifiers Java Letter Java Digita.. z A.. Z $ _ OtherJava Letter 0.. 9
Syntax nText12 $Text m_oObj2 12Def #we23
Syntax Primative type Class type Identifier, ;
Syntax private int nVar; protected floatfVar; StringsText;
Syntax Variables are: Boxes you save data in You have to declare variables What type are they? What names does it have?
Syntax Class type Built into Java Data + methods Create your own User defined String Integer FileDialog
Syntax Primitive data types Built into Java int – integer (whole numbers) short long float – real numbers (approximately) double boolean – logical. True or false char – a single Unicode character byte – 8 bits of data
Syntax TypeSizeMinMax byte8 Bits short16 bits int32 bits long< -9 x > -9 x float32 bits+/- 3.4 x decimal digits double64 bits+/- 1.7 x decimal digits
Quiz Define “variable” in the context of software engineering List the inbuilt primitive data types in Java
Syntax Char Unicode character 16 bits unique characters A.. Z, Å, Ä, Ö, 1.. 9, Æ Ë Σ Ω ش הּ Ordered Numbered \u2000 = 丠
Syntax Boolean True or false Used in comparisons Not a number Not the same as C
Syntax Objects Instances of classes How to create an object? StringstrText = new String();
Syntax Constructor A method called when the object is created Sets up the object Same name as the class No return type New Key word in Java Used when creating an object Create the object in memory
Syntax StringstrText = new String(); Reference A variable that tells use where to find an object A pointer Value What’s in the box Primitive data type Hello World strText
Syntax Aliasing More than one variable can point to the same object StringstrText1 = new String(); StringstrText2 = strText1; Hello World strText1 strText2
Syntax Garbage collection When an object no longer has a reference pointing to it Hello World strText
Syntax class identifier Class associations Class body modifier { } Class member Field declaration Constructor declaration Method declaration
Syntax public class HelloWorld { }
Syntax public static void main(String[] args) { System.out.println(“Hello World”); } type identifier Method body modifier parameters void
Quiz List the parts of a class definition used in Java List the parts of a method definition in Java
Syntax public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello World”); }
Syntax public class FunctionCall { private intm_nCount = 0; public void func1() { m_nCount++; System.out.println(“Count is : “ + m_nCount); } public static main(String[] args) { func1(); }
Questions?