Showing posts with label Java Fundamentals. Show all posts
Showing posts with label Java Fundamentals. Show all posts

Tuesday, July 13, 2010

String Vs. StringBuffer Vs. StringBuilder


String

StringBuffer

StringBuilder

Mutable

No

Yes

Yes

Synchronized

No

Yes

No

Serialized

Yes

Yes

Yes



When to use these classes?

String – When your text is not going to change
StringBuffer – When your text will be changed by multiple threads
StringBuilder – When your text will be changed by a single thread

Comparison between mutable and immutable objects

String is always considered an immutable class because String objects can't be changed. Usually the "+" or concatenation operator is used to add a character sequence to an existing String variable, but that does not mean its making alterations to the existing object. If you have a look at the example below first a String object is created and then it is concatenated with another String which eventually creates a new object for Str1 to refer to in the memory.


String str1 = "abc"; //1st Object

str1 = str1 + "d"; //2nd Object


The scenario is different for a StringBuffer or a StringBuilder. Once an object is created, it can be changed as many times as possible using methods like append(). If you look at the example below, 1st an object is created and later changes are made to the existing object in the memory using append method.

StringBuffer sbf1 = new StringBuffer("abc"); //1st Object


sbf1.append("d"); //appends "d" to the 1st Object




Author: Thiagu

Thursday, January 15, 2009

Interfaces in JAVA (Java Fundamentals)

An interface is similar to Abstract classes but the only difference here is an interface cannot have any code implementation within it's body. Interfaces serves as a prototype for the whole project, so that all the clasess implementing an interface would override the methods in it.

Here are the features and rules of an Interface:


  1. Interface can only be declared with public or default access modifiers
  2. Any method declared within an Interface is "public abstract"
  3. Any method declared within an interface should not have code implementations
  4. Any variables declared within an Interface is "public static final", in other words all of them will be constant values.
  5. A sub-class that does not override an Interface method/s should be declared abstract.
  6. An Interface can implement several interfaces.
  7. "extends" keyword is used for an interface to implement another interface

Let's take an example to figure out how actually an interface works. Let's expand the previous example "The Car Scenario". Now what are the common features of a Vehicle? Every modern Vehicle will have it's own engine, color and maximum speed (kmh). This feature will vary in accordance to the type of Vehicle. An economic car could have 4 seats, metalic or non-metalic color, and etc. Have a look at this code to see how actually interfaces are implemented for the Economic car.


interface Vehicle {
int engine() ;
int speed() ;
}

interface Color
{
String colorType() ;
boolean isMetalic() ;
}

interface Car extends Vehicle, Color
{
int numSeats() ;
}

public class EconomicCar implements Car
{
public int engine()
{
int cylinder = 6 ;
return cylinder ;
}

public int speed()
{
int maxSpeed = 200 ;
return maxSpeed ;
}

public String colorType()
{
String color = "black" ;
return color ;
}

public boolean isMetalic()
{
boolean metalic = true ;
return metalic ;
}

public int numSeats()
{
int seats = 4 ;
return seats ;
}
}


Every car will have it's own engine, color and max speed so, the Car interface implements both Color and Vehicle interface. The Car interface has it's own abstract method called numSeats beacuse number of seats vary for different type of cars. The sub-class of the whole hirearchy is EconomicCar, and this sub-class has to override all the abstract methods of it's super-classes.





Author: Thiagu

Sunday, January 11, 2009

Abstract Classes and Abstract Methods (Java Fundamentals)

You have to know two important concepts for this part. One is Abstract classes and the other is Interface. Depends on the nature of the project, you need to set certain level of standards and rules that the other developers must follow. This is where Abstract classes and Interfaces come into picture. Abstract classes are usually used on small and big time projects, where it can have code implementation like general classes and also declare Abstract methods (empty methods that require implementation from the sub-classes). Abstract methods usually serves as a prototype to developers.

Lets take an example of a Car, every car has seats but the number of seats vary based on the type of car. A sports car will have 2 seats and an economic family car has 4 seats. At this point the common feature for every car are seats, so we need to create an Abstract class called Car and an abstract method called seat. Than we can create 2 sub-classes called SportsCar and EconomicCar that will override the seat method of Car class.


public abstract Car{
public abstract int seat() ;
}

public class SportsCar extends Car{
public int seat()
{
int seat = 2 ;
return seat ;
}
}

public class EconomicCar extends Car{
public int seat()
{
int seat = 4 ;
return seat ;
}
}


Here are the rules of an Abstract class and method:

  1. Abstract classes cannot be instantiated
  2. Abstract class can extend an abstract class or a concrete class and implement several interface classes
  3. Abstract class cannot extend an interface
  4. If a class contains Abstract method, the class has to be declared Abstract class
  5. An Abstract class may or may not contain an Abstract method
  6. Abstract method should not have any code implementations, the sub-classes must override it (sub-class must give the code implementations).
  7. If a sub-class of an Abstract class does not override the Abstract methods of its super-class, than the sub-class should be declared Abstract also.
  8. Abstract method cannot be declared with any other non-access modifiers such as static, final, native, transient, volatile
  9. Abstract classes can only be declared with public and default access modifiers.
  10. Abstract methods can only be declared with public, default and protected modifier.



Author: Thiagu

Java Modifiers (Java Fundamentals)

There are two types of modifiers in Java one is Access modifiers and the other is Non-access modifiers.

Examples of Access Modifiers are:


  1. private
  2. public
  3. protected
  4. Default (Package)

Examples of non-access modifiers:


  1. static
  2. final
  3. abstract
  4. synchronized
  5. native
  6. transient
  7. volatile
Some developers or lecturers do term access modifiers as access specifiers but the official name used on books and SUN Microsystem is "Access Modifiers"


Author: Thiagu