Chapter 1 – Java Building Blocks

To compile the java file

  • javac – it creates .class file(zoo)

To execute the java file

  • java zoo

Understanding package declaration and imports

Packages – logical grouping of classes. If the package is not imported, it shows following error message,

  • cannot be resolved to a type

import – this statement tells the compiler which packages to look into to find a class

wildcards –  import java.util.*;

  • * – is a wildcard that matches all classes in the package not sub-package classes

static import – feature of Java 5 used to access any static member of a class directly. There is no need to qualify it by the class name.

  1. importstaticlang.System.*;
  2. classStaticImportExample{
  3. public static void main(String args[]){
  4. //Now no need of System.out
  5.            println(“Hello”);
  6.            println(“World”);
  7. }
  8. }

import java.nio.*.* – we can have only one wildcard and it must be at the end.

import java.nio.files.paths – we cannot import methods, but we can import only classes

Naming Conflicts

Below 2 lines does not compile since the type Date is present in both the packages,

import java.util.*;

import java.sql.*;

Below 2 lines work, since class Date is explicitly imported. So, it takes precedence over wildcards.

import java.util.Date;

import java.sql.*;

Code formatting on the exam

  • Not all the questions will include the imports


# Same as class name

# No return type

# If there is no constructor, compiler will supply a “do nothing” default constructor.

# A constructor is called when we write new followed by the name of the class we want to instantiate.

  • new Dog();

Initializer Blocks
# Fields & Initializer blocks are run in the order in which they appear

# Constructor runs after all fields & initializer blocks have run

public class Chick {

private String name = “Janani”;


System.out.println(“Setting field”);


public Chick() {

name = “Velmurugan”;

System.out.println(“Constructor invoked”);


public static void main(String[] args) {

Chick chick = new Chick();





Setting field

Constructor invoked


Difference between reference objects and primitives

Primitive Types

# Java applications contain two types of data: primitive types and reference types

=> Octal (digits 0–7) – number 0 as a prefix—for example, 017

=> Hexadecimal (digits 0–9 and letters A–F) – number 0 followed by x or X as a prefix—for example, 0xFF

=> Binary (digits 0–1) – number 0 followed by b or B as a prefix—for example, 0b10

Reference Types

# refers to an object (an instance of a class).

Key Differences

Reference Type Primitive Type
Can be assigned null Compiler error when assigned with null
Call methods when they do not point to null Do not have methods declared on them
Java classes or classes we create begin with uppercase Have lowercase type names



Garbage Collection

# refers to the process of automatically freeing memory on the heap by deleting objects that are no longer reachable in your program

# System.gc() is not guaranteed to run – Java is free to ignore the request

An object is no longer reachable when one of two situations occurs:

■ The object no longer has any references pointing to it.

■ All references to the object have gone out of scope.

# finalize() call could run zero or one time.

Command Line Aruguments

# arrays index starts with 0(zero)

# string with spaces must be specified in quotes

Ex: java bird sparrow “blue jay”



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s