Java Classes & Primitive Types Rui Moreira Classes Ponto (from figgeom) x : int = 0 y : int = 0 n Attributes q Characteristics/properties of classes q Primitive types (e.g., char, byte, int, float, etc.) q Class types (e.g., String, Date, Vector, Ponto, etc.) q Automatic initialisation (primitive type variables are initialised to 0; class type variables are initialised to null) n Methods Ponto( ) getx( ) gety( ) setx( ) sety( ) drawponto( ) q Constructors (special methods with same name of class and no return value) q Behaviour of classes (receive list of pass-by-value parameters and return given type value) Rui Moreira 2 1
Example: Ponto class /** Ponto Class Declaration: represents a generic point in space */ /** Attributes: by default these attribute fields are public */ int x = 0; int y = 0; /** Default Constructor: automatically provided by Java language */ //public Ponto() { /* A constructor is a public method with the same name as the class and with no return type; a constructor method is used to build/create objects of the class type, e.g., Ponto p = new Ponto(); If no other constructor is provided we do not need to implement the default constructor (that is why it is commented!!) */ Rui Moreira 3 Example: Ponto class /** Ponto Class Declaration: represents generic point in space */ /** Attributes: by default these attribute fields are public */ int x = 0; int y = 0; /** We may specify other specific constructors, for example, to explicitly initialize the attributes of our objects: Ponto p = new Ponto(2, 4); */ public Ponto(int a, int b) { x = a; y = b; /** If the previous constructor is provided Java no longer provides the default constructor; therefore, if we want to be able to use the default constructor we also need to implement it as follows... */ public Ponto() { x = 0; y = 0; Rui Moreira 4 2
Example: Ponto class /** Ponto Class Declaration: represents generic point in space */ /** Attributes: by default these attribute fields are public */ int x = 0; int y = 0; /** Specific constructors: explicitly initialize object attributes */ public Ponto(int a, int b) { x = a; y = b; /** Default Constructor: explicit implementation because other constructors exist */ public Ponto() { x = 0; y = 0; Rui Moreira 5 Example: Ponto class /** Ponto Class Declaration: represents generic point in space */ /** Encapsulation: hide implementation with private attributes; provide access to attributes through well defined public method interfaces */ private int x = 0; private int y = 0; /** Get attribute value */ public int getx() { return x; /** Set attribute value */ public void setx(int a) { x = a; // Do the same for the y attribute Rui Moreira 6 3
Example: Ponto class /** Ponto Class Declaration: represents generic point in space */ /** Encapsulated attributes behind public interface methods */ private int x = 0; private int y = 0; /** Constructors */ public Ponto(int a, int b) { x = a; y = b; public Ponto() { x = 0; y = 0; /** Get attribute values */ public int getx() { return x; public int gety() { return y; /** Set attribute values */ public void setx(int a) { x = a; public void sety(int b) { y = b; /** Other behaviour.../ public void drawponto() { /*... */ Rui Moreira 7 Identifiers & Name Conventions n Identifiers (for classes, interfaces, methods and attributes) q Are case-sensitive; q No size limit (number of characters); q Must start with: letter, underscore or $; q May contain digits/numbers after first character; n Classes & Interfaces q Usually first letter of each word is uppercase, e.g., OneClasse, OneInterface n Methods & Attributes q Usually first word is entirely smallcase then first letter of remainer words is uppercase, e.g., onemethod(), oneattribute Rui Moreira 8 4
Attributes: primitve types n Logical q boolean (true or false); n Textual q char (unsigned 16 bit number - unicode character); q String (final Class - sequence of unicode chars); n Integer q byte (8 bits); short (16 bits); q int (32 bits); long (64 bits); n Floating Point q float (32 bits floating-point) q double (64 bits floating-point) Rui Moreira 9 Attributes: primitve types particularities n Casts not allowed between booleans <-> intetegers; n String is a class but presents also behaviour of primitive types: q String s = Hello World! ; // e.g., assigned a literal sequence of chars n Integer literals are signed (e.g., byte b; // -128 < b < 127); n Integers are int by default except when followed by L (long); n Integers may be represented in the following formats: q int d = 10; // decimal format q int o = 077; // octal format q int h = 0xA; // hexadecimal format n Floating Point literals may use. or E: q float f1 = 3.14f; // float number followed by letter f or F q float f2 = 6.04E23; // float number representing 6.04x10 23 q double d = 23.6E+235D; // double number followed by d or D Rui Moreira 10 5
Some declarations n boolean truth = false; n char a = a, c = C ; n char r= \n, t= \t ; // Usage of escape char n char u = \u0000 ; // Represents null char n String str = new String( string example ); n byte b = 128; n short s = 512; n int i = 1024; n long l = 2L; n float f = 2.78F n double d = 8.16; Rui Moreira 11 Memory allocation n When declaring variable/attributes of primitve types the memory space for the variables is automatically allocated; n When declaring variable/attributes of class types (e.g., String str) the memory is allocated for the reference (variable) and not for the object! n Before we can use a reference variable we must set it to point to a given object! Rui Moreira 12 6
Creating Objects puclic class DateApp { public static void main(string args[]){ // Declare reference variable Date today; // Create object today = new Date(12, 2, 2005); class Date { int day=0, month, year; public Date(int d, int m, int y){ day=d; month=m; year=y; Calling constructor: Creating objects in 3 Steps: Allocate memory for the reference; Allocate memory for the object; Set reference pointing to object. 1 st allocates memory for the specific object; 2 nd initialises attributes by default (month=0, year=0) and sets explicit attribute values (day=0); 3 rd runs constructor code (inside constructor method) Rui Moreira 13 Creating Objects // Declare reference variable - today Date today; today variable day 12 // today references new Date object today = new Date(12, 2, 2005); month 2 year 2005 Date object Rui Moreira 14 7
Default Initialisation of Attributes n Java automatically initialises attributes (class member variables): q Primitive types: n boolean attribute variables false n char attribute variables \u0000 n integer attribute variables - 0 n floating point attribute variables 0.0f or 0.0d q Class types: n reference attribute variables null String s; // s is set to null, meaning it points to no object Rui Moreira 15 Local Variables n Default initialisation is done only for attribute variables (class member variables); n Local variables (and parameters) are stored in the STACK which means these are NOT automatically initialised, i.e., we may NOT use local variables without explicit initialisation; n The Java compiler generates a compile-time error if we try to use a non-initialised local variable. Rui Moreira 16 8
Exercises implement classes: n Implement Class Rectangulo n Implement Class Triangulo n Implement Class Circulo n Implement Methods: Rectangulo upleftponto : Ponto = null downrightponto : Ponto = null Rectangulo( ) getupleftponto( ) getdownrightponto( ) setupleftponto( ) setdownrightponto( ) isinside( ) area( ) tostring( ) Triangulo upponto : Ponto = null downponto : Ponto = null middleponto : Ponto = null getupponto( ) getdownponto( ) getmiddleponto( ) setupponto( ) setdownponto( ) setmiddleponto( ) isinside( ) area( ) tostring( ) q public void move(int dx, int dy) - changes coordinates of the given amount q public boolean isinside(ponto p) - checks if p is inside the geometric figure q public float area() - returns the area of the geometric figure q public String tostring() - returns a string describing the geometric figure, e.g., for the Rectangulo with points (1,2) and (3,7) it returns Rectangle@[(1,2);(3,7)] Circulo centerponto : Ponto = null diameter : int = 0 getcenterponto( ) getdiameter( ) setcenterponto( ) setdiameter( ) isinside( ) area( ) tostring( ) Rui Moreira 17 Instantiation example // Declaring and creating 2 points Ponto p1 = new Ponto(1, 2); Ponto p2 = new Ponto(4, 6); Rectangulo upleftponto : Ponto = null downrightponto : Ponto = null Rectangulo( ) getupleftponto( ) getdownrightponto( ) setupleftponto( ) setdownrightponto( ) isinside( ) area( ) tostring( ) // Declaring and creating 2 rectangles Rectangulo r1 = new Rectangle(p1, p2); Rectangulo r2 = new Rectangle(new Ponto(5, 8), new Ponto(10, 12)); reference p1 reference p2 reference r1 object Ponto x 1 y 2 object Ponto x 4 y 6 object Rectangulo upleftponto downleftponto object Ponto x 5 y 8 object Ponto x 10 y 12 object Rectangulo upleftponto downleftponto reference r2 Rui Moreira 18 9