• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Core Java Tutorials

Welcome to Core Java  Tutorials. The objective of these tutorials is to provide in depth understand of Core Java .

In addition to free Core Java  Tutorials, we will cover common interview questions, issues and how to’s of Core Java .

Introduction

The prime reason behind creation of Java was to bring portability and security feature into a computer language. Beside these two major features, there were many other features that played an important role in moulding out the final form of this outstanding language.

Those features are

 Simple

Java is easy to learn and its syntax is quite simple, clean and easy to understand.The confusing and ambiguous concepts of C++ are either left out in Java or they have been re-implemented in a cleaner way.

Eg : Pointers and Operator Overloading are not there in java but were an important part of C++.

Object Oriented

In java everything is Object which has some data and behaviour. Java can be easily extended as it is based on Object Model.

Robust

Java makes an effort to eliminate error prone codes by emphasizing mainly on compile time error checking and runtime checking. But the main areas which Java improved were Memory Management and mishandled Exceptions by introducing automatic Garbage Collector and Exception Handling.

Platform Independent

Unlike other programming languages such as C, C++ etc which are compiled into platform specific machines. Java is guaranteed to be write-once, run-anywhere language.

On compilation Java program is compiled into bytecode. This bytecode is platform independent and can be run on any machine, plus this bytecode format also provide security. Any machine with Java Runtime Environment can run Java Programs.

Secure

When it comes to security, Java is always the first choice. With java secure features it enable us to develop virus free, temper free system. Java program always runs in Java runtime environment with almost null interaction with system OS, hence it is more secure.

 Multi Threading

Java multi threading feature makes it possible to write program that can do many tasks simultaneously. Benefit of multi threading is that it utilizes same memory and other resources to execute multiple threads at the same time, like While typing, grammatical errors are checked along.

Architectural Neutral

Compiler generates bytecodes, which have nothing to do with a particular computer architecture, hence a Java program is easy to intrepret on any machine.

Portable

Java Byte code can be carried to any platform. No implementation dependent features. Everything related to storage is predefined, example: size of primitive data types

High Performance

Java is an interpreted language, so it will never be as fast as a compiled language like C or C++. But, Java enables high performance with the use of just-in-time compiler.

Java Core Concepts

Variables

Operations

Classes + Objects

Fields

Constructors

Methods

Interfaces

Variables

Computer programs, regardless of programming language, typically read data from somewhere (file, keyboard, mouse, network etc.), process the data, and write some data somewhere again (to screen, file, network etc.).

In a Java program data is kept in variables. Your Java program first declares the variables, then read data into them, execute operations on the variables, and then write the variables (or data based on the variables) somewhere again. Variables are explained in more detail in the text on Java variables.

Each variable has a data type. The data type determines what kind of data the variable can contain, and what operations you can execute on it. For instance, a variable could be a number. Numbers can be added, subtracted, multiplied, divided etc. Or, a variable could be a string (text). String’s can be divided into substrings, searched for characters, concatenated with other strings etc. Java comes with a set of built-in data types. These data types are described in more detail in the text on Java data types.

Here is a simple Java variable declaration and operation example. Don’t worry if you don’t understand it now. Later texts in this Java language tutorial explains the details. The purpose of the example is just to give you a feeling for how working with Java variables look.

int myNumber;

myNumber = 0;

myNumber = myNumber + 5;

The first line of this example declares a variable named myNumber of the data type int. An int is a 32 bit integer (number without fractions).

The second line sets the value of the myNumber variable to 0.

The third line adds 5 to the current value of myNumber

Operations

Operations in Java are the instructions you can use to process the data in variables. Some operations read and write the values of variables (as you have already seen examples of), while other operations control the program flow. The most important operations are:

Variable operations

-Variable assignment of values.

-Variable reading of values.

-Variable arithmetic.

-Object instantiation.

Program flow

-for loops.

-while loops.

-if statements (branches).

-switch statements.

Method calls.

All of these operations are explained in detail in their own texts.

Here are a few examples of operations:

int number = 0;
int abs    = 0;
    
//imagine some operations that assign a value to number 
// - but left out of this example.
    
if(number >= 0) {
    abs = number;    
} else {
    abs = -number; 
}

This example first declares two variables named number and abs. The variable abs is supposed to contain the absolute value of number. The absolute value of a number is always positive. For positive numbers the absolute value is the number itself. For negative numbers the absolute value is the number without the negative sign. For instance, the absolute value of -10 is 10.

