You are on page 1of 13

Objects and Classes II

Instructor: Dr. Fahed Jubair


Java Variable Types
• In Java, variables are either primitive types or reference types
• Primitive types are: boolean, char, byte, short, int, float, double
• Reference types refer to an object or an array
• In contrast, primitive types are not objects
• A variable declared as primitive type refers to a data item allocated in
memory with a fixed size
• A variable declared as reference type refers to an allocated object in
memory

© All rights reserved.


The null Value
• As we learned before, Java has default values for primitive data types:
• 0 for numeric types
• false for boolean type
• \u000 for char type
• Java assigns null as default value for reference type
• NullPointerException is a common runtime error that occurs when
attempting to access methods or attributes of a reference type with a
null value
• However, Java assigns no default value to a local variable inside a
method
© All rights reserved.
Uninitialized Variables
• The following code has a compile error due to uninitialized variables

public static void main(String[] args) {

String name; // local variables have no default values


int cnt; // local variables have no default values

System.out.println(name);
System.out.println(cnt);

© All rights reserved.


Immutability

• A class is said to be immutable if its


attributes cannot be modified after
instantiation
• A simple way to make a class
immutable is to make attributes
private and remove all setter methods
• An object is mutable if its content can
be changed after instantiation
• What is the advantage of
immutability? Imposes saftey

© All rights reserved. 5


Strings in Java are Immutable
• In Java, strings are immutable, i.e., their content cannot be modified.
• Then, if strings are immutable, how does this code gets executed?
String str = “Hello” ;
str = str + “World!” ;

• Given that strings are immutable, can you spot the problem in this
code?
String str = “” ;
for (int i = 0; i < n; i++)
str = str + i
© All rights reserved.
StringBuilder Class
• Mutable version of the String class

StringBuilder sb = new StringBuilder ("Hello, how are you?");


System.out.println(sb.length( )); // prints 19
sb.append(" I am fine thank you");
System.out.println(sb.length( )); // prints 39
sb.setCharAt(6, '#');
sb.setCharAt(9, '#');
System.out.println(sb.toString( )); // prints "Hello,#ho# are you? I am fine thank you"

© All rights reserved.


BigInteger and BigDecimal classes
• What if you need to compute with very large integers or floating-point
numbers (beyond the size of 64 bits)?
• Java Math utility provides two classes: BigInteger and BigDecimal
BigInteger a = new BigInteger(“9223372036854775807”) ;
BigInteger b = new BigInteger(“2”) ;
BigInteger c = a.multiply(b) ;
System.out.println(c); // output is 18446744073709551614

Exercise: write a Java program that compute the factorial of big integers (e.g., 50!)

© All rights reserved.


Testing for Equality in Strings
• We already covered useful methods available by the String class in Java
• E.g., charAt, getChars, length, etc
• What about methods for testing if two strings are equal?
String s1 = new String (“Java”);
String s2 = new String (“Java”);
String s3 = new String(“java”);
System.out.print(s1.equals(s2)); // prints true
System.out.print(s1 == s2) ; // prints false
System.out.print(s1.equals(s3)); // prints false
System.out.print(s1.equalsIgnoreCase(s2)); // prints true
System.out.print(s1.equalsIgnoreCase(s3)); // prints true
System.out.print(s1.compareTo(s2)); // prints 0
System.out.print(s1.compareTo(s3)); // prints -32
System.out.print(s3.compareTo(s1)); // prints +32
© All rights reserved.
Exception Handling
• As mentioned before, handling exceptions in your program make it more
robust and fault-tolerant
• Typically, a program deals with an exception by either tolerate it
(continue the execution) or terminate “gracefully”
• When terminating the execution, throw proper Exception type
• Java provides many exception types that you should read
• We can use try-catch block to catch exceptions (see next slide)

© All rights reserved.


Try-Catch

© All rights reserved.


Enumerated Types

A set of named constants that represents


useful attributes in a program

© All rights reserved.


Exercise
import java.util.Scanner;

public class Demo {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


if(checkFormat(date)){
System.out.println("day is " + getDay(date));
System.out.println("month is " + getMonth(date));
System.out.println("year is " + getYear(date));
}
else
System.out.println("error in format");
sc.close();
} Complete this function to return true if
public static boolean checkFormat(String date){ the date has the format month-day-year
e.g., 09-07-2014
}

public static int getDay(String date){


Complete this function to extract the day
from a given date and return it as an integer
}
Complete this function to extract the month
public static int getMonth(String date){
from a given date and return it as an integer
}
Complete this function to extract the year
public static int getYear(String date){
from a given date and return it as an integer
}
} © All rights reserved.

You might also like