Java Terminology: Part 1 - The Basics

A Cheat sheet to understand the JAVA language better

Java Terminology: Part 1 - The Basics

Created in 1995, Java, was developed to overcome the flaws of modular programming. Java is used in various sectors like internet security, Android Development, Web Development, and more.

Java is

  • Object-Oriented Programming Language
  • Open Source
  • Platform-neutral
  • Garbage collected language
  • Robust and Secure
  • Exception Handling
  • Portable and Platform Independent

JDK internals

JDK.png

Primitive Data Types with sizes

1 byte = 8 bits
boolean = 1 bit
char = 2 byte
byte = 1 byte
short = 2 byte
int = 4 byte
long = 8 byte
float = 4 byte
double = 8 byte

Non-Primitive Data Types String Array Class Interface

Typecasting It is a method of converting a variable of one data type to another data type to process these variables correctly. Two types: Implicit & Explicit

Operators in Java Arithmetic operators +,-,/,*,%
Relational operators <, >, <=, >=,==, !=
Logical operators && , ||
Assignment operator =, +=, −=, ×=, ÷=, %=, &=, ^=, |=, <<=, >>=, >>>=
Increment and Decrement operator ++ , - -
Conditional operators ?:
Bitwise operators ^, &, |
Special operators . (dot operator to access methods of class)

Popular Java IDE's

  • Eclipse
  • NetBeans
    Java code can also be written in any text editor and compiled on the terminal with the following commands :
    $ javac [file_name].java
    $ java [file_name]
    

Variables in Java

  • Local Variable
  • Global or Instance Variable
  • Static Variable

Basic Reserved Words
Class: enum, class, extends, super, this, new
Interface: abstract, interface, implements
Methods: final, static, return
Exception Handling: try, catch, throw, throws, finally

Statements in Java

  1. Conditional Statements
  2. Switch
  3. Loops (For, While, Do-while)

OOPS Concepts

  1. Object and Class
    In an OOPS world, all entites are represented as Classes and their attributes as variables. The actions they perform are represented as methods. Helps in Code-readability.

  2. Data Abstraction and Encapsulation
    The wrapping or enclosing up of data and methods into a single unit. Data Hiding: This insulation of data from direct access by the program.

  3. Polymorphism
    Defined as the ability to take more than one form. Two types: Method overloading: When a method in a child class has the same name and type signature as a method in its parent class, then it is said to override the method in the parent class. Removes redundancy in code.
    Method overriding: Two or more methods in the same class sharing the same name, as long as any one of the following declarations are different: Number of parameters, data type of parameters, Sequence of the data type of parameters

  4. Inheritance
    Inheritance provides the concept of reusability. In a Parent-Child relationship, a child (derived class) that extends a parent class, inherits all the variables and methods of the parent(base class).

Types of Inheritance in Java
Single Inheritance - Parent-Child
Multilevel Inheritance - Parent-Child-Grandchild
Multiple Inheritance: Two parent classes. Achieved by using Interfaces.
Hierarchical Inheritance: Parent - Children

Limitations:
Private members and constructors are not inherited.
There can be only one super class to a subclass.

Interfaces
Interfaces serve as blueprints for other classes. The classes that implements an interface must provide their own unique implementation of the methods in the interface. They also serve as placeholders for a generic code implementation. Since Java 8, interfaces can have their own default implementation.

Abstract Class
Superclass only defines a generalized form shared by all of its subclasses, leaving it to each subclass to implement its methods.

Constructors
A constructor initializes an object on creation. They have the same name as the class. They do not have any return type, not even void. The constructor cannot be static, abstract, or final. Types of Contructors:

  • Non-Parameterised/Default (invoked by default)
  • Parameterized (Used to initialize the fields of the class with predefined values)

Arrays An array is a group of like-type variables referred by a common name, having continuous memory. Has fixed size. Can also be used to hold custom classes. Types: Single Dimensional, Multi-Dimensional/matrix-arrays

Strings Strings are a non-primitive data type that represents a sequence of characters. Java strings are immutable; we cannot change them. Whenever a string variable is created, a new instance is created in the String Pool. Implements the CharSequence interface Methods toLowerCase, toUpperCase, replace, equals. equalsIgnoreCase, length, charAt, compareTo, concat, substring, substring, toString, indexOf, indexOf, valueOf

String Builder: Non-Synchronized, Multithreaded
String Builder: Synchronized, Thread Safe

Exception Handling
The exception is an abnormality or error condition caused by a run-time error in the program. We can create custom Exception classes that extends the Exception or Throwable class to throw our own Exceptions.

For Example: ArithmeticException, ArrayIndexOutOfBoundException, ArrayStoreException, FileNotFoundException, IOException, NullPointerException, NumberFormatException, OutOfMemoryException, StringIndexOutOfBoundException

Types:

  • Checked Exceptions (Handled explicitly in the code itself with the help of try-catch block, Extended from java. lang.Exception class)
  • Unchecked Exceptions/ Runtime Exceptions (JVM handles such exceptions, Extended from java.lang.RuntimeException class)

Try and Catch
Try keyword is used to preface a block of code that is likely to cause an error condition and throw an exception. The keyword catch defines a catch block “catches” the exception “thrown” by the try block and handles it appropriately. Multiple catch blocks can be used for a single try block.

Finally
Finally, statement: A final block is guaranteed to execute, regardless of whether or not an exception is thrown. It is usually used to free up memory or resources.