Sunday, July 22, 2012

JAVA CUSTOM DATA TYPES


Custom data types is data type (which can hold value) but created by you.

Let us say, I have a class Dog.

public class Dog
{
public String breed;
public String name;
public float weight;

public void bark()
{}

public void play()
{}

}

So, looking at the class structure, it can store value of breed, name and weight. 
So this is different than the int datatype which can store single integer value.

So if I want a person class to hold all values associated with dog, I take a variable to type Dog class.

public class Person // [access modifiers] [class - the keyword] [class_name]
{
// [access modifiers] [datatype] [variable_name]
public String name; 
public int age;
public char gender;
public Dog dog; // not a int or char datatype but a Dog type.

[access modifiers] [return type] [method_name]
public void walk()
{}

public void eat()
{}

public void run(int kms)
{}

}

So, every  java class is a custom data type.


No comments:

Post a Comment