K. Ravi Chythanya
K. Ravi Chythanya
School of Computer Science & Artificial Intelligence
Mr. K. Ravi Chythanya
Assistant Professor
 ravi.chythanya@sru.edu.in
 +91 9000 188 956
K. Ravi Chythanya
Course
Object-Oriented Programming
Concepts using JAVA
K. Ravi Chythanya
Course Outcomes
● Explain different programming structures in Java.
● Apply the concepts of object-oriented programming:
encapsulation, abstraction, inheritance, and polymorphism.
● Create GUI Based Applications.
K. Ravi Chythanya
Syllabus
Unit – I:
Overview of course, Create executable Java applications with a main method, run a Java program from the command
line, including console output, Platform independence, create if and if/else and ternary constructs, Use a switch
statement; Create and use while loops, for loop, Create and use do/while loops, Nested loops, Define the scope of
variables, object orientation, encapsulation, Abstraction etc.; Know how to read or write to object fields, Explain an
Object’s Lifecycle (creation, “dereference by reassignment” and garbage collection), Wrapper classes such as Boolean,
Double, and Integer, Use Java operators; including parentheses, String operations; Declare, instantiate, initialize and
use a one-dimensional array, multi-dimensional array.
Unit – II:
Create methods with arguments and return values, Apply the static keyword to methods and fields, Create and
overload constructors; including impact on default constructors; Apply access modifiers, Apply encapsulation principles
to a class; Constructor, destructor, Describe inheritance and its benefits; Develop code that demonstrates the use of
polymorphism, use super and this to access objects and constructors, use abstract classes and interfaces, Searching
Sorting, Stack and queue
K. Ravi Chythanya
Syllabus
Unit – III:
Differentiate among checked exceptions, unchecked exceptions, and Errors, create a try-catch block and determine
how exceptions alter normal program flow; Describe the advantages of Exception handling, create and invoke a
method that throws an exception.
Unit – IV:
Packages- Defining a Package, CLASSPATH, Access protection, importing packages. Command line arguments. Declare
and use an Array List, Vectors collection Class. Multithreading- Differences between thread-based multitasking and
process-based multitasking, Java thread model, creating threads, thread priorities, synchronizing threads, inter thread
communication.
Unit – V:
The Byte Stream: Input stream, output stream, file input stream, file output stream, print stream, Random access file,
the character streams, Buffered reader, buffered writer, Swings: JLabel and ImageIcon, JTextField, JButton, JTabded
pan, JScrolpan, Action listener, JDBC Connection with Database.
K. Ravi Chythanya
Assessment
Components of Course Evaluation Percentage
Mid Term Examination 20
End Term Examination 40
Continuous Lab Evaluation 30
Lab Exam 40
Quiz 20
Certification/Assignment 20
Project 30
K. Ravi Chythanya
Unit-I
K. Ravi Chythanya
Unit-I
Overview of course, Create executable Java applications with a main method, run a Java program
from the command line, including console output, Platform independence, create if and if/else
and ternary constructs, Use a switch statement; Create and use while loops, for loop, Create and
use do/while loops, Nested loops, Define the scope of variables, object orientation,
encapsulation, Abstraction etc.; Know how to read or write to object fields, Explain an Object’s
Lifecycle (creation, “dereference by reassignment” and garbage collection), Wrapper classes such
as Boolean, Double, and Integer, Use Java operators; including parentheses, String operations;
Declare, instantiate, initialize and use a one-dimensional array, multi-dimensional array.
K. Ravi Chythanya
Course Overview
● Java is a general-purpose programming language that is class-based, object-oriented, and
designed to have as few implementation dependencies as possible.
● It is intended to let application developers write once, run anywhere (WORA), meaning that
compiled Java code can run on all platforms that support Java without the need for
recompilation.
● Java applications are typically compiled to bytecode that can run on any Java virtual machine
(JVM) regardless of the underlying computer architecture.
● The syntax of Java is similar to C , but it has fewer low-level facilities than either of them. Java
was one of the most popular programming languages in use according to GitHub, particularly
for client-server web applications, with a reported 9 million developers.
K. Ravi Chythanya
Create executable Java applications with a main method
public class FirstProgram{
public static void main(String[] s){
System.out.println(“Hello World!”);
}
}
K. Ravi Chythanya
Run a Java program from the command line, including console output
● The command "java" is used to run the compiled bytecode, the .class need to be omitted
in java command's parameter.
● Java program calls main() method to start a Java process.
● The JVM allocate resources for the process to run.
K. Ravi Chythanya
Platform Independence
● Java is platform independent.
● In the Java programming language, all source code is first written in plain text files
ending with the .java extension.
● Those source files are then compiled into .class files by the javac compiler.
● A .class file does not contain code that is native to your processor; it instead
contains bytecodes — the machine language of the Java Virtual Machine1 (Java VM
or JVM).
● The java launcher tool then runs your application with an instance of the Java Virtual
Machine.
K. Ravi Chythanya
Platform Independence
● Java Runtime Environment (JRE) is a software package that contains what is
required to run a Java program.
● It includes a Java Virtual Machine implementation together with an implementation
of the Java Class Library.
● The Oracle Corporation, which owns the Java trademark, distributes a Java Runtime
environment with their Java Virtual Machine called HotSpot.
● Java Development Kit (JDK) is a superset of a JRE and contains tools for Java
programmers, e.g. a javac compiler.
● The Java Development Kit is provided free of charge either by Oracle Corporation
directly, or by the OpenJDK open source project, which is governed by Oracle.
K. Ravi Chythanya
Platform Independence
K. Ravi Chythanya
Create if and if/else and ternary constructs
● if
● if-else
● else-if Ladder
● switch statement
● ternery construct
K. Ravi Chythanya
Ternary constructs
● Ternary constructs or the ternary operator (? :) is the only opeator that takes three operands:
● booleanExpression ? expression1 : expression2
K. Ravi Chythanya
Use a switch statement
● Switch is one of the flow control statement, its syntax is:
switch(variable) {
case constantExpression1:
statements1;
break;
case constantExpression2:
statements2;
...
default:
statementsN;
break;
...
}
K. Ravi Chythanya
Use a switch statement
● From top to bottom, if variable matches the first constantExpression value, the
corresponding statements are then executed; after that, the flow goes to cases
downstairs without evaluating constantExpression any more.
● Any time it meets a "break;" statement, the flow then goes out of switch, skipping
all the code below.
● The default is a special case, which matches everything else the other
constantExpressions didn't cover.
● The allowed variable types includes: byte, Byte, short, Short, int, Integer, char,
Character, String, enum values. (Note there is no boolean!)
● The constantExpressions have to match variable type, and they have to be final
constant.
K. Ravi Chythanya
Create and use while loops
K. Ravi Chythanya
Create and use while loops
● The Java while loop is used to iterate a part of the program repeatedly until the
specified Boolean condition is true. As soon as the Boolean condition becomes false,
the loop automatically stops.
● The while loop is considered as a repeating if statement. If the number of iteration is
not fixed, it is recommended to use the while loop.
while (condition){
//code to be executed
increment / decrement statement
}
K. Ravi Chythanya
Create and use do/while loops
K. Ravi Chythanya
Create and use do/while loops
● The Java do-while loop is used to iterate a part of the program repeatedly, until the
specified condition is true. If the number of iteration is not fixed and you must have
to execute the loop at least once, it is recommended to use a do-while loop.
● Java do-while loop is called an exit control loop. Therefore, unlike while loop and for
loop, the do-while check the condition at the end of loop body. The Java do-while
loop is executed at least once because condition is checked after loop body.
do{
//code to be executed / loop body
//update statement
}while (condition);
K. Ravi Chythanya
Create and use for loops
K. Ravi Chythanya
Create and use for loops
● A basic for loop has the following structure:
Syntax:
for(initializing; booleanExpressions ; inc/dec) {
//body
}
● When there is only one body statement, {} can be omitted. The variable update can
happen in both update states or in the loop body.
● Loop continues as long as booleanExpressions is evaluated to true.
K. Ravi Chythanya
Create and use for each loops
● We need to iterate through an array, we can use enhanced for (for-each) loop.
Syntax:
for(datatype instance: collection) {
//body
}
● Datatype have to match the collection member's datatype, the collection have to be
an array or class implements java.lang.Iterable.
K. Ravi Chythanya
Define scope of variables
● There are three types of variables:
○ Local variables are defined inside a function or code block, they can not be accessed
outside the method or code block it is defined.
○ Instance variables are also called fields, they are defined in the class itself and not in
constructor or methods. Instance variables have the same lifetime as the object it is
belongs to. Each instance have its own copy of the instance variable.
○ Class variables has the keyword static before it. Class variables are shared among all
objects of the class. Class variable have the same lifetime as the java program.
● Local variables need to be initialized before using, instance variables and class variables
don't need programer to initialize.
K. Ravi Chythanya
General Form of a Class
class ClassName {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodName1(parameter-list) {
// body of method
}
// ...
type methodNameN(parameter-list) {
// body of method
}
}
K. Ravi Chythanya
A Simple Class
● As stated, a class defines a new type of data.
● In this case, the new data type we are creating is called Box.
● You will use this name to declare objects of type Box. It is important to remember
that a class declaration only creates a template; it does not create an actual object.
● Thus, the preceding code does not cause any objects of type Box to come into
existence.
● Ex: class Box {
double width;
double height;
double depth;
}
K. Ravi Chythanya
Declaring Objects
● As just explained, when you create a class, you are creating a new data type.
● You can use this type to declare objects of that type.
● However, obtaining objects of a class is a two-step process.
● First, you must declare a variable of the class type. This variable does not define an
object.
● Instead, it is simply a variable that can refer to an object.
● Second, you must acquire an actual, physical copy of the object and assign it to that
variable. You can do this using the new operator.
Box box1 = new Box();
K. Ravi Chythanya
A Closer Look at new
● As just explained, the new operator dynamically allocates memory for an object. It
has this general form:
● class-var = new ClassName ( );
K. Ravi Chythanya
Object's Lifecycle
● Object is an instance of a class.
● We create an object with keyword new.
K. Ravi Chythanya
Object's Lifecycle
● Date d = new Date();
● In this statement, Date d is a reference variable refers to the object created, new
Date() creates the object.
● Objects are stored in java program's memory heap.
● Java have a process called garbage collection -- for those object no longer reachable,
they are automatically deleted from memory heap.
● An object became unreachable when there is no reference variable refers to it or the
object run out of scope.
● As programmer, we can not control when garbage collection will happen. The
statement System.gc(); only suggests JVM this is a good time to collect garbage.
K. Ravi Chythanya
Object's Lifecycle
● Function protected void finalize() is called once and only once when the object is garbage
collected.
● OCA tests objects' lifecycle, it is easier to answer those questions if we trace reference variables
by drawing links to objects.ructor, it is a special method that creates a new object.
K. Ravi Chythanya
Wrapper classes
● Each java primitive type has a corresponding wrapper class.
● Constructing a wrapper class creates an object.
● You don't have to construct a wrapper class before using, because java support auto-boxing --
wherever a method or constructor expect a wrapper class, you can pass in the corresponding
primitive type data, java will automatically convert the primitive type into the wrapping class.
K. Ravi Chythanya
Use Java operators; including parentheses
● Operator in Java is a symbol that is used to perform operations. There are many types of
operators in Java which are given below:
○ Unary Operator,
○ Arithmetic Operator,
○ Shift Operator,
○ Relational Operator,
○ Bitwise Operator,
○ Logical Operator,
○ Ternary Operator and
○ Assignment Operator.
K. Ravi Chythanya
String Operations
● String is a sequence of characters. But in Java, string is an object that represents a sequence of
characters. The java.lang.String class is used to create a string object.
● Java String class provides a lot of methods to perform operations on strings such as compare(),
concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.
● The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.
K. Ravi Chythanya
Declare, instantiate, initialize and use a one-dimensional array
K. Ravi Chythanya
Declare, instantiate, initialize and use a one-dimensional array
● An array is an ordered list with fixed size. The most common ways to create an array
are:
○ type[] variableName = new type[size];
○ type variableName[] = new type[size];
● Array have fixed size, so you have to specify array size or give initial values (which
specifies size implicitly).
● When you don't give initial values, the default values are given.
● Though Array's size can not change, array's contents can change, so array is mutable.
● Array is an object (although weird looking).
● That means array can be initialized with new method, it extends object class, and has
methods.
K. Ravi Chythanya
Declare, instantiate, initialize and use a multi-dimensional array
● The array is an ordered list of objects or primitives. The objects stored in an array can be type of array.
● For example int [2] [5] twoD; declares a matrix-like 2-D array named twoD.
● The first dimension have 2 elements. Each element have type int array, which is an object reference variable
pointing to an int [5] array object.

