Skip to main content

Data Types in Java

Session 1 - Data Types in Java

What is Data ?
 Most of you have shopped online and this is what a typical purchase looks like

  1.     You select a product which has a Price, Tax, Quantity
  2.     You pay using your Credit Card by entering Name, CC Number, Expiration Date, Billing    Address, Zipcode
Words above in Bold are data.

What is then Data Type ?
   Can we say Price, Tax, Quantity is of type Number because we can apply Arithmetic (+, -, /, *, %) Operations on them, i.e. we multiply Price Quantity and than add Tax.
Similarly Your Name, Address are text because it doesn't make sense to have Arithmetic Operation on them and in Java we call them String.

Supported Data Types in Java
  String   -  Text
  double  - Real Numbers i.e. 33.2, 20.0 etc
  int         - Non-Real Numbers i.e. 10, 30, 3453 etc

Typical Java Programs using the above Data Types

public class DataTypesDemo {
    public static void main(String[] args) {

        // define a Data Variable message of type String
        String message = "Hello World!";
        // print to console i.e. black screen

        Code Snippet - 1

        // define a Data Variables of type int
        int x = 10;
        int y = 20;
        int z = x + y;
        // print to console i.e. black screen

        Code Snippet - 2
       // define a Data Variables of type int
        double x = 10.0;
        double y = 20.0;
        double z = x + y;
        // print to console i.e. black screen
Question for you.
  what type is Phone No. (i.e. int or String) ?
    Its String since you will never apply Arithmetic Operations on it.

There are few others Data Types which I missed that can be found in any book. 


Jon said…
I know this is an 'introduction', but Java Doubles are NOT real numbers! They are _approximations_ of Real numbers! Doubles/Floats should NEVER under any circumstance be used for prices! Watch the movie Office Space for an explanation... or try this code at home:

public static void testSomething() {
double maybeOne = .1 * 10;
System.out.println("Does .1 x 10 = 1 in floating point math?");
System.out.println(maybeOne == 1);
System.out.println("Actual Value:" + maybeOne);

maybeOne = .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1;
System.out.println("How about if we add .1 ten times?");
System.out.println(maybeOne == 1);
System.out.println("Actual Value:" + maybeOne);


Does .1 x 10 = 1 in floating point math?
Actual Value:1.0
How about if we add .1 ten times?
Actual Value:0.9999999999999999
Jon said…
Good post other than that small technical error!
Its also advisable to use BigDecimal instead of float and double if you are doing price calculation. also checking float value in loop or if condition could result in erroneous comparison .

An Example of using ArrayList in Java 1.5 with generics
Somnath Kayal said…
good blog & here also a good blog which describe the data types in java in a clear diagram, so people can easily read and understand.

Popular posts from this blog

JPA 2 new feature @ElementCollection explained

@ElementCollection is new annotation introduced in JPA 2.0, This will help us get rid of One-Many and Many-One shitty syntax.

Example 1: Stores list of Strings in an Entity

public class Users implements Serializable {

    private static final long serialVersionUID = 1L;
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private List<String> certifications = new ArrayList<String>();

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public List<String> getCertifications() {
        return certifications;

    public void setCertifications(List<String> certifications) {
        this.certifications = certifications;

        Users u = new Users();
        u.getCertifications().add("Sun Certified Java Programmer");

Generated Tables

Column --> ID
    Row             1


ArrayList vs LinkedList vs HashSet Performance Comparision

ConclusionsInserting & Reading sequentially from Collection prefer LinkedList/ArrayListInserting & Reading/Deleting by Search/equals from Collection prefer HashSetInserting, ArrayList & LinkedList performs best while HashSet takes double the timeReading, HashSet performs best while ArrayList & LinkedList are marginally lessDeleting, HashSet performs 10 times better than ArrayList & ArrayList performs 4 times better than LinkedList. LinkedList is slow because of sequencial search Bottom line : unless you are not going to iterate using for(Integer i : list ) then prefer HashSet
Inserting/Reading/Deleting integer's from zero till countJDK7Collectionactioncounttime msArrayListInsert1000/1LinkedListInsert1000/1HashSetInsert1000/1ArrayListInsert100005LinkedListInsert100004HashSetInsert100007ArrayListInsert10000011LinkedListInsert10000011HashSetInsert10000021ArrayListGet/Read1000LinkedListGet/Read1000HashSetGet/Read1000ArrayListGet/Read100004LinkedListGet/Read100003Has…

Validating CSV Files

What is CsvValidator ?
  A Java framework which validates any CSV files something similar to XML validation using XSD.

Why should I use this ?
  You don't have to use this and in fact its easy to write something your own and also checkout its source code for reference.

Why did I write this ?
  Some of our projects integrate with third party application which exchanges information in CSV files so I thought of writing a generic validator which can be hooked in multiple projects or can be used by QA for integration testing.

What is the license clause ?

Are there any JUnit test cases for me checkout ?
 Yes, source

How to integrate in my existing project ?

Just add the Jar which can be downloaded from here CsvValidator.jar and you are good.

Instantiate CsvValidator constructor which takes these 3 arguements

         // filename is the the file to be validated and here is a sample         // list - defines all the fields in the above csv file ( a field has index, type, isOptional, rege…