Tuesday, November 5, 2019

Data Types In JAVA

Data Types In JAVA

Data Types In JAVA | JAVA Programming Language


Java data types
Data Types In JAVA Programming Language


Data Types in Java


Data types specify the different sizes and values which can be stored in the variable.

There are two types of data types available in Java Programming Language:

1. Primitive Data Types
2. Non Primitive Data Types


  • 1. Primitive Data Types

The primitive data types consists boolean, char, byte, short, int, long, float and double.


  • 2. Non-primitive data types: 

The non-primitive data types consists Classes, Interfaces, and Arrays.


Java Primitive Data Types


In Java Programming language, primitive data types are the building blocks of data manipulation. These are the very basic data types available in Java programming language.

There will be 8 types of primitive data types are available in Java Programming language

  • 1. boolean data type
  • 2. byte data type
  • 3. char data type
  • 4. short data type
  • 5. int data type
  • 6. long data type
  • 7. float data type
  • 8. double data type

All the primitive data types are explained below:


  • 1. Boolean Data Type

The Boolean data type is the data type which used to store only two possible values: first true and second false. Boolean data type is used for simple flags which track true/false conditions.

The Boolean data type consider one bit of information, but its size can not be defined precisely.


  • 2. Byte Data Type

The byte data type is one of the example of primitive data type. Which is an 8-bit signed 2's complement integer. 


Byte data type value-range varied between -128 to 127 (inclusive). The minimum value of byte data type is -128 and the maximum value of byte data type is 127. That's default value is 0.


The byte data type is used for save memory in large arrays where the savings of memory is must required. This data type saves space because a byte is 4 times smaller than an integer. Byte data type can also be used in place of "int" data type.


  • 3. Short Data Type

The short data type is a 16-bit signed 2's complement integer. The value range of short data type is lies between -32,768 to 32,767 (inclusive). 


The minimum value of short data type is -32,768 and maximum value of short data type is 32,767. The Default value of short data type must be 0.


We can also use the short data type to save memory as like byte data type. The short data type is 2 times smaller than an integer data type.


  • 4. Int Data Type

The int data type is primitive data type and also a 32-bit signed 2's complement integer. Its value-range varies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive).


The minimum value of int data type is - 2,147,483,648 and maximum value of int data type is 2,147,483,647. In int data type"0" is used as a default value.


We should used the int data as a default data type for integral values unless if there is no problem about memory.


  • 5. Long Data Type

The long data type is a 64-bit 2's complement integer. The value range of long data type is varies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive).


The minimum value of the long data type is - 9,223,372,036,854,775,808 and the maximum value of long data type is 9,223,372,036,854,775,807. 


Default value of the long data type is 0. The long data type is used when we need such a range of values more than those provided by int.


  • 6. Float Data Type

The float data type is a data type which is also known as a single precision 32-bit IEEE 754 floating point. The value range of float data type is unlimited. 

This data type is allowed to use a float (instead of double) if we need to save memory in large arrays of floating point numbers. The float data type is never be used for precise values, as like currency. The Default value of float data type is 0.0F.


  • 7. Double Data Type

The double data type is the data type which is also called a double precision 64-bit IEEE 754 floating point. The value range of double data type is unlimited. 


The double data type is mostly used for decimal values as like float data type. The double data type does not used for precise values, as like currency. In double data type "0.0d" is the default value.


  • 8. Char Data Type

The char data type is the data type which is known as a single 16-bit Unicode character. 


The value range of char data type is varies between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive). It also used to store characters.


Java Non Primitive Data Types


Non-primitive data types are known as the data type which is created by programmers. These data types are not predefined in Java as like primitive data types. 


If we define a variable of non-primitive data types, then it references a memory location where data is stored in the heap memory. Such that it references to a memory where an object is actually placed.


So, the variable of a non-primitive data type is also known as reference data types also called a object reference variable. This reference data types available in the stack memory and the object  which it points always lives on the heap memory. The stack stay holds a pointer to the object on the heap.


In Java programming language, all non primitive data types are known as objects which are created by instantiating a class.


There will be mainly 5 types of Non Primitive Data Types are available:

  • 1. Class
  • 2. Object
  • 3. String
  • 4. Array
  • 5. Interface

The description of all the five non primitive data types are explained below:


  • 1. Class and objects: 

Every class is data type and it is also considered as user defined data types. This can be possible because of a user creates a class.

  • 2. String: 

A string which represents the sequence of characters like America, DEF456, etc. A simplest way of to create a string object which is possible by storing sequence of characters into string type variable as like this:

          String str="Apple";

Here, A string type variable str contains "Apple". A string is also called a class.


  • 3. Array: 

