Tuesday 11 September 2018

Data Type




Before goes to our Topic, Let's try to understand some basics ...

# KEYWORD 


  • The keyword is the predefined word and it has a predefined meaning.
  • we cannot use it for some other purpose.
  • all the keyword in Java is in a small case.
  • total 53 keywords in java.

Definition - "The Java programming language has 53 keywords. Each keyword has a specific meaning in the language. You can’t use a keyword for anything other than its pre-assigned meaning."


For Ex - 



Now ,
Data Type is also a keyword.
Total 8 Data Types in Java



Question - Suppose I have to store something in the program. Let Say I have to store age, So how I can store age in the program.????

Answer -  Whenever, we need to store something, we need memory. For that, we have to provide its size and type.

Providing size and its type, we have to declare a DATATYPE and to give the name of the memory location is called  VARIABLE.

So, VARIABLE - is the name given to the memory location where some value can be stored.
and
DATA TYPE - is means to identify the type of data and associated operation of handling it.


Now, Come to the question...

To do so, you have to think like... 
  • What I have to store?  ==> I have to story AGE
  • What type of DATA it is?  ==> Number(Numerical kind)
  • How Big Data It is?  ==> maximum 3 digits (Range of the data). 




In Java, We have Four Types Of Data
  • Character Type --> char
  • Numerical Type --> byte, short, int, long
  • Floating Point Type --> float,double
  • Boolean Type (Flag) --> boolean




As a programmer, You don't need to remember the range all the data type.
In Java, you have methods that will tell you all these.

just you have to learn Range Formula.
[WHERE - N SHOULD BE IN BITS]


For Example - 

For Byte -

-128  to  127



# Code 



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

// Integer
byte b = 10;
int i = 11; 
short s = 12;
long l = 129846;

// Double 
float f1 = 1.5f;
//or
float f2 = (float)2.5;
double d = 22.5; // Double

boolean bb1 = true;
boolean bb2 = false;

System.out.println(b+" "+i+" "+s+" "+l); 
System.out.println(f1+" "+f2+" "+d);
System.out.println(bb1+" "+bb2);
}
}


Question --  Why we are getting error " float f = 1.5; "?

Answer -- We have to write this ... because
by default float value is double literal, So to tell the compiler to treat it as float explicitly ---- we have to write it in below ways...

float f1 = 1.5f;  
//or
float f2 = (float)2.5;

Note - [ we can use  " f  " or " F "]






No comments:

Post a Comment