Friday 31 January 2014

Data Types in Java


Java supports mainly two categories of Data Types
     1. Primitive Types
    2. Reference Types

1. Primitive Types
    Java language supports 8 primitive types of data.
    Those are:
        byte, short, int, long
        float, double
        boolean
        char

        byte, short, int and long used to store integer values
        float and double used to store real values.
        Boolean used to store true or false values
        char is used to store single character.

How to define variable

    Syntax:
        dataType variableName = value;

    Example:
       int intVariable = 10;

        Here
            dataType is int
            variableName is intVariable
            value is 10


A variable is a named memory location, which holds a value.

Naming convention to define a variable

1. A variable name must start with a character or underscore

2. Variable name can contain alpha numeric characters

3. varibale names shouldn't be a reserverd or keyword of java

4. By convention a variable name starts with small letter, abd each sub sequent word starts with capital letter.
Ex: noOfChar, mySalary etc.,

Java Keywords
Keyword is a reserved word, which has special meaning defined for it.


abstract continue for New switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while

Note: All keywords are in lower case only

/* Simple Program to Print the variable values */
class DataTypes{
     public static void main(String args[]){
        byte byteVariable = 10;
        short shortVariable = 20;
        int intVariable = 30;
        long longVariable = 40;
        float floatVariable = 10;
        double doubleVariable = 20.123;
        boolean booleanVariable = false;
        char charVariable ='A';
    
        System.out.println("******************************************");
        System.out.println("Value Of byte Variable is " + byteVariable);
        System.out.println("Value Of short Variable is " + shortVariable);
        System.out.println("Value Of int Variable is " + intVariable);
        System.out.println("Value Of long Variable is " + longVariable);
        System.out.println("Value Of float Variable is " + floatVariable);
        System.out.println("Value Of double Variable is " + doubleVariable);
        System.out.println("Value Of boolean Variable is " + booleanVariable);
        System.out.println("Value Of char Variable is " + charVariable);
        System.out.println("******************************************");
     }
 }

Output
******************************************
Value Of byte Variable is 10
Value Of short Variable is 20
Value Of int Variable is 30
Value Of long Variable is 40
Value Of float Variable is 10.0
Value Of double Variable is 20.123
Value Of boolean Variable is false
Value Of char Variable is A
******************************************

Reference Types
In java Strings, Arrays and Objects are considered to be reference types. Will discuss more about these in later posts.

Some Points to Remember

1. Like many other languages Java doesn't support unsigned numbers. There is no unsigned specifier in Java

class UnsignedEx{
 unsigned int a;
}

When you try to compile the above class, below error is thrown.

UnsignedEx.java:2: error: <identifier> expected
   unsigned int a;
    ^
UnsignedEx.java:2: error: <identifier> expected
   unsigned int a;
   ^
2 errors

2. In java all the bytes, shorts are promoted to int before performing operations on them, So we need to explicitly cast them.

class ConversionEx{
    public static void main(String args[]){
        byte b1 = 1;
        byte b2 = 2;
        byte b = (b1 + b2);
    
        System.out.println(b);

        short s1 = 1;
        short s2 = 2;
        short s = (s1 + s2);
    
        System.out.println(s);
    }
}

  When you tries to compile the above program, compiler throws the below error

ConversionEx.java:6: error: possible loss of precision
 byte b = (b1 + b2);
 ^
 required: byte
 found:    int

ConversionEx.java:10: error: possible loss of precision
 short s = (s1 + s2);
 ^
 required: short
 found:    int
2 errors
 
To make the program compiles fine, we need to explicitly cast, after performing the operation

class ConversionEx{
    public static void main(String args[]){
        byte b1 = 1;
        byte b2 = 2;
        byte b = (byte)(b1 + b2);
        System.out.println(b);
        
        short s1 = 1;
        short s2 = 2;
        short s = (short)(s1 + s2);
        System.out.println(s);
    }
}
 
Output
3
3


3. In Java, if one of the operand is double, then other also promoted as double, and the final result will be double

Example

 class ConversionEx{
    public static void main(String args[]){
        float f1 = 1.1f;
        double f2 = 1.2f;
        float f = (f1 + f2);
        System.out.println(f);
    }
}


When you tries to compile the above program, compiler throws the below error

   ConversionEx.java:6: error: possible loss of precision
      float f = (f1 + f2);
      ^
      required: float
      found: double
1 error

To make the program run, cast the result to float.
class ConversionEx{
    public static void main(String args[]){
        float f1 = 1.1f;
        double f2 = 1.2f;
        float f = (float)(f1 + f2);
        System.out.println(f);
    }
}
  
Output
2.3000002



Hello World Application                                                 More About primitive types                                                 Home

No comments:

Post a Comment