An array in java is one of the object which is used to store same type multiple variable. That variables can be primitive or non-primitive data type.

 
To declaring an array variable of primitive data type int is shown below:

           int [ ] runs;

To declaring an array variable of non primitive data type is shown below:

          Animals [ ] animals; // animals is a name of class.

  • 4. Interface: 


The interface is declared as like a class but one of the difference is that interface contains only final variables and method declarations. Interface is the fully abstract class.


Friday, October 25, 2019

What is Inheritance ? | Concept of inheritance in JAVA | JAVA Programming

What is Inheritance ? | Concept of inheritance in JAVA | JAVA Programming

What is Inheritance ? | Concept of inheritance in JAVA | JAVA Programming




What is Inheritance?


In this article we learn about inheritance in java with the help of simple examples.


Inheritance is the one of the part of or key features of object oriented programming (OOP) which allows us to define a new class from an existing class.

Example:

class Birds
{
          // eat() method
          // sleep() method
}
class Parrot extends Birds
{
          // fly() method
}


In Java Programming, we can use the extends keyword to inherit from a class.


Here, we have inherited the parrot class from the Birds class.


The Birds is the super or parent class and the parrot is a subclass or child class.


The subclass inherits the fields and methods of the super class.




is-a relationship


Inheritance is the one type of is-a relationship. We can use inheritance if and only if an is-a relationship is present between two classes.

Example:

A mango is a fruit.
A bus is a vehicle.
A parrot is a bird.
A cat is a animal.
A Neem is a tree.
A fan is an object.


Example of JAVA Inheritance:

class Birds
{
          public void eat()
          {
                    System.out.println( " I can eat " );
          }
          public void sleep()
          {
                    System.out.println( " I can sleep " );
          }
}

class Parrot extends Birds
{
           public void fly()
           {
                     System.out.println( " I can fly " );
           }
}

class main
{
          public static void main ( string[ ] args )
          {
                    Parrot.parrot1 = new Parrot();
                    
                    parrot1.eat();
                    parrot1.sleep();

                    parrot1.fly();
          }
}

Output:

I can eat
I can sleep
I can fly


In this example we have inherited a subclass Parrot from superclass Birds. 

The Parrot class inherits the methods eat() and sleep() from the Birds class.

So, object of the Parrot class can access the members of both the Parrot class and the Birds class.



Types of inheritance


There are five types of inheritance.

1. Single inheritance:

In this type of Inheritance Class B applies only to Class A.


2. Multi-level inheritance

In multi level inheritance class B extends class A;  then class C extends from class B.

 3. Hierarchical inheritance: 

In Hierarchical inheritance Class A acts as a superclass for classes B, C and D.


 4. Multiple inheritance:

In Multiple inheritance Class C extends from interfaces A and B.

 5. Hybrid inheritance

It is a combination of two or more types of inheritance.


Java programming should not support multiple and hybrid inheritance through classes. Also we can get multiple legacy in Java via interfaces.



Why use inheritance?

The most important use is the reuse of the code. The code in the parent class does not need to be rewritten in the child class.

To get run-time polymorphism by overloading a method.  We will learn more about polymorphism in future article.

Wednesday, October 23, 2019

What is OOPS ? | Concept of OOPS | JAVA programming

What is OOPS ? | Concept of OOPS | JAVA programming

What is OOPS ? | Concept of OOPS | JAVA programming


JAVA programming

What is OOPS?

Object-oriented programming is a programming concept based on the principle that objects are the most important part of your program.  


Lets users create the desired objects and then create methods to manage those objects.  Handling these objects for results is the goal of object-oriented programming.


Object-oriented programming, better known as OOP, is used in a modern programming language such as Java.


The basic concepts of OOPS are

(1) Class


The class is a group of similar entities.  It's just a logical component and not the physical entity.  

For example, 

if you have a class called Costly Cars, it may include items such as Mercedes, BMW, Toyota, etc.  Their characteristics (data) can be the price or the speed of these cars.  While methods can be done with these cars, drive, reverse, brake, etc.


(2) Object


An object can be defined as an instance of a class and a program can contain multiple instances of a class.  


An object contains both the data and the function that works with the data.  

For example

chair, bike, marker, pen, table, car, etc.


(3) The Inheritance


Inheritance is an OOPS concept in which an object captures the properties and behavior of the parent object.  

A parent-child relationship is established between two classes.  It provides a robust and natural mechanism for organizing and structuring any software.


(4) Polymorphism


Polymorphism refers to the ability of a variable, object, or function to take many forms.  

For example, 

In English, the verb has a different meaning when you use it with a laptop, a race and a company.  


