Core - Java Learners Guide (Module 03) PDF

Document Details

CommodiousCalcium

Uploaded by CommodiousCalcium

TATA ELXSI

2020

Tags

java programming arrays constructors Computer Science

Summary

This document is module 3 from a Core-Java learners guide, providing essential concepts on arrays and constructors. It offers practical examples using Java code.

Full Transcript

Core – Java Learning & Development Team Friday, November 6, 2020 1 Module – 3 : Arrays and constructors 2 Agenda Arrays Access Modifiers Constructors Array of Objects 3 Array Details...

Core – Java Learning & Development Team Friday, November 6, 2020 1 Module – 3 : Arrays and constructors 2 Agenda Arrays Access Modifiers Constructors Array of Objects 3 Array Details all arrays must be dynamically allocated arrays have a public, final field called length built in size field, no separate variable needed don't confuse length (capacity) with elements in use elements start with an index of zero, last index is length - 1 trying to access a non existent element results in an ArrayIndexOutOfBoundsException (AIOBE) 4 Array Initialization Array variables are object variables They hold the memory address of an array object The array must be dynamically allocated All values in the array are initialized (0, 0.0, char 0, false, or null) Arrays may be initialized with an initializer list: int[] intList = {2, 3, 5, 7, 11, 13}; double[] dList = {12.12, 0.12, 45.3}; String[] sList = {"Olivia", "Kelly", "Isabelle"}; 5 Array Declaration and allocation of memory double[] data; // declare an array data = new Double; // allocate memory //create a 2d array int[][] a = { {-10, -20, 30}, {-4, -5, 6, 9}, {70,80}, }; 6 Access Modifiers private – members are accessible with in the class only. public – members are accessible with in the class, outside the class and also outside the package. protected – members are accessible with in the class and only in the sub-class. *** these members are accessible in the sub-class even if it is in other package. Default -- members are accessible with in the class, outside the class but within the package. 7 Constructor A constructor in Java is similar to a method that is invoked when an object of the class is created. Unlike Java methods, a constructor has the same name as that of the class and does not have any return type. For example, class Test { Test() { // constructor body } } 8 Constructor Constructors are invoked implicitly when you instantiate objects. The name of the constructor should be the same as the class. A Java constructor must not have a return type. If a class doesn't have a constructor, the Java compiler automatically creates a default constructor during run-time. The default constructor initializes instance variables with default values. For example, the int variable will be initialized to 0. A constructor cannot be abstract or static or final. A constructor can be overloaded but can not be overridden. 9 Arrays of objects A native array of objects is actually a native array of object variables all object variables in Java are really what? Pointers! public void objectArrayExamples() { Rectangle[] rectList = new Rectangle; // How many Rectangle objects exist? rectList.setSize(5,10); //uh oh! for(int i = 0; i < rectList.length; i++) { rectList[i] = new Rectangle(); } rectList.setSize(100,200); } 10 References Java 1.8 documentation: https://docs.oracle.com/javase/tutorial/java/index.html Head First Java by Kathy Sierra & Bert Bates: 2nd Edition Java: The Complete Reference by Herbert Schildt Core Java: an Integrated Approach [Author: R. Nageswara Rao ] 11 Thank you For any Queries: Learning & Development Tata Elxsi Bangalore www.tataelxsi.com Confidentiality Notice This document and all information contained herein is the sole property of Tata Elxsi Limited and shall not be reproduced or disclosed to a third party without the express written consent of Tata Elxsi Limited. 12

Use Quizgecko on...
Browser
Browser