Data Types and Literals

What is Datatype ?

Datatype is a way to represent the information of our daily life in the programming language to perform operation on that data.

It is a way of storing data having some characteristics.




Integer Type

The integer type is for numbers without fractional parts.

Negative values are allowed.

Java provides four integer types :-

  • int takes 4 bytes
  • short takes 2 bytes
  • long takes 8 bytes
  • byte takes 1 byte

 

Note: All are signed by default

 

 

FLOATING TYPE

It is for numbers with fractional part.

  • float takes 4 bytes

  • double takes 8 bytes

 the name double refers to the fact that these numbers have twice the precision.

 

Characters

 

  • char type is used to describe individual characters.
  • char takes 2 bytes (Unicode)

 

Boolean

  • BOOLEAN TYPE – it varies jvm to jvm, undefined (Oracle)

 

Resource: http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

 

 

Providing data to variables

 

  • Using Literals

  • Using Interactive Input

  • Using Command Line Input

 

Literals

 

The values that we use from our side for some expression or assignment are called as literals or constant values.

 

Types of Literals

 

  1. Integral Literals
    1. Default is int

    2. Use l or L with long. Fpr e.g

      1. int num=6;

      2. long p=7L;

  2. Floating Literals

    1. Default is double

    2. Use f or F with float

      1. double x=5.6;

      2. float y=4.5; //error

      3. float y=4.5f; //correct

  3. Characters Literals

    1. Enclosed in single codes

      1. char ch='A';

    2. Use Escape Characters as per needs

      1. \n New Line

      2. \t Horizontal Tab

      3. \" Print "

      4. \\ Print \

      5. \r Carriage Return

 

 

Write the print(System.out.println) statement to display the following

 

He said "\n" is made for new line.

 

Solution

System.out.println("He said \"\\n\" is made for new line");

 

  1. String Literals

    1. Enclosed in double quotes

      1. String name="Dalai Lama";

    2. Managed by String class

    3. No termination by null character

  2. Boolean literals

    1. Can have true or false only

    2. boolean married=false;