JAVA 3
JAVA 3
counter 10
Variables Objects
Assigning a Reference to Another
Reference
myShirt = yourShirt;
0x034009
12
15.99
B
counter 10
0x99f311
myShirt 0x99f311 12 shirtID
15.99 price
yourShirt 0x99f311
colorCode
B
Two References, One Object
Code fragment:
Shirt color: G
Array of Objects All in
• Examples: one
1 String[] names = {"Mary","Bob","Carlos"}; line
2
3 int[] ages = new int[3];
4 ages[0] = 19; Multistep
5 ages[1] = 42;
approach
6 ages[2] = 92;
int [] ages;
ages = {19, 42, 92};
Array of Objects All in
• Examples: one
1 String[] names = {"Mary","Bob","Carlos"}; line
2
3 int[] ages = new int[3];
4 ages[0] = 19;
Multistep
approach
5 ages[1] = 42;
6 ages[2] = 92;
Primitive
variable of
type int
0x034009
age
35 0 19 Primitive
ages 0x034009
42 variables of
2 92 type int held
as array
elements
• The Array variable is an object reference, not a primitive data type.
Storing Arrays of Object References
in Memory
Item item = new Item();
Item[] items = { new Item(), new Item(), new Item() };
0 itemId
item 0x034009 0.0 price
U colorCode
0 itemId
0.0 price
U colorCode
Sorting Strings
0x034009
Hello
myString 0x034009
Concatenate Strings
0x034009
"Hello"
0x99f311
myString 0x99f311
"Hello World"
Question
Given the following array declaration, which of the following
statements are true?
• int [ ] ages = new int [13];
a. ages[0] is the reference to the first element in the array.
b. ages[13] is the reference to the last element in the array.
c. There are 13 integers in the ages array.
d. ages[5] has a value of 0.
Question
Which statements are true?
a. There are eight primitive types built in to the Java programming language.
b. byte, short, char, and long are the four integral primitive data types in
the Java programming language.
c. A boolean type variable holds true, false, and nil.
d. short Long = 10; is a valid statement that adheres to the variable
declaration and initialization syntax.
Overloading Methods
• Have the same name in a class
• Have different signatures
• The number of parameters
• The types of parameters
• The order of parameters
• May have different functionality or similar functionality
• Are widely used in the foundation classes
Overloading Methods
The method
signature
• 1 public final class Calculator {
• 2 The method type
• 3 public static int sum(int num1, int num2){
• 4 System.out.println("Method One");
• 5 return num1 + num2;
• 6 }
• 7
• 8 public static float sum(float num1, float num2) {
• 9 System.out.println("Method Two");
• 10 return num1 + num2;
• 11 }
• 12 public static float sum(int num1, float num2) {
• 13 System.out.println("Method Three");
• 14 return num1 + numb2;
• 15 }
Overloading Methods
• 1 public class CalculatorTest {
• 2
• 3 public static void main(String[] args) {
• 4
• 5 int totalOne = Calculator.sum(2, 3);
• 6 System.out.println("The total is " + totalOne);
• 7
• 8 float totalTwo = Calculator.sum(15.99F, 12.85F);
• 9 System.out.println(totalTwo);
• 10
• 11 float totalThree = Calculator.sum(2, 12.85F);
• 12 System.out.println(totalThree);
• 13 }
• 14 }
Question
• Which method corresponds to the following method call?
• myPerson.printValues(100, 147.7F, "lavender");
Class
Navigato
r
Program Output
Introducing NetBeans IDE
• The code editor will tell you when you have done something wrong.
• Avoiding Syntax Problems
Introducing NetBeans IDE
• Compile Error: Variable Not Initialized
1 public static void main(String[] args){
2
3 Customer customer01; //Declare the reference
4 //No instantiation
5 customer01.name = "Robert";
9
10 }
NetBeans indicates
that the variable
may not have been
initialized.
Exercises Time!
Exercises
• Write a Java program to create a class called "Person" with a name
and age attribute. Create 3 instances of the "Person" class, 1 set its
attributes using the constructor, 2 set their attributes from user, and
print their name and age.
• https://discuss.educative.io/t/dao-vs-dto-vs-pojo/37663/2