Docsity

Java Programming Concepts: Classes, Interfaces, Inheritance, and More - Prof. Tushar, Lecture notes of Java Programming

Partial preview of the text

Download Java Programming Concepts: Classes, Interfaces, Inheritance, and More - Prof. Tushar and more Lecture notes Java Programming in PDF only on Docsity!

Java Tutorial

2013-

Java program Compilation and

Interpretation

Basic Java program

// Choose a meaningful Classname. Save as "Classname.java"

public class Classname {

public static void main(String[] args) {

// Your programming statements here!

Primitive Data types

Type conversion and Type casting

Multi dimensional arrays

  • (^) int twoDArray[][] = new int[3][];
  • (^) twoDArray[0] = new int[2];
  • twoDArray[1] = new int[3];
  • (^) twoDArray[2] = new int[4];

final key word

  • (^) You can declare a class, a variable or a method to be final.
  • (^) A final class cannot be sub-classed (or extended).
  • (^) A final variable cannot be re-assigned a new value.
    • (^) A final variable of primitive type is a constant, whose value cannot be changed.
    • A final variable of a reference type (e.g., an instance of a class or an array) cannot be re- assigned a new value (reference). That is, you can modify the content of the instance, but cannot re-assign the variable to another instance.
  • (^) An instance blank final variables must be initialized in all the constructors of a class.
  • (^) Static final variable must be initialized in static block of the class.
  • (^) Final methods cannot be overridden but they can be overloaded.
  • (^) Abstract classes and interfaces cannot be marked as final.

final classes

final class BaseClass{} // Error - The type DerivedClass cannot subclass the final class BaseClass public class DerivedClass extends BaseClass{ }

Garbage Collection

GC Phases

  • (^) Mark Phase
    • (^) Marks the live objects in the heap. Typically most of the GC time is spent here.
  • (^) Sweep Phase
    • (^) Performs the actual garbage removal.
  • (^) Compaction Phase
    • (^) Compacts the heap to provide larger contiguous storage areas.

GC Overview

  • (^) GC manages the java Heap (default 512 MB)
  • (^) GC only executes
    • When objects cannot be allocated with in the heap.
    • (^) Requested via System.gc() call.
  • (^) All other threads are stopped before GC runs.

static method

  • (^) It is a method which belongs to the class and not to the object(instance)
  • (^) A static method can access only static data. It can not access non-static data (instance variables)
  • (^) A static method can call only other static methods and can not call a non-static method from it.
  • (^) A static method can be accessed directly by the class name and doesn’t need any object
  • (^) Syntax : <class-name>.<method-name>
  • (^) A static method cannot refer to “this” or “super” keywords in anyway

static block

  • (^) The static block, is a block of statement inside a Java class that will be executed when the class is loaded into memory by Class Loader of Java Virtual Machine. class Test{ static { //Code goes here } }