Objects Classes and Syntax Dr Andrew Wallace Ph
Objects, Classes and Syntax Dr. Andrew Wallace Ph. D BEng(hons) Eur. Ing andrew. wallace@cs. umu. se
Overview • Objects • Classes • Syntax
Objects
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 25 • Can change! n. Use full. B ox
Syntax Identifier Expression • private int • protected float • String n. Var; f. Var; s. Text; ;
Syntax • Identifiers Java Letter a. . z Java Digit A. . Z $ _ Other. Java Letter 0. . 9
Syntax • n. Text 12 • $Text • m_o. Obj 2 • 12 Def • #we 23
Syntax Primative type Identifier Class type , ;
Syntax • private int • protected float • String n. Var; f. Var; s. Text;
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 • File. Dialog
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 Type Size Min Max byte 8 Bits -128 127 short 16 bits -32 768 32 767 int 32 bits -2 147 483 648 2 147 483 647 long < -9 x 1018 > -9 x 1018 float 32 bits +/- 3. 4 x 1038 7 decimal digits double 64 bits +/- 1. 7 x 10308 15 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 • 65 536 unique characters • A. . Z, Å, Ä, Ö, 1. . 9, Æ Ë Σ Ω הּ ﺵ • Ordered • Numbered • u 2000 = � • www. unicode. org
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? • String str. Text = 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 • String • Reference str. Text = new String(); • A variable that tells use where to find an object • A pointer str. Text • Value • What’s in the box • Primitive data type Hello World
Syntax • Aliasing • More than one variable can point to the same object String str. Text 1 = new String(); str. Text 2 = str. Text 1; str. Text 1 str. Text 2 Hello World
Syntax • Garbage collection • When an object no longer has a reference pointing to it str. Text Hello World
Syntax class identifier Class associations Class body modifier { Field declaration Constructor declaration Method declaration } Class member
Syntax public class Hello. World { }
Syntax type modifier identifier parameters void public static void main(String[] args) { System. out. println(“Hello World”); } Method body
Quiz • List the parts of a class definition used in Java • List the parts of a method definition in Java
Syntax public class Hello. World { public static void main(String[] args) { System. out. println(“Hello World”); } }
Syntax public class Function. Call { private int m_n. Count = 0; public void func 1() { m_n. Count++; System. out. println(“Count is : “ + m_n. Count); } public static main(String[] args) { func 1(); } }
Questions?
- Slides: 33