The if operation checks the value of the number variable to see if it is larger than or equal to 0. If it is, the absolute value assigned to the abs variable is the value of the number variable. If the number value is less than 0, then the value assigned to number is equal to -number. Negating a negative number gives a positive number, remember? -(-10) is 10.

Classes + Objects

Classes group variables and operations together in coherent modules. A class can have fields, constructors and methods (plus more, but that is not important now). I will shortly describe fields, constructors and methods here, but they are explained in more details in their own texts too.

Objects are instances of classes. When you create an object, that object is of a certain class. The class is like a template (or blueprint) telling how objects of that class should look. When you create an object, you say “give me an object of this class”.

If you think of a factory producing lots and lots of the same items, then the class would be the blueprint / manual of how the finished product should look, and the objects would be each of the finished products. If the factory produced cars, then the blueprint / design manual of the cars to produce corresponds to a Java class, and the physical cars produced corresponds to Java objects.

Here is a simple diagram illustrating the principle of objects being of a certain class. The class determines what fields and methods the objects of that class have.

Capture 37

A Java class can contain fields, constructors and methods

public class Car {Here is an example Java class declaration: }

This example declares a class named Car. The Car class does not contain any fields, constructors or methods. It is empty. The example primarily serves to show you an example of how a class declaration looks in Java code.

Fields

A field is a variable that belongs to a class or an object. It is a piece of data, in other words. For instance, a Car class could define the field brand which all Car objects would have. Each Car object could then have a different value for the brand field.

Fields are covered in more detail in the text on Java fields.

Here is the Car class declaration from above with a field name brand added:

public class Car {
    private String brand;
}

This example declares a field named brand of data type String which is text.

Constructors

Constructors are a special kind of method that is executed when an object of that class is created. Constructors typically initialize the objects internal fields – if necessary.

Constructors are covered in more detail in the text on Java constructors.

Here is the Car class from before with a constructor that initializes the brand field:

public class Car {
    
    private String brand;

    public Car(String theBrand) {
        this.brand = theBrand;
    }
}

Methods

Methods are groups of operations that carry out a certain function together. For instance, a method may add to numbers, and divide it by a third number. Or, a method could read and write data in a database etc.

Methods are typically used when you need to group operations together, that you need to be able execute from several different places. Or, if you just want your code to be easier to read. In other programming languages methods may be called “procedures” or “functions”.

Methods are covered in more details in the text on Java methods.

Here is the Car class from before with a single, simple method named getBrand added:

public class Car {
    
    private String brand;

    public Car(String theBrand) {
        this.brand = theBrand;
    }
    
    
    public String getBrand() {
        return this.brand;
    }
}

Interfaces

Interfaces is a central concept in Java. An interface describes what methods a certain object should have available on it. A class can implement an interface. When a class implements an interface, the class has to implement all the methods described in the interface. Interfaces are described more in my text about Java interfaces.

Packages

Packages in Java is another central concept. A package is a directory containing Java classes and interfaces. Packages provides a handy way of grouping related classes and interfaces, thus making modularization of your Java code easier. Packages are described in more detail in my text about Java pack

Data Types in Java

Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in an identifier.

In java, data types are classified into two catagories :

-Primitive Data type

-Non-Primitive Data type

Primitive Data type

A primitive data type can be of eight types :
char

boolean

byte

short

int

long

float

double

Once a primitive data type has been declared its type can never change, although in most cases its value can change. These eight primitive type can be put into four groups.

Integer: This group includes byte, short, int, long

byte : It is 8 bit integer data type. Value range from -128 to 127. Default value zero. example: byte b=10;

short : It is 16 bit integer data type. Value range from -32768 to 32767. Default value zero. example:short s=11;

int : It is 32 bit integer data type. Value range from -2147483648 to 2147483647. Default value zero. example:int i=10;

long : It is 64 bit integer data type. Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Default value zero. example: long l=100012;

Floating-Point Number

This group includes float, double

float : It is 32 bit float data type. Default value 0.0f. example: float ff=10.3f;

double : It is 64 bit float data type. Default value 0.0d. example: double db=11.123;

Characters

This group represent char, which represent symbols in a character set, like letters and numbers.

char : It is 16 bit unsigned unicode character. Range 0 to 65,535. example: char c=’a’;
Boolean

This group represent boolean, which is a special type for representing true/false values. They are defined constant of the language. example: boolean b=true;

Non-Primitive(Reference) Data type

A reference data type is used to refer to an object. A reference variable is declare to be of specific and that type can never be change. We will talk a lot more about reference data type later in Classes and Object lesson.

Site Disclaimer, Copyright © 2016 - All Rights Reserved.