Core Java Basics

Evolution of Java

1. Smalltalk was the first object-oriented language. Hence, it had a major influence on the design of Java.
2. Java is a programming language from Sun Microsystems.
3. It was developed by:

  • James Gosling
  • Patrick Naughton
  • Chris Warth
  • Ed Frank
  • Frank Sheridan

4. Need for Java

  • Platform Independence
  • WWW (Internet)
  • High Performance

The Java Architecture

• Write the Source Code

• Compile to standardized portable binary format – bytecode, contained in .class files

• Bytecode is highly optimized set of instructions

• Run the class files on JVM which acts as an interpreter for bytecode

Java Architecture

Life Cycle of Java Code

java code life-cycle

Variables In Java

There are two types of variables in Java:
Primitives: A primitive can be one of eight types:

  • Char
  • Boolean
  • Byte
  • Short
  • Int
  • Long
  • Double
  • Float

Reference variables: A reference variable is used to refer to (or access) an object.

Variable Declaration

Primitive variables can be declared as class variables (statics), instance variables, method parameters, or local variables.
Examples of primitive variable declarations:

 byte b;
	boolean myBooleanPrimitive;
	int x, y, z; // declare three int primitives

Reference variables can be declared as static variables, instance variables, method parameters, or local variables.
Examples of reference variable declarations:

Object o;
	Dog myNewDogReferenceVariable;
	String s1, s2, s3; // declare three String vars

What Is An Object?

• Real-world objects share two characteristics: They all have state and behavior. For Example : Dogs have state (name, color, breed) and behavior (barking, fetching, wagging tail). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
• Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object’s internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object methods is known as data encapsulation — a fundamental principle of object-oriented programming.

Creating An Object In Java

At runtime, when the Java Virtual Machine (JVM) encounters the “new” keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class.

       Syntax :
		Dog dogExample = new Dog();

What is a class ?

A Class is a template that describes the kinds of state and behavior that objects of its type support.

What is Constructor ?

Objects are constructed. You can’t make a new object without invoking a constructor. In fact, you can’t make a new object without invoking not just the constructor of the object’s actual class type, but also the constructor of each of its superclasses! Constructors are the code that runs whenever you use the keyword new.
Every class, including abstract classes, MUST have a constructor.

  • But just because a class must have one, doesn’t mean the programmer has to type it. A constructor looks like this:
   class Foo {
         Foo() { } // The constructor for the Foo class
    }
  • Notice what’s missing? There’s no return type! Two key points to remember about constructors are that they have no return type and their names must exactly match the class name.
    Take a look at the following example :
  class Foo {
	int size;
	String name;
	Foo(String name, int size) {
		this.name = name;
		this.size = size;
	}
     }
  • In the preceding code example, the Foo class does not have a no-arg constructor. That means the following will fail to compile:
     Foo f = new Foo(); // Won't compile, no matching constructor
        but the following will compile:
	Foo f = new Foo("Fred", 43); // No problem. Arguments match
	          		    // the Foo constructor.

  • So it’s very common (and desirable) for a class to have a no-arg constructor , regardless of how many other overloaded constructors are in the class (yes, constructors can be overloaded). You can’t always make that work for your classes; occasionally you have a class where it makes no sense to create an instance without supplying information to the constructor. A java.awt.Color object, for example, can’t be created by calling a no-arg constructor, because that would be like saying to the JVM, “Make me a new Color object, and I really don’t care what color it is…you pick.”
constructor
constructor
  • The figure shows the constructor call hierarchy when the object of the subclass Horse is invoked (Horse class extends Animal class).