Java Programming Fundamentals PDF
Document Details
Uploaded by Deleted User
GITAM
Ranajit Senko
Tags
Summary
This document provides an introduction to Java classes, objects, and methods. It covers various aspects of object-oriented concepts in Java. The document is a comprehensive guide covering primitive types and more for beginners.
Full Transcript
Introduction to Classes, Objects and Methods By Ranajit Senko Department of Computer Science and Engineering Topics: 1. Class Fundamentals, Objects creation, Reference Variables and Ass...
Introduction to Classes, Objects and Methods By Ranajit Senko Department of Computer Science and Engineering Topics: 1. Class Fundamentals, Objects creation, Reference Variables and Assignment 2. Methods, Returning a Value, Using Parameters 3. Constructors, Parameterized Constructors, new Operator, this Keyword, finalize() method 4. Wrapper Classes, Parsing, Auto boxing and Unboxing 5. I/O: Command-Line Arguments, Scanner and Buffered Reader Classes, Department of CSE Syntax of Class Class Fundamentals class classname { type instance-variable1; The entire Java language is built in with classes, because it type instance-variable2; //... defines the shape and nature of the Object. type instance-variableN; A class is a user defined data type. Once the class is defined, type methodname1(parameter-list) { the ‘new’ type can be used to create Object of that type. // body of method } A class is a template for an object, and an object is an type methodname2(parameter-list) { instance of a class. // body of method } When you define a class, you can declare its exact form and //... nature. type methodnameN(parameter-list) { // body of method A class is declared by the use of the “class” keyword. } } The data or the variable defined within the class are called “Instance Variables”. The code is contained within methods. the methods and variables defined within a class are called members of the class. Department of CSE A Simple Class class Box { double width; double height; double depth; // create a class named Box } class Box { // This class declares an object of type Box. class BoxDemo { double double width; width; public static void main(String args[]) { double height; Box mybox = new Box(); double height; double depth; double vol; } double depth; // assign values to mybox's instance variables mybox.width = 10; } mybox.height = 20; mybox.depth = 15; Box mybox = new Box(); // compute volume of box vol = mybox.width * mybox.height * mybox.depth; System.out.println("Volume is " + vol); mybox.width = 100; } } Department of CSE Object Creation Box mybox = new Box(); Box mybox; // declare reference to object mybox = new Box(); // allocate a Box object Department of CSE Assigning Object Reference Variables Box b1 = new Box(); Box b2 = b1; Department of CSE Methods Classes usually consist of two things: instance variables and methods. type method_name(parameter-list) { // body of method } Department of CSE class BoxDemo3 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); // This program includes a method inside the box // assign values to mybox1's instance variables class. mybox1.width = 10; class Box { mybox1.height = 20; double width; mybox1.depth = 15; double height; // display volume of a box mybox2.width = 3; void volume() { mybox2.height = 6; System.out.print("Volume is "); mybox2.depth = 9; System.out.println(width * height * depth); // display volume of first box } mybox1.volume(); } // display volume of second box mybox2.volume(); } Department of CSE } class BoxDemo3 { Returning a Value public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // This program includes a method inside the // assign values to mybox1's instance variables box class. mybox1.width = 10; class Box { mybox1.height = 20; double width; mybox1.depth = 15; double depth; mybox2.width = 3; // display volume of a box mybox2.height = 6; double volume() { mybox2.depth = 9; return width * height * depth; // get volume of first box vol = mybox1.volume(); } System.out.println("Volume is " + vol); } // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } Department of CSE } Method without parameters Method with parameters int square() { int square(int i) return 10 * 10; { } return i * i; } int x; x = square(5); // x equals 25 x = square(9); // x equals 81 Department of CSE // This program uses a parameterized method. class BoxDemo5 { class Box { public static void main(String args[]) { double width; Box mybox1 = new Box(); double height; Box mybox2 = new Box(); double depth; double vol; // compute and return volume // initialize each box double volume() { mybox1.setDim(10, 20, 15); return width * height * depth; mybox2.setDim(3, 6, 9); } // get volume of first box // sets dimensions of box vol = mybox1.volume(); void setDim(double w, double h, double d) { System.out.println("Volume is " + vol); width = w; // get volume of second box height = h; vol = mybox2.volume(); depth = d; System.out.println("Volume is " + vol); } } } } Department of CSE A constructor is a special method whose task is to initialize the objects of its class. It is a special method because its name is same as the class name. The constructor is syntactically similar to a method. Once the constructor is defined it automatically called after the object is created before the new operator completes. These constructors have no return type, even void also. Two types of constructors: Default Constructor and Parameterized Constructor Default Constructor: A constructor that accepts no parameters is called the “Default Constructor”. The Default constructor automatically initializes all instance variable to zero(0). If you don’t implement any constructor in your class, the Java compiler inserts default constructor into your code on your behalf. You will not see the default constructor in your source code(the.java file) as it is inserted during compilation and present in the bytecode (.class file). The parameterized constructors are the constructors having a specific number of arguments to be passed. The purpose of a parameterized constructor is to assign user-wanted specific values to the instance variables of different objects. A parameterized constructor is written explicitly by a programmer. Department of CSE class Box { double width; class BoxDemo6 { double height; public static void main(String args[]) { double depth; Box mybox1 = new Box(); // This is the constructor for Box. Box mybox2 = new Box(); Box() { double vol; System.out.println("Constructing Box"); // get volume of first box width = 10; vol = mybox1.volume(); height = 10; System.out.println("Volume is " + vol); depth = 10; // get volume of second box } vol = mybox2.volume(); // compute and return volume System.out.println("Volume is " + vol); double volume() { } return width * height * depth; } } } Department of CSE class Box { double width; class BoxDemo7 { double height; public static void main(String args[]) { double depth; Box mybox1 = new Box(10, 20, 15); // This is the constructor for Box. Box mybox2 = new Box(3, 6, 9); Box(double w, double h, double d) { double vol; width = w; // get volume of first box height = h; vol = mybox1.volume(); depth = d; System.out.println("Volume is " + vol); } // get volume of second box // compute and return volume vol = mybox2.volume(); double volume() { System.out.println("Volume is " + vol); return width * height * depth; } } } } Department of CSE The this Keyword this can be used inside any method to refer to the current object. class Sample { class Sample { int n1; int n1; int n2; int n2; Sample(int n1, int n2) { Sample(int n1, int n2) { n1=n1; this.n1=n1; n2=n2; this.n2=n2; } } void display() { void display() { System.out.println(n1+” ”+n2); System.out.println(n1+” ”+n2); } } } } Department of CSE Garbage Collection when no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed. finalize( ) method Sometimes an object will need to perform some action when it is destroyed. protected void finalize( ) { // finalization code here } Department of CSE Wrapper Classes The wrapper classes in Java are used to convert primitive types into corresponding objects and objects into primitive types. Primitive types Wrapper classes boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double Department of CSE Autoboxing and Unboxing Autoboxing: Primitive Type to Wrapper Object Two ways of autoboxing: using constructor and using valueOf() method. Example: Integer obj = new Integer(10); Integer obj = Integer.valueOf(10); Unboxing: Wrapper Objects to Primitive Types It can be done using corresponding value() method. Example: obj.intValue(), obj.doubleValue(), etc. Example: int i = obj.intValue(); Department of CSE Parsing The parse() method receives some string as input, "extracts" the necessary information from it. Example: Integer.parseInt() to convert string into int Double.parseDouble() to convert string into double Department of CSE Java Command Line Arguments The java command-line argument is an argument i.e. passed at the time of running the java program. class CmdDemo{ public static void main(String args[]){ int count = args.length; for(int i=0;i