Skip to main content

1. Introduction to Java Basics

 

Java Program

A java program is mostly a collection of objects talking to other objects by invoking each other's methods. Every object is of certain type, and that type is defined by a class or interface. Most java programs use a collection of objects of many different types.

Some Basic Terms

Class

A template that describes the kinds of state and behaviour that objects of its type support.

Object

At runtime, when 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 behaviours defined by its class.

State (instance variables)

Each object (instance of class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state.

Behavior (methods)

Methods are where the class's logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.

Identifiers

All the Java components we just talked about - classes, variables, and methods need names. In Java, these names are called identifiers.

Technically, legal identifiers must be composed of only Unicode characters, numbers, currency symbols, and connecting characters (like underscores).

  • Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore (_). Identifiers cannot start with a number!
  • After the first character, identifiers can also include digits.
  • Identifiers can be of any length.
  • We can't use a Java keyword as an identifier.
  • Identifiers in Java are case-sensitive; deep and Deep are two different identifiers.

Keywords

Like all programming languages, Java has a set of built-in keywords. These keywords must not be used as identifiers.

abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends final finally float
for goto if implements import
instanceof int interface long native
new package private protected public
return short static strictfp super
switch synchronized this throw throws
transient try void volatile while

Source File Declaration Rules

  • There can be at most one public class per source code file.
  • Comments can appear at the beginning or end of any line in the source code file, they are independent of any of the positioning rules.
  • If the source file contains a public class, the filename must match the public class name.
  • A file can have only package statement, but multiple imports.
  • The package statement (if any) must be the first (non-comment) line in source file.
  • The import statements (if any) must come after the package (if any) statement and before class or enum or interface declaration.
  • If there is no package statement, import statements must be the first (non-comment) statements in the source file.
  • A file can have more than one non-public class.
  • Files with no public classes have no naming restrictions.
Prev Next

Comments