SH1801 Classes and Objects (Part 2) PDF
Document Details
Uploaded by FlexibleTonalism3837
STI College
Tags
Summary
This document covers value and reference types, the Math class, static fields and methods in programming, specifically using examples in Java. It is likely part of a course in computer science or a related field. It explains the fundamentals of these different programming concepts.
Full Transcript
SH1801 Classes and Objects (Part 2) I. Value & Reference Types A. Value Types Value types are the basic types and include byte, short, int, long, float, double, boolean, and char. These types store the values assigned to them in corresponding mem...
SH1801 Classes and Objects (Part 2) I. Value & Reference Types A. Value Types Value types are the basic types and include byte, short, int, long, float, double, boolean, and char. These types store the values assigned to them in corresponding memory locations. Once you pass them to a method, you basically operate on the variable's value rather than on the variable itself. For example: public class MyClass { public static void main(String[ ] args) { int x = 5; addOneTo(x); System.out.println(x); } static void addOneTo(int num) { num = num + 1; } } // Outputs "5" B. Reference Types A reference type stores a reference (or address) to the memory location where the corresponding data is stored. When you create an object using the constructor, you create a reference variable. For example, consider having a Person class defined: public class Person { private int age; public int getAge() { return age; } public void setAge(int a) { this.age = a; } } public class MyClass { public static void main(String[ ] args) { Person j = new Person(); j.setAge(20); celebrateBirthday(j); System.out.println(j.getAge()); } static void celebrateBirthday(Person p) { p.setAge(p.getAge() + 1); } } //Outputs "21" II. The Math Class The JDK defines a number of useful classes, one of them being Math class, which provides predefined methods for mathematical operations. 07 Handout 1 *Property of STI [email protected] Page 1 of 3 SH1801 Math.abs() returns the absolute value of its parameter. int a = Math.abs(10); // 10 int b = Math.abs(-20); // 20 Math.ceil() rounds a floating point value up to the nearest integer value. The rounded value is returned as a double. double c = Math.ceil(7.342); // 8.0 Similarly, Math.floor() rounds a floating point value down to the nearest integer value. double f = Math.floor(7.343); // 7.0 Conversely, Math.min() returns the smallest parameter. int m = Math.min(10, 20); // 10 Math.pow() takes two (2) parameters and returns the first parameter raised to the power of the second parameter. double p = Math.pow(2, 3); // 8.0 III. Static Fields and Methods When you declare a variable or a method as static, it belongs to the class rather than to a specific instance. This means that only one instance of a static member exists, even if you create multiple objects of the class or if you do not create any. It will be shared by all objects. For example: public class Counter { public static int COUNT=0; Counter() { COUNT++; } } The COUNT variable will be shared by all objects of that class. public class MyClass { public static void main(String[ ] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); System.out.println(Counter.COUNT); } } //Outputs "2" The same concepts apply to static methods. public class Vehicle { public static void honk() { System.out.println("Beep"); } } Now, the honk method can be called without creating an object. public class MyClass { public static void main(String[ ] args) { Vehicle.honk(); } } 07 Handout 1 *Property of STI [email protected] Page 2 of 3 SH1801 IV. Final Variables Use the final keyword to mark a variable constant so that it can be assigned only once. For example: class MyClass { public static final double PI = 3.14; public static void main(String[ ] args) { System.out.println(PI); } } PI is now a constant. Any attempt to assign it a value will cause an error. Iguide: Methods and classes can also be marked final. This serves to restrict methods so that they cannot be overridden and classes so that they cannot be subclassed. These concepts will be covered in the next module. References: Deitel, H., & Deitel, P. (2014). Java: How to program-early objects (10th ed.). Prentice Hall. SoloLearn.com – Java Programming. Retrieved on March 07, 2018 from https://www.sololearn.com/Play/Java# 07 Handout 1 *Property of STI [email protected] Page 3 of 3