Java API
An API(Application Programming Interface) , in the framework of java is a collection of pre-written classes,interfaces , which are commonly used to perform any particular operations.
Java Buzzwords /Java Features
1.Simple – As compare to C, C++ learning java is easy , complicated concepts like Pointer and goto statement are not included in Java.
2.Object Oriented – Java is object oriented programming language.
3.Distributed – Java is distributed , We can write program which can distribute information across network , we can write program which can capture information and distribute across client.
4.Architecture Neutral – Java Byte code is platform independent , it can run on any machine any OS.
5.Secured – Java program will be first compiled to byte code.This byte code will then be interpreted by Java Virtual Machine (JVM). JVM makes java secure.
6.Portable – Java is portable , we can execute Byte Code on any platform.It doesn’t require write different program for different platform.
7. Multi-threaded – We can create threads to perform several tasks simultaneously.
Identifier In Java
In Java Programming language , an Identifier is a name given to a variable, class or method.
Identifier start with a letter or underscore( _ ) or dollar sign ($) . Identifiers are case sensitive and have no maximum length.Question mark( ? ) can not be used as Identifier.An Identifier can not be same as Query language keyword.It is not recommended to use SQL keyword as an Identifier.
Following are valid Identifier :
studentName
collegeName
institute_Name
_college_Fees
$employeeSalary
The variable type determine the value it can hold , when we declare a variable in java , we follow syntax as :
DataType variableName;
There are two main type of Data Types :
1.Primitive Data Type
2.Non-Primitive Data Type Or Referenced Datatype
Variable of primitive type contain value and reference type variable contain address, java support 8 primitive data types
DataType | size(in bits) | size(in bytes) | Minimum Range | Maximum Range | Default Value |
---|---|---|---|---|---|
byte | 8 | 1 byte | -128 | +127 | 0 |
short | 16 | 2 bytes | -32768 | +32767 | 0 |
int | 32 | 4 bytes | -2147483648 | 2147483647 | 0 |
long | 64 | 8 bytes | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | 0L |
float | 32 | 4 bytes | 3.40282347 x 10^38 | 1.40239846 x 10^-45 | 0.0f |
double | 64 | 8 bytes | 1.7976931348623157 x 10^308 | 4.9406564584124654 x 10^-324 | 0.0d |
char | 16 | 2 bytes | 0 | 65535 | ‘\u0000’ |
boolean | 1 | NA | NA | false |
Reference Data Types In Java are also known as non-primitive data types , it contain address or reference.Reference type is not predefined , it is created by programmer as per requirement,for example , a variable of class type is called referenced data type , it hold address of Object.
Class,Array,Enumeration, Interface are referenced Data Types.
Example :
Student s1=new Student() here s1 is referenced type.
Employee e1=new SoftwareDeveloper() here e1 is referenced type
Employee e2=new Accountant() here e2 is referenced type
Types Of Variables In Java
Variable is a name give to memory location.
Syntax :
DataType variableName; //declaration
variableName=value; //initialization
Example :
String collegeName;
collegeName=”MIT”;
or both declaration and initialization in same line as :
String collegeName=”MIT”;
1.Local Variables :
Local Variables in java are tied to method, scope of local variable is within the method.
2.Instance Variable(Non-static)
Non-static Instance variable tied to Object,scope of an instance variable is the whole class.
3.Static Variable
static variable is tied to class and shared by all instances of class.
OOP BASICS
Object-Oriented Programming (OOP) is a programming paradigm that emphasizes the use of objects and classes to structure code and model real-world concepts. Java is an object-oriented programming language that fully supports OOP principles. Let’s explore the key OOP concepts in Java in depth, along with examples:
- Classes and Objects:
Classes are the fundamental building blocks of Java programs. A class defines a blueprint that encapsulates data and behavior. Objects, on the other hand, are instances of classes. They represent specific entities or instances of the class. Here’s an example:
// Class definition public class Car { // Instance variables private String brand; private String color; // Constructor public Car(String brand, String color) { this.brand = brand; this.color = color; } // Method public void drive() { System.out.println("Driving the " + color + " " + brand + " car."); } } // Creating objects Car car1 = new Car("Toyota", "Red"); Car car2 = new Car("Honda", "Blue"); // Accessing object methods car1.drive(); // Output: Driving the Red Toyota car. car2.drive(); // Output: Driving the Blue Honda car.
- Encapsulation:
Encapsulation refers to the bundling of data (instance variables) and methods within a class. It protects the data from direct external access and provides controlled access through methods. This concept promotes data hiding and abstraction. Here’s an example:
public class BankAccount { private String accountNumber; private double balance; public BankAccount(String accountNumber) { this.accountNumber = accountNumber; this.balance = 0.0; } public void deposit(double amount) { // Validate amount and perform necessary calculations balance += amount; } public void withdraw(double amount) { // Validate amount and perform necessary calculations if (amount <= balance) { balance -= amount; } else { System.out.println("Insufficient balance."); } } public double getBalance() { return balance; } } BankAccount account = new BankAccount("1234567890"); account.deposit(1000.0); account.withdraw(500.0); double balance = account.getBalance(); // Returns 500.0
- Inheritance:
Inheritance allows the creation of a new class (child class) based on an existing class (parent class). The child class inherits the properties and methods of the parent class and can add additional features or override existing behavior. Inheritance promotes code reuse and hierarchical organization of classes. Here's an example:
public class Animal { public void eat() { System.out.println("Animal is eating."); } } public class Dog extends Animal { public void bark() { System.out.println("Dog is barking."); } } Dog dog = new Dog(); dog.eat(); // Output: Animal is eating. dog.bark(); // Output: Dog is barking.
- Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables code flexibility and dynamic behavior at runtime. Polymorphism is achieved through method overriding and method overloading. Here's an example:
public class Shape { public void draw() { System.out.println("Drawing a generic shape."); } } public class Circle extends Shape { @Override public void draw() { System.out.println("Drawing a circle."); } } public class Rectangle extends Shape { @Override public void draw() { System.out.println("Drawing a rectangle."); } } Shape shape1 = new Circle(); Shape shape2 = new Rectangle(); shape1.draw(); // Output: Drawing a circle. shape2.draw(); // Output: Drawing a rectangle.
- Abstraction:
Abstraction focuses on defining the essential characteristics and behaviors of an object while hiding the implementation details. Abstract classes and interfaces are used to achieve abstraction in Java. They provide a blueprint for derived classes to follow. Here's an example:
public abstract class Animal { public abstract void makeSound(); } public class Dog extends Animal { @Override public void makeSound() { System.out.println("Woof!"); } } public class Cat extends Animal { @Override public void makeSound() { System.out.println("Meow!"); } } Animal dog = new Dog(); Animal cat = new Cat(); dog.makeSound(); // Output: Woof! cat.makeSound(); // Output: Meow!
These are the key OOP concepts in Java. Understanding and applying these concepts can help you design modular, reusable, and maintainable code.
Happy Learning