04 Object
04 Object
This Lecture
• Objects
• Classes
• instances
• Methods
• String Methods
• Wrapper Classes
• Math Class
Objects
• Objects are variables of user-defined data types.
(reference type variables)
a person,
an item in a shop,
an order someone has made, etc.
Objects
• Objects are variables of user-defined data types.
(reference type variables)
Persons
int number = 9;
Person c =……
Book d =……
Object vs Primitive
int number = 9;
double temperature = 36.6;
Book a =……
Book b =……
How to create a new
Book ( object ) with Java?
Template :
public Book(String bookName, String Author, String Publisher, String Genre, boolean
Fiction) {
this.bookName=bookName;
this.Author=Author; public class Book {
this.Publisher=Publisher; String bookName;
this.Genre=Genre; String Author;
this.Fiction=Fiction; String Publisher;
} String Genre;
boolean Fiction;
double price;
}
public class Book {
String bookName;
String Author;
String Publisher;
String Genre;
boolean Fiction;
public Book(String bookName, String Author, String Publisher, String Genre, boolean
Fiction) {
this.bookName=bookName;
this.Author=Author;
this.Publisher=Publisher;
this.Genre=Genre;
this.Fiction=Fiction;
} Object 1 :
public Book(String bookName, String Author, String Publisher, String Genre, boolean
Fiction) {
this.bookName=bookName;
this.Author=Author;
this.Publisher=Publisher;
this.Genre=Genre; Constructor
this.Fiction=Fiction;
}
(methods/behavior)
public static void main(String[] argument){
Book bookObject1 = new Book("Picking daisies","J.Frank","Cambridge
new object
horticulture","Gardening",false);
}
Methods
public class car {
private String color;
private int productionDate;
Introducing Strings:
A String is a sequence of characters.
For example, the famous "Hello World" is a String.
int number = 9;
double temperature = 36.6;
String str = "Hello World" ;
constructor
String Objects: Concatenation, Literals & More
Concatenating Strings
Concatenate is a fancy word for joining or linking two
things together.
You can do this in Java by using the addition operator, +,
String one = "Mc";
or +=.
String two = "Donald's";
String concatenate = one + two;
String Objects: Concatenation, Literals & More
Escape Sequences
Index: 0 1 2 3 4 5
67
String BigGreeting = "Hi Jambo" ;
int len =
// int len = 8;
BigGreeting.length();
String name = // String name =
BigGreeting.substring(3,len); “Jambo”;
String name = // String name =
BigGreeting.substring(3,7); “Jamb”;
String name =
// String name = “Hi Jambo”;
BigGreeting.substring(0);
int index =
// int index = 1;
BigGreeting.indexOf("i");
int index =
// int index = -1;
BigGreeting.indexOf("c");
int index =
// int index = 3;
BigGreeting.indexOf("Jam");
boolean bool = BigGreeting.equals("hi
// boolean bool = false;
Jambo");
int com =BigGreeting.compareTo("Hi
// int com = 0;
Jambo");
String Methods String Index Values
Index: 0 1 2 3 4 5
67
String BigGreeting = "Hi Jambo" ;
int com =BigGreeting.compareTo("Hi
// int com = 0;
Jambo");
if string1 > string2, it returns positive number
Integer.MIN_VALUE Returns the minimum possible int or Integer value(2^31-1) double varDbl = 15.3;
Integer.MAX_VALUE Returns the maximum possible int or Integer value(2^31) Double varDbl = 15.3;
varInt.intValue() Converts Integer value into an int value
double y = new Double(varDbl);
varDbl.doubleValue() Converts Double value into a double
value
Double x = new Double(varDbl);
double Num = x.doubleValue();
Autoboxing and Unboxing in Java
Autoboxing:
• Definition: Automatic conversion of primitive types to their corresponding wrapper classes.
• Example:
int num = 5;
Integer obj = num; // Autoboxing: int to Integer
• Usage: Occurs when a primitive value is passed to a method expecting an object or assigned to a
wrapper class variable.
Unboxing:
• Definition: Automatic conversion of wrapper class objects to their corresponding primitive types.
• Example:
Integer obj = 10;
int num = obj; // Unboxing: Integer to int
• Usage: Occurs when a wrapper class object is passed to a method expecting a primitive or
assigned to a primitive variable.
These processes simplify code by eliminating the need for manual conversion between primitives and
objects.
int number = 24;
Integer num = number; //Autoboxing: int to Integer Java's wrapper classes serve two
int x = new Integer(number); //Unboxing: Integer to int main purposes:
1.Object Conversion:
Integer z = new Integer(number); //create an Integer
object They convert primitive data types into
int y = z. intValue(); //Converts Integer value into an
int value objects. This is useful when an object
representation is required.
double var = 15.3;
2.Static Methods for Operations:
Double varDbl = var; //Autoboxing: double to Double They provide static methods for
double y = new Double(varDbl); //Unboxing: Integer to operations like converting strings to
double
numbers. These operations can be
Double x = new Double(varDbl); //create an Double performed without creating an
object
double Num = x.doubleValue(); Converts Double value
instance of the class.
into an double value
Using the Math Class
The Math class is a class that only contains static methods that are used
for specific purposes.
Method Use
Math.abs(x) Returns the absolute value of x. This can take either an int or a double.
Math.pow(base, exponent) Returns a double value of base raised to the power of exponent.
Math.random() Returns a double value greater than or equal to 0.0 and less than 1.0
// 9^(1/2))
double num = Math.sqrt(9);
// 9^3 = 9*9*9
double cubed = Math.pow(9, 3);