Objects in Java:
Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.
Classes in Java:
A class is a blue print from which individual objects are created.
A sample of a class is given below:
public class Dog{
String breed;
int ageC
String color;
void barking(){
}
void hungry(){
}
void sleeping(){
}
}
A class can contain any of the following variable types.
Local variables:
Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
Instance variables:
Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
Class variables:
Class variables are variables declared with in a class, outside any method, with the static keyword.
A class can have any number of methods to access the value of various kinds of methods. In the above example, barking(), hungry() and sleeping() are methods.
Below mentioned are some of the important topics that need to be discussed when looking into classes of the Java Language.
Object-Oriented Programming Concepts
If you've never used an object-oriented programming language before, you'll need to learn a few basic concepts before you can begin writing any code. This lesson will introduce you to objects, classes, inheritance, interfaces, and packages. Each discussion focuses on how these concepts relate to the real world, while simultaneously providing an introduction to the syntax of the Java programming language.
What Is an Object?
An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. This lesson explains how state and behavior are represented within an object, introduces the concept of data encapsulation, and explains the benefits of designing your software in this manner.
What Is a Class?
A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.
What Is Inheritance?
Inheritance provides a powerful and natural mechanism for organizing and structuring your software. This section explains how classes inherit state and behavior from their superclasses, and explains how to derive one class from another using the simple syntax provided by the Java programming language.
What Is an Interface?
An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface. This section defines a simple interface and explains the necessary changes for any class that implements it.
What Is a Package?
A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage. This section explains why this is useful, and introduces you to the Application Programming Interface (API) provided by the Java platform.
ArrayList Vs Vector :
Vector , ArrayList classes are implemented using dynamically re-sizable array providing fast random access and fast list traversal very much like using an ordinary array . ArrayList support dynamic arrays that can grow as needed that is ArrayList can be dynamically increased or decreased in size .
1. Synchronization and Thread-Safe
Vector is synchronized while ArrayList is not synchronized . Synchronization and thread safe means at a time only one thread can access the code .In Vector class all the methods are synchronized .That's why the Vector object is already synchronized when it is created .
2. Performance
Vector is slow as it is thread safe . In comparison ArrayList is fast as it is non synchronized . Thus in ArrayList two or more threads can access the code at the same time , while Vector is limited to one thread at a time.
3. Automatic Increase in Capacity
A Vector defaults to doubling size of its array . While when you insert an element into the ArrayList , it increases its array size by 50%.
By default ArrayList size is 10 . It checks whether it reaches the last element then it will create the new array ,copy the new data of last array to new array ,then old array is garbage collected by the Java Virtual Machine (JVM) .
4. Set Increment Size
ArrayList does not define the increment size . Vector defines the increment size .
You can find the following method in Vector Class
public synchronized void setSize(int i) { //some code }
There is no setSize() method or any other method in ArrayList which can manually set the increment size.
5. Enumerator
Other than Hashtable ,Vector is the only other class which uses both Enumeration and Iterator .While ArrayList can only use Iterator for traversing an ArrayList .
6. Introduction in Java
java.util.Vector class was there in java since the very first version of the java development kit (jdk).
java.util.ArrayList was introduced in java version 1.2 , as part of Java Collections framework . In java version 1.2 , Vector class has been refactored to implement the List Inteface .
No comments:
Post a Comment