Thursday, 4 July 2019

Data Types


For learning a programing language first thing you need to consider is about the data type. The most frequently used data types are:

·                     Integer
·                     Double
·                     Char
·                     Boolean 

In java string is not under data types, it is a predefined class and which can be used as data types.

Data Types can be divided into primitive data types and derived data types:

Primitive Data Types:Int,double,char,boolean,float
Derived Data Types:String,Array,ArrayList,HashMap
Integer: Used for represent the whole number without decimals.
Example: int a =10;


If again write
int i=20;
compiler throw an error because we cannot duplicate the variable name, but can write
i=20;
so now the value of ‘i’ is replaced with 20. At a time, ‘i’ can store only one value.
int k=-1;
int p=0;
int j=12.33; it is not allowed.

Float: Used for represent the decimal values
Example: float f=10.1F;

Double: Also used for represent the decimal values
Example: double d=10.1;
double d1=14.11;
double d2=100; Stored as ‘100.00’

Char: Used for represent the character values
Example: char c='A';
char c1=’1’;
char c2=’$’;
char c3=’ca’; it is not allowed
Char: only single digit value, should be written within single quotes.

Boolean: Used for represent the Boolean(True/False) values
Example: boolean b1=true;
boolean b2=false;
true and false are a value and also keywords in java.

String: Used for represent the strings. In java string is not under data types, it is a predefined class and which can be used as data types.
Example: String s="Welcome";
String s1=”100”;
String s2=”12.33”;

Practice:
package LearnJava;

public class DataTypes
{
 
        public static void main(String[] args)
        {
            //1.Data Types

            //Primitive data types:int,double,char,boolean

        //    1.int
            int i=10;
            i=20;
            int j=-1;
            int k=0;

        //    2.double
            double d =12.33;
            double d1=100; //100.00
         
        //  Float
            float f=10.1F;

        //    3.char:only single digit value,should be written within single quotes
            char c='a';
            char c1='A';
            char c2='1';
            char c3='$';

        //    4.boolean
            boolean b1=true;
            boolean b2=false; //true or false is a value and also a keyword in java
         

        //    5.String is a class,not a data type
            String s="Hello World";
            String S1="Selenium";
            String s2="Hi this is my java code";
            String s3="1000";
            String s4="12.33";

        //    print the value in console (syso+cntrl+Space->shortcut)

            System.out.println(i+j);
         
        }
}       

No comments:

Post a Comment