OOPC_Unit-I.pdf

  • 1.
  • 2.
    K. Ravi Chythanya Schoolof Computer Science & Artificial Intelligence Mr. K. Ravi Chythanya Assistant Professor  ravi.chythanya@sru.edu.in  +91 9000 188 956
  • 3.
    K. Ravi Chythanya Course Object-OrientedProgramming Concepts using JAVA
  • 4.
    K. Ravi Chythanya CourseOutcomes ● Explain different programming structures in Java. ● Apply the concepts of object-oriented programming: encapsulation, abstraction, inheritance, and polymorphism. ● Create GUI Based Applications.
  • 5.
    K. Ravi Chythanya Syllabus Unit– I: Overview of course, Create executable Java applications with a main method, run a Java program from the command line, including console output, Platform independence, create if and if/else and ternary constructs, Use a switch statement; Create and use while loops, for loop, Create and use do/while loops, Nested loops, Define the scope of variables, object orientation, encapsulation, Abstraction etc.; Know how to read or write to object fields, Explain an Object’s Lifecycle (creation, “dereference by reassignment” and garbage collection), Wrapper classes such as Boolean, Double, and Integer, Use Java operators; including parentheses, String operations; Declare, instantiate, initialize and use a one-dimensional array, multi-dimensional array. Unit – II: Create methods with arguments and return values, Apply the static keyword to methods and fields, Create and overload constructors; including impact on default constructors; Apply access modifiers, Apply encapsulation principles to a class; Constructor, destructor, Describe inheritance and its benefits; Develop code that demonstrates the use of polymorphism, use super and this to access objects and constructors, use abstract classes and interfaces, Searching Sorting, Stack and queue
  • 6.
    K. Ravi Chythanya Syllabus Unit– III: Differentiate among checked exceptions, unchecked exceptions, and Errors, create a try-catch block and determine how exceptions alter normal program flow; Describe the advantages of Exception handling, create and invoke a method that throws an exception. Unit – IV: Packages- Defining a Package, CLASSPATH, Access protection, importing packages. Command line arguments. Declare and use an Array List, Vectors collection Class. Multithreading- Differences between thread-based multitasking and process-based multitasking, Java thread model, creating threads, thread priorities, synchronizing threads, inter thread communication. Unit – V: The Byte Stream: Input stream, output stream, file input stream, file output stream, print stream, Random access file, the character streams, Buffered reader, buffered writer, Swings: JLabel and ImageIcon, JTextField, JButton, JTabded pan, JScrolpan, Action listener, JDBC Connection with Database.
  • 7.
    K. Ravi Chythanya Assessment Componentsof Course Evaluation Percentage Mid Term Examination 20 End Term Examination 40 Continuous Lab Evaluation 30 Lab Exam 40 Quiz 20 Certification/Assignment 20 Project 30
  • 8.
  • 9.
    K. Ravi Chythanya Unit-I Overviewof course, Create executable Java applications with a main method, run a Java program from the command line, including console output, Platform independence, create if and if/else and ternary constructs, Use a switch statement; Create and use while loops, for loop, Create and use do/while loops, Nested loops, Define the scope of variables, object orientation, encapsulation, Abstraction etc.; Know how to read or write to object fields, Explain an Object’s Lifecycle (creation, “dereference by reassignment” and garbage collection), Wrapper classes such as Boolean, Double, and Integer, Use Java operators; including parentheses, String operations; Declare, instantiate, initialize and use a one-dimensional array, multi-dimensional array.
  • 10.
    K. Ravi Chythanya CourseOverview ● Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible. ● It is intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. ● Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of the underlying computer architecture. ● The syntax of Java is similar to C , but it has fewer low-level facilities than either of them. Java was one of the most popular programming languages in use according to GitHub, particularly for client-server web applications, with a reported 9 million developers.
  • 11.
    K. Ravi Chythanya Createexecutable Java applications with a main method public class FirstProgram{ public static void main(String[] s){ System.out.println(“Hello World!”); } }
  • 12.
    K. Ravi Chythanya Runa Java program from the command line, including console output ● The command "java" is used to run the compiled bytecode, the .class need to be omitted in java command's parameter. ● Java program calls main() method to start a Java process. ● The JVM allocate resources for the process to run.
  • 13.
    K. Ravi Chythanya PlatformIndependence ● Java is platform independent. ● In the Java programming language, all source code is first written in plain text files ending with the .java extension. ● Those source files are then compiled into .class files by the javac compiler. ● A .class file does not contain code that is native to your processor; it instead contains bytecodes — the machine language of the Java Virtual Machine1 (Java VM or JVM). ● The java launcher tool then runs your application with an instance of the Java Virtual Machine.
  • 14.
    K. Ravi Chythanya PlatformIndependence ● Java Runtime Environment (JRE) is a software package that contains what is required to run a Java program. ● It includes a Java Virtual Machine implementation together with an implementation of the Java Class Library. ● The Oracle Corporation, which owns the Java trademark, distributes a Java Runtime environment with their Java Virtual Machine called HotSpot. ● Java Development Kit (JDK) is a superset of a JRE and contains tools for Java programmers, e.g. a javac compiler. ● The Java Development Kit is provided free of charge either by Oracle Corporation directly, or by the OpenJDK open source project, which is governed by Oracle.
  • 15.
  • 16.
    K. Ravi Chythanya Createif and if/else and ternary constructs ● if ● if-else ● else-if Ladder ● switch statement ● ternery construct
  • 17.
    K. Ravi Chythanya Ternaryconstructs ● Ternary constructs or the ternary operator (? :) is the only opeator that takes three operands: ● booleanExpression ? expression1 : expression2
  • 18.
    K. Ravi Chythanya Usea switch statement ● Switch is one of the flow control statement, its syntax is: switch(variable) { case constantExpression1: statements1; break; case constantExpression2: statements2; ... default: statementsN; break; ... }
  • 19.
    K. Ravi Chythanya Usea switch statement ● From top to bottom, if variable matches the first constantExpression value, the corresponding statements are then executed; after that, the flow goes to cases downstairs without evaluating constantExpression any more. ● Any time it meets a "break;" statement, the flow then goes out of switch, skipping all the code below. ● The default is a special case, which matches everything else the other constantExpressions didn't cover. ● The allowed variable types includes: byte, Byte, short, Short, int, Integer, char, Character, String, enum values. (Note there is no boolean!) ● The constantExpressions have to match variable type, and they have to be final constant.
  • 20.
    K. Ravi Chythanya Createand use while loops
  • 21.
    K. Ravi Chythanya Createand use while loops ● The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops. ● The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is recommended to use the while loop. while (condition){ //code to be executed increment / decrement statement }
  • 22.
    K. Ravi Chythanya Createand use do/while loops
  • 23.
    K. Ravi Chythanya Createand use do/while loops ● The Java do-while loop is used to iterate a part of the program repeatedly, until the specified condition is true. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use a do-while loop. ● Java do-while loop is called an exit control loop. Therefore, unlike while loop and for loop, the do-while check the condition at the end of loop body. The Java do-while loop is executed at least once because condition is checked after loop body. do{ //code to be executed / loop body //update statement }while (condition);
  • 24.
    K. Ravi Chythanya Createand use for loops
  • 25.
    K. Ravi Chythanya Createand use for loops ● A basic for loop has the following structure: Syntax: for(initializing; booleanExpressions ; inc/dec) { //body } ● When there is only one body statement, {} can be omitted. The variable update can happen in both update states or in the loop body. ● Loop continues as long as booleanExpressions is evaluated to true.
  • 26.
    K. Ravi Chythanya Createand use for each loops ● We need to iterate through an array, we can use enhanced for (for-each) loop. Syntax: for(datatype instance: collection) { //body } ● Datatype have to match the collection member's datatype, the collection have to be an array or class implements java.lang.Iterable.
  • 27.
    K. Ravi Chythanya Definescope of variables ● There are three types of variables: ○ Local variables are defined inside a function or code block, they can not be accessed outside the method or code block it is defined. ○ Instance variables are also called fields, they are defined in the class itself and not in constructor or methods. Instance variables have the same lifetime as the object it is belongs to. Each instance have its own copy of the instance variable. ○ Class variables has the keyword static before it. Class variables are shared among all objects of the class. Class variable have the same lifetime as the java program. ● Local variables need to be initialized before using, instance variables and class variables don't need programer to initialize.
  • 28.
    K. Ravi Chythanya GeneralForm of a Class class ClassName { type instance-variable1; type instance-variable2; // ... type instance-variableN; type methodName1(parameter-list) { // body of method } // ... type methodNameN(parameter-list) { // body of method } }
  • 29.
    K. Ravi Chythanya ASimple Class ● As stated, a class defines a new type of data. ● In this case, the new data type we are creating is called Box. ● You will use this name to declare objects of type Box. It is important to remember that a class declaration only creates a template; it does not create an actual object. ● Thus, the preceding code does not cause any objects of type Box to come into existence. ● Ex: class Box { double width; double height; double depth; }
  • 30.
    K. Ravi Chythanya DeclaringObjects ● As just explained, when you create a class, you are creating a new data type. ● You can use this type to declare objects of that type. ● However, obtaining objects of a class is a two-step process. ● First, you must declare a variable of the class type. This variable does not define an object. ● Instead, it is simply a variable that can refer to an object. ● Second, you must acquire an actual, physical copy of the object and assign it to that variable. You can do this using the new operator. Box box1 = new Box();
  • 31.
    K. Ravi Chythanya ACloser Look at new ● As just explained, the new operator dynamically allocates memory for an object. It has this general form: ● class-var = new ClassName ( );
  • 32.
    K. Ravi Chythanya Object'sLifecycle ● Object is an instance of a class. ● We create an object with keyword new.
  • 33.
    K. Ravi Chythanya Object'sLifecycle ● Date d = new Date(); ● In this statement, Date d is a reference variable refers to the object created, new Date() creates the object. ● Objects are stored in java program's memory heap. ● Java have a process called garbage collection -- for those object no longer reachable, they are automatically deleted from memory heap. ● An object became unreachable when there is no reference variable refers to it or the object run out of scope. ● As programmer, we can not control when garbage collection will happen. The statement System.gc(); only suggests JVM this is a good time to collect garbage.
  • 34.
    K. Ravi Chythanya Object'sLifecycle ● Function protected void finalize() is called once and only once when the object is garbage collected. ● OCA tests objects' lifecycle, it is easier to answer those questions if we trace reference variables by drawing links to objects.ructor, it is a special method that creates a new object.
  • 35.
    K. Ravi Chythanya Wrapperclasses ● Each java primitive type has a corresponding wrapper class. ● Constructing a wrapper class creates an object. ● You don't have to construct a wrapper class before using, because java support auto-boxing -- wherever a method or constructor expect a wrapper class, you can pass in the corresponding primitive type data, java will automatically convert the primitive type into the wrapping class.
  • 36.
    K. Ravi Chythanya UseJava operators; including parentheses ● Operator in Java is a symbol that is used to perform operations. There are many types of operators in Java which are given below: ○ Unary Operator, ○ Arithmetic Operator, ○ Shift Operator, ○ Relational Operator, ○ Bitwise Operator, ○ Logical Operator, ○ Ternary Operator and ○ Assignment Operator.
  • 37.
    K. Ravi Chythanya StringOperations ● String is a sequence of characters. But in Java, string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object. ● Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc. ● The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.
  • 38.
    K. Ravi Chythanya Declare,instantiate, initialize and use a one-dimensional array
  • 39.
    K. Ravi Chythanya Declare,instantiate, initialize and use a one-dimensional array ● An array is an ordered list with fixed size. The most common ways to create an array are: ○ type[] variableName = new type[size]; ○ type variableName[] = new type[size]; ● Array have fixed size, so you have to specify array size or give initial values (which specifies size implicitly). ● When you don't give initial values, the default values are given. ● Though Array's size can not change, array's contents can change, so array is mutable. ● Array is an object (although weird looking). ● That means array can be initialized with new method, it extends object class, and has methods.
  • 40.
    K. Ravi Chythanya Declare,instantiate, initialize and use a multi-dimensional array ● The array is an ordered list of objects or primitives. The objects stored in an array can be type of array. ● For example int [2] [5] twoD; declares a matrix-like 2-D array named twoD. ● The first dimension have 2 elements. Each element have type int array, which is an object reference variable pointing to an int [5] array object.