Core Java Tutorial

Ratings:
(4)
Views:443
Banner-Img
  • Share this blog:

 

Welcome to Core Java  Tutorials. The objective of these tutorials is to provide an in-depth understanding 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 the creation of Java was to bring portability and security features into a computer language. Besides these two major features, there were many other features that played an important role in molding 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 an object which has some data and behavior. 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, the Java program is compiled into bytecode. This bytecode is platform-independent and can be run on any machine, plus this bytecode format also provides 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 enables us to develop a 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 multithreading feature makes it possible to write programs that can do many tasks simultaneously. The benefit of multi-threading is that it utilizes the same memory and other resources to execute multiple threads at the same time, like While typing, grammatical errors are checked along.

Architectural Neutral

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

Portable

Java Byte code can be carried to any platform. No implementation-dependent features. Everything related to storage is predefined, for example, the 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 a just-in-time compiler.

Java Core Concepts

1. Variables

2. Operations

Inclined to build a profession as core java training? Then here is the blog post on, explore core java training

3. Classes + Objects

  • Fields
  • Constructors
  • Methods

4. 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 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:

1. Variable operations

  • Variable assignment of values.
  • Variable reading of values.
  • Variable arithmetic.
  • Object instantiation.

2. Program flow

  • for loops.
  • while loops.
  • if statements (branches).
  • switch statements.

3. 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 detail 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.

Core Java

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 object's 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 to 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 detail 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

The interface 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 provide 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 categories :

  1. Primitive Data type
  2. 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 types can be put into four groups.

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

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

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

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

long: It is a 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 a 32-bit float data type. Default value 0.0f. example: float ff=10.3f

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

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

char: It is a 16-bit unsigned Unicode character. Range 0 to 65,535. example: char c='a';

Boolean: This group represents boolean, which is a special type for representing true/false values. They have defined as constants 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 declared to be specific and that type can never change. We will talk a lot more about reference data type later in the Classes and Object lessons.

For an Indepth knowledge on core java, click on below

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox