0% found this document useful (0 votes)
2 views1 page

Array of Objects - Java Tutorial - Java With Us(1)

The document provides a tutorial on creating and manipulating arrays of objects in Java, specifically using a Student class. It explains how to instantiate Student objects, store them in an array, and modify their properties while highlighting potential runtime errors like NullPointerException. The tutorial also discusses the differences between handling arrays of objects and primitive types, emphasizing the use of reference types in Java.

Uploaded by

nigel195314150
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Array of Objects - Java Tutorial - Java With Us(1)

The document provides a tutorial on creating and manipulating arrays of objects in Java, specifically using a Student class. It explains how to instantiate Student objects, store them in an array, and modify their properties while highlighting potential runtime errors like NullPointerException. The tutorial also discusses the differences between handling arrays of objects and primitive types, emphasizing the use of reference types in Java.

Uploaded by

nigel195314150
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Java With Us

Home | Tutorial | Programs

Java Tutorial Array of Objects


Introduction to Java As we have already said, arrays are capable of storing objects also. For example, we can create an array
Hello World Program of Strings which is a reference type variable. However, using a String as a reference type to illustrate the
Variables and Data types concept of array of objects isn't too appropriate due to the immutability of String objects. Therefore, for
More about data types this purpose, we will use a class Student containing a single instance variable marks. Following is the
Displaying text using print and definition of this class.
println
class Student {
Displaying text using printf
int marks;
Java Comments }
Naming conventions for Identifiers
Mathematical operations in Java An array of objects is created just like an array of primitive type data items in the following way.
Taking input from the user
Student[] studentArray = new Student[7];

Classes and Objects The above statement creates the array which can hold references to seven Student objects. It doesn't
Introduction to object oriented create the Student objects themselves. They have to be created separately using the constructor of the
programming Student class. The studentArray contains seven memory spaces in which the address of seven Student
The constituents of a Class objects may be stored. If we try to access the Student objects even before creating them, run time errors
would occur. For instance, the following statement throws a NullPointerException during runtime which
Creating objects and calling
indicates that studentArray[0] isn't yet pointing to a Student object.
methods
Get and Set Methods studentArray[0].marks = 100;
Default constructor provided by
the compiler The Student objects have to be instantiated using the constructor of the Student class and their
references should be assigned to the array elements in the following way.
Access Specfiers
Scope and lifetime of Variables
studentArray[0] = new Student();
Call by value and Call by Reference
In this way, we create the other Student objects also. If each of the Student objects have to be created
A few more topics using a different constructor, we use a statement similar to the above several times. However, in this
Casting particular case, we may use a for loop since all Student objects are created with the same default
constructor.
Class as a reference data type
Constants or literals for ( int i=0; i<studentArray.length; i++) {
Final variables studentArray[i]=new Student();
Increment and decrement }
operators
The above for loop creates seven Student objects and assigns their reference to the array elements.
Manipulating Strings Now, a statement like the following would be valid.
Operators
Overloading constructors and studentArray[0].marks=100;
methods
Static methods and variables Enhanced for loops find a better application here as we not only get the Student object but also we are
capable of modifying it. This is because of the fact that Student is a reference type. Therefore the
The Java API
variable in the header of the enhanced for loop would be storing a reference to the Student object and
The Math class not a copy of the Student object which was the case when primitive type variables like int were used as
this keyword array elements.
Wrapper classes
for ( Student x : studentArray ) {
x.marks = s.nextInt(); // s is a Scanner object
Control Structures
}
Control Statements
Repetition statements Recall that we were not able to assign to the array elements in a similar way when the array was of type
Nested loops int.
Formulating algorithms
Branching Statements Moreover, in the case of array of objects, when we pass an array element to a method, the object is
susceptible to changes. This is because the element being passed is also a reference type item. This
differs from the situation when we have an int array. Following illustrates this concept.
Arrays
Arrays introduction public static void main(String[] args) {
Processing arrays using 1oops Student[] studentArray = new Student[7];
Searching and sorting arrays studentArray[0] = new Student();
studentArray[0].marks = 99;
Array of objects
System.out.println(studentArray[0].marks); // prints 99
Multi dimensional arrays modify(studentArray[0]);
Taking input as command line System.out.println(studentArray[0].marks); // prints 100 and not 99
arguments // code
}
Using ellipsis to accept variable
number of arguments public static void modify(Student s) {
s.marks = 100;
}
Inheritance
Inheritance introduction
Compare the output with the one when the array was of type int[].
Relation between a super class
and sub class Processing an array of objects is much similar to the processing of an array of primitive type. the only
Final classes and methods thing to be kept in mind is the possibility of NullPointerException being thrown during run time and also
The protected access specifier remembering that the array elements themselves are reference types, which brings subtle differences
Class Object from the case when they are passed as parameters. Moreover, an enhanced for loop may be used to
initialise the array elements.

Polymorphism Next : Multi dimensional arrays


Introduction Prev : Searching and sorting arrays
Interfaces
Packages
Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions

Java With Us
4,3 rb suka

Sukai Halaman

Jadilah orang pertama yang menyukai


ini.

Privacy Policy

You might also like