Saturday, July 2, 2016

Variable and Datatypes

Variable:

A variable shows the memory location to store the data or values

For example:
    int x,y

 int is datatype (will discuss later)

 int x,y means that programmer telling to java compiler that we are going to store integer type data into x,y variable.

Now after that we can assign the values to these variable as

 x=10
 y=20

 ‘=‘ is the assignment operator and thus 10 is stored into x and 20 is stored into y.

Few Tips on Naming Convention in Java:

Java is a Case Sensitive Language, it recognize capital and small letters as different.We use below syntax to print the statement on console

System.out.println("Core Java Session");

If we use System as system (s is in lower case then java will not accept it).So we need to understand that where we need to use Capital and small letters.

1. Package name always start from small letter like java.awt,java.io etc

2. Class name and Interface name always start from Capital letter like System, ActionListener etc

3. Method fist word would be start from small letter but from 2nd word onwards letter would be start from Capital letter.
 Eg: println,readLine()

Datatypes:

In starting of this blog we  understood the variable and  with that variable we used the data type as int.
int y

Here we are declaring that y is a variable which can store integer type data, then it means y can store only integer number like 23,897 etc

 y =25

Datatype types are as :

1. Integer Datatypes
2. Float Datatypes
3. Character Datatypes
4. String Datatypes

5. Boolean Datatypes

Float data types:
These data types is used to represent the numbers with decimal point e.g.-  float num = 3.1
Float data types  into two categories

 Float (4 bytes Memory size) – Float can represent up to 7 digits accurately after decimal point
 Double (8 bytes Memory size) – Double can represent up to 15 digits accurately after decimal point

Question:
 Float fix =3.156 (What would be the memory size would be occupied by fix variable)
 Answer would be 8 bytes or 4 bytes?

Character data types:
This data types is used to represent a single character for example z,g,& $ etc.
  char ( 2 bytes Memory size)
  e.g – char ex ='Z';

String  data types:
This data type represent the group of characters like Salman Khan, so it means string will be create by grouping the characters.
String name ="KeenSmartz Solution“;

Boolean data types:
This data types represent only two values –true or false.
 e.g – boolean value = true;

Literals:
Literal represent a value that is stored into a variable directly in the program.
 boolean value = true;
 char ex ='Z';
 String name ="KeenSmartz Solution“;
 int i =20;

In Next Topic we will discuss about the Operators

No comments:

Post a Comment