10 min read•march 28, 2023
Kashvi Panjolia
Athena_Codes
Kashvi Panjolia
Athena_Codes
💡 are instances of and we can write to manipulate these .
Java is what is known as an object-orientated programming language, or OOP for short. What is this object-orientated programming, and why is it so important to Java? This is the first of three units emphasizing object-orientated programming (the others are Units 5 and 9, covering and respectively). This unit, we will be learning about , the cornerstone of object-orientated programming, and what they are. There are four principles of object-orientated programming: (partly covered in this unit, also in Unit 5), (covered in Unit 5), , and (both covered in Unit 9).
In this unit, you'll learn how to write your first (functions in other programming languages), which perform a specific class. You'll also learn how to use from the Math and String , which will allow us to work more with numbers and text. These will make the foundations of all future units. Finally, you'll learn about , which are : combinations of other data types that allow us to represent actual items and situations.
Java is an object-orientated programming language, which means that most major programs in Java are about the manipulation of . What are ? are a reference type, which refers to how they are combinations of other primitive and reference data types. When you refer to them, you are not referring to the actual object itself, but where it is stored in data.
How do we know what combination of primitive and each object has? This is due to the help of a class. A class is like a template that defines what an object is like and what the object can do. A class is basically the guidelines for a type of object, and an object is a particular instance of that class. We can think of a class as a blueprint for a house, and the object is a particular house. Different houses are different . The different houses may look different, but they have the same general features and functions. This is how and work.
To create an object, we call the object's . Every class must have a , and if you do not create one, it will have a default that is empty. If we use the analogy that a class is like a blueprint for an object, the , when called, is the architect that makes the blueprint come to life. The takes in , which are the attributes of the object (name, age, etc), and when called, those can be assigned specific values to create the object. Here is an example and initialization of a car object:
Car(String brand, String model, int year) Car flash = new Car("BMW", "X7", 2023);
Creating an object is similar to creating a variable: you start with the type, then enter the name of the object, and use the equals sign () to assign the object to a value. With , however, the "new" keyword is used to create a new object. It is important you include this keyword when you initialize ; otherwise, you will simply point the object you created to another object.
Notice how the brand, model, and year were provided in the order they were written in the . When calling and constructors, the order of the matters. An will arise if you enter the order of the differently than they were provided in the . In this case, the stated the order needs to be (String, String, int), so if you entered (String, int, String), the program would crash, and this exception will display. The computer will not know what to do if you enter the in a different order.
A class can have multiple constructors. This is called overloading a . Each must be different than the others. Since the must only be named the name of the class, the constructors should be differentiated based on their .
Null is another keyword in Java. You can instantiate an object as null, meaning there is no reference created and the object contains nothing. Here's how to do it:
Car lightning = null;
Be careful when you instantiate an object as null, because if you try to call a method on it or use it anywhere else, you will get a NullPointerException. This is an important exception, and it states that the computer cannot do anything with this object because, well, there is no reference to the object.
A method is made up of five main parts: the scope, the return type, the name, the , and the body. This may sound like a lot, but it's very formulaic. We'll tackle with this example:
public void study(int hoursStudied) {
totalHours += hoursStudied;
}
The first word, public, defines the scope of the method. Public means that anyone that accesses your code after it has been published can see this method. The next word, void, defines the return type of the method. Void means the method returns nothing. The name of the method is study, and it is case-sensitive.
This method only has one parameter: hoursStudied, which is an int. When this method is called, the user will have to enter an integer defining the number of hours they studied for. Finally, the body of the method is the statement totalHours++; and it tells the computer what the method does. There may be a variable elsewhere in the class called totalHours that is being incremented by the amount of hoursStudied every time the method is called.
This method can be called on an object. Person bob = new Person("bob");
bob.study(2);
In this snippet, a new object of type Person was created, and the name parameter was passed in as "bob." Then, the study method was called on bob using dot notation. Dot notation is the usage of a period, or dot, to call a method on an object. The number of hours bob studied was 2 hours, so 2 was passed in as an argument to the study method.
, like constructors, can be overloaded. This means the parameter list for each method that is overloaded must be different.
public void waterTracker(int numberOfGlasses)
public void waterTracker(String typeOfDrink)
The method waterTracker is overloaded because there are two with the same name. However, these two are still different because the first one takes an integer as a parameter and the second one takes a String as a parameter. Depending on what you pass in as the argument when called the waterTracker method, one of these two will be called. Remember to stay hydrated.🚰
In the study(int hoursStudied) method defined above, the return type was void, so nothing was returned. However, there are many different return types for in Java, and they can be both primitive and . A method can return an int, String, boolean, double, Object, and other types of data. Implementing a non-void return type in a method is useful when we want to store the result of the method for later.
Each method that has a non-void return type must have a return statement at the end of the method. A return statement will tell the computer what is being returned by that method and will be the data we will store for later. The return statement must always be the last line in the method.
public double degreesToRadians(double degrees) {
double radians = degrees * (3.141 / 180);
return radians;
} In this method, the return type of the degreesToRadians method is a double, so we return the variable radians because it is a double. We can now store the value returned by this method in a variable like this:
double result = degreesToRadians(34.5); The right side of the expression will evaluate to approximately 0.602, which will then become the value of the variable result. Pretty useful!
In Java, a String object is a sequence of characters. You can create a String object by using the String class's .
String greeting = new String("What's up?");
However, the more efficient version is to use a string literal, which you learned about in the previous unit.
String greeting = "What's up?";
Since a String is an object, you can call on Strings like you would for any other object. Some common are .length() and .toLowerCase(). One special attribute of Strings is string concatenation. String concatenation is the process of combining two or more strings into a single string. You can concatenate strings using the + operator. Be sure to add a space between words because Java will not do that for you.
String greeting = "What's " + "up?";
Escape characters are characters that are used to represent certain special characters in a string. For example, the \n escape character is used to represent a new line in a string. Here is an example of a string with an escape character:
String greeting = "Hello,\nworld!";
This will output: Hello,
world!
In Java, the String class has several that can be used to perform operations on strings. Some common String are:
As you can see, the substring method is an overloaded method and has two versions. The index of a string starts at 0 and counts every character, including spaces and punctuation marks. The reason the index starts at 0 and not 1 is that Java is a zero-indexed language. The string "taco cat" has a length of 8 characters, or 8 indices, by starting at 0 and counting each letter until the end of the string.
The is a class in Java that provides various mathematical functions and constants. You do not need to import this class; it is already included in the standard Java package. Some common of the are:
The also has constants you can use to increase your efficiency, such as (approximately 3.14159).
10 min read•march 28, 2023
Kashvi Panjolia
Athena_Codes
Kashvi Panjolia
Athena_Codes
💡 are instances of and we can write to manipulate these .
Java is what is known as an object-orientated programming language, or OOP for short. What is this object-orientated programming, and why is it so important to Java? This is the first of three units emphasizing object-orientated programming (the others are Units 5 and 9, covering and respectively). This unit, we will be learning about , the cornerstone of object-orientated programming, and what they are. There are four principles of object-orientated programming: (partly covered in this unit, also in Unit 5), (covered in Unit 5), , and (both covered in Unit 9).
In this unit, you'll learn how to write your first (functions in other programming languages), which perform a specific class. You'll also learn how to use from the Math and String , which will allow us to work more with numbers and text. These will make the foundations of all future units. Finally, you'll learn about , which are : combinations of other data types that allow us to represent actual items and situations.
Java is an object-orientated programming language, which means that most major programs in Java are about the manipulation of . What are ? are a reference type, which refers to how they are combinations of other primitive and reference data types. When you refer to them, you are not referring to the actual object itself, but where it is stored in data.
How do we know what combination of primitive and each object has? This is due to the help of a class. A class is like a template that defines what an object is like and what the object can do. A class is basically the guidelines for a type of object, and an object is a particular instance of that class. We can think of a class as a blueprint for a house, and the object is a particular house. Different houses are different . The different houses may look different, but they have the same general features and functions. This is how and work.
To create an object, we call the object's . Every class must have a , and if you do not create one, it will have a default that is empty. If we use the analogy that a class is like a blueprint for an object, the , when called, is the architect that makes the blueprint come to life. The takes in , which are the attributes of the object (name, age, etc), and when called, those can be assigned specific values to create the object. Here is an example and initialization of a car object:
Car(String brand, String model, int year) Car flash = new Car("BMW", "X7", 2023);
Creating an object is similar to creating a variable: you start with the type, then enter the name of the object, and use the equals sign () to assign the object to a value. With , however, the "new" keyword is used to create a new object. It is important you include this keyword when you initialize ; otherwise, you will simply point the object you created to another object.
Notice how the brand, model, and year were provided in the order they were written in the . When calling and constructors, the order of the matters. An will arise if you enter the order of the differently than they were provided in the . In this case, the stated the order needs to be (String, String, int), so if you entered (String, int, String), the program would crash, and this exception will display. The computer will not know what to do if you enter the in a different order.
A class can have multiple constructors. This is called overloading a . Each must be different than the others. Since the must only be named the name of the class, the constructors should be differentiated based on their .
Null is another keyword in Java. You can instantiate an object as null, meaning there is no reference created and the object contains nothing. Here's how to do it:
Car lightning = null;
Be careful when you instantiate an object as null, because if you try to call a method on it or use it anywhere else, you will get a NullPointerException. This is an important exception, and it states that the computer cannot do anything with this object because, well, there is no reference to the object.
A method is made up of five main parts: the scope, the return type, the name, the , and the body. This may sound like a lot, but it's very formulaic. We'll tackle with this example:
public void study(int hoursStudied) {
totalHours += hoursStudied;
}
The first word, public, defines the scope of the method. Public means that anyone that accesses your code after it has been published can see this method. The next word, void, defines the return type of the method. Void means the method returns nothing. The name of the method is study, and it is case-sensitive.
This method only has one parameter: hoursStudied, which is an int. When this method is called, the user will have to enter an integer defining the number of hours they studied for. Finally, the body of the method is the statement totalHours++; and it tells the computer what the method does. There may be a variable elsewhere in the class called totalHours that is being incremented by the amount of hoursStudied every time the method is called.
This method can be called on an object. Person bob = new Person("bob");
bob.study(2);
In this snippet, a new object of type Person was created, and the name parameter was passed in as "bob." Then, the study method was called on bob using dot notation. Dot notation is the usage of a period, or dot, to call a method on an object. The number of hours bob studied was 2 hours, so 2 was passed in as an argument to the study method.
, like constructors, can be overloaded. This means the parameter list for each method that is overloaded must be different.
public void waterTracker(int numberOfGlasses)
public void waterTracker(String typeOfDrink)
The method waterTracker is overloaded because there are two with the same name. However, these two are still different because the first one takes an integer as a parameter and the second one takes a String as a parameter. Depending on what you pass in as the argument when called the waterTracker method, one of these two will be called. Remember to stay hydrated.🚰
In the study(int hoursStudied) method defined above, the return type was void, so nothing was returned. However, there are many different return types for in Java, and they can be both primitive and . A method can return an int, String, boolean, double, Object, and other types of data. Implementing a non-void return type in a method is useful when we want to store the result of the method for later.
Each method that has a non-void return type must have a return statement at the end of the method. A return statement will tell the computer what is being returned by that method and will be the data we will store for later. The return statement must always be the last line in the method.
public double degreesToRadians(double degrees) {
double radians = degrees * (3.141 / 180);
return radians;
} In this method, the return type of the degreesToRadians method is a double, so we return the variable radians because it is a double. We can now store the value returned by this method in a variable like this:
double result = degreesToRadians(34.5); The right side of the expression will evaluate to approximately 0.602, which will then become the value of the variable result. Pretty useful!
In Java, a String object is a sequence of characters. You can create a String object by using the String class's .
String greeting = new String("What's up?");
However, the more efficient version is to use a string literal, which you learned about in the previous unit.
String greeting = "What's up?";
Since a String is an object, you can call on Strings like you would for any other object. Some common are .length() and .toLowerCase(). One special attribute of Strings is string concatenation. String concatenation is the process of combining two or more strings into a single string. You can concatenate strings using the + operator. Be sure to add a space between words because Java will not do that for you.
String greeting = "What's " + "up?";
Escape characters are characters that are used to represent certain special characters in a string. For example, the \n escape character is used to represent a new line in a string. Here is an example of a string with an escape character:
String greeting = "Hello,\nworld!";
This will output: Hello,
world!
In Java, the String class has several that can be used to perform operations on strings. Some common String are:
As you can see, the substring method is an overloaded method and has two versions. The index of a string starts at 0 and counts every character, including spaces and punctuation marks. The reason the index starts at 0 and not 1 is that Java is a zero-indexed language. The string "taco cat" has a length of 8 characters, or 8 indices, by starting at 0 and counting each letter until the end of the string.
The is a class in Java that provides various mathematical functions and constants. You do not need to import this class; it is already included in the standard Java package. Some common of the are:
The also has constants you can use to increase your efficiency, such as (approximately 3.14159).