Here we understand the term meaning based on the other words used together with term.  The same is true for polymorphism.


(5) Abstraction


An abstraction consists in presenting the essential characteristics without taking into account the details of the background. This is a technique for creating a new data type that is suitable for a particular application.  

For example, 

If you drive a car, you do not have to worry about internal work.  Here, you only need to worry about parts such as the steering wheel, the transmission, the accelerator pedal, and so on.


(6) Encapsulation


Encapsulation is a OOP technique for encapsulating data and code.  In this OOPS concept, variables in one class are always hidden from other classes.  


It could only be accessed using the methods of their current class.  


For example, 

At school, a classless student can not exist.


(7) Association


Association is a relationship between two objects.  It defines the diversity between objects.  


In this OOP concept, all objects have a distinct life cycle and there is no owner.  


For example, many students may be assigned to one teacher, while one student may be assigned to more than one teacher.


(8) Aggregation


In this technique, all objects have their own life cycle.  However, there are property rights so that children can not belong to any other parent.  


For example, consider the class / object department and the teacher.  Here, only one teacher can not belong to more than one department, but even if we delete the department, the teaching object is never destroyed.


(9) composition


A composition is a particular form of aggregation.  It is also called "relationship of death".  Child objects do not have a life cycle.

If the parent object deletes all child objects, they will also be deleted automatically. Take an example of the house and the room. Each house can have several rooms.  


A room can not be part of two different houses.  So if you delete the house, the room will also be deleted.


Benefits of OOPS:


✓ OOP provides a clear and easy-to-understand modular structure for programs.

✓ Objects created for object-oriented programs can be reused in other programs. This saves considerable development costs.

✓ Large programs are hard to write, but if the development and design team follows the OOPS concept, they can design better with minimal errors.

✓ This also improves the modularity of the program because each object exists independently.

Tuesday, October 8, 2019

What is JAVA ? | History Of JAVA | Features Of JAVA

What is JAVA ? | History Of JAVA | Features Of JAVA

History of Java / Core Java


JAVA Programming Language



Java Programming Language, was invented by James Gosling under Sun Microsystem company.


But both Mike Sheridan and Patrick Naughton have a big stake in making Java language.  In all three, 'James Gosling' played an important role.


Java language was originally created for the interaction of Television.  But currently this language proved to be very important and in future its importance will increase even more.


Java was first named 'Oak' from this tree.  This means 'James Gosling' and his co-workers used to work for Java, there was 'Oak' tree and 'Oak' is also the national tree of many countries.  That's why it was named 'Oak'.


This 'Oak' was named in 1991.  But this 'Oak' name was already from 'Oak Technologies'. After that, 'Oak' was renamed 'Java'.


Java was the only reason to name it, that when 'James Gosling' and his co-operatives worked, they used to drink Indonesian coffee of 'Java' seeds and the name 'Java' was quite new too.  That's why Java was named.


In 2010, Sun Microsystem sold it to 'Oracle'.


When java started, its first specimen came in '1995'.



All versions of Java are shown below.



Java versions
JAVA versions



Introduction for Java / Core Java



What is java


Java is a very popular programming language worldwide.

Three variants have been created for Java.

✓ Java SE (Standard Edition)

✓ Java EE (Enterprise Edition)


✓ Java ME (Micro Edition)


1. Java SE

Java SE is used for Basic Programming, also known as Core Java.

2. Java EE

Java EE is Advanced Programming, it is also called Advanced Java.

3. Java ME

Java ME is suitable for mobile programming, but it has no relation with android.


Java was made free when it was launched and will remain free in present and future.  


Another importance of Java is Write Once, Run Anywhere, which means it is run on any mobile-based, windows-based and web-based application.



Java features



Features Of JAVA


Java is designed for Programming with many features.

 Like,

✓ Simple

✓ Secure

✓ Object-oriented

✓ Independence

✓ Portable

✓ Robust

✓ Interpreted

 ✓ Multitreaded

 ✓ High performance

✓ Distributed


Simple: 



This is somewhat similar to Language C ++.  Any user who gets good knowledge from C ++, also has no problem in learning Java.



Secure: 



It is more secure than Language C and C ++.  This is Language virus-free Language.



Object-Oriented: 



It is Language Class-based and Object-based.



Independent: 



It is independent language, if you want to build a software, then it does not need anything.


Portable: 



This language can be run on any platform.



Robust: 



This language and its memory management are very strong.



Interpreted: 



This language is an interpreted language.



Multitreaded:



More than one program can be run simultaneously in this language.



High Performance: 



Compilers of Java do not take much time for program execution, hence it is considered a high-performing language.



Distributed: 



The program created by Java is distributed from one computer to another.