Variables in Java - tutorial

VARIABLES IN JAVA 



Table of Contents

Variable
Comments
Passing arguments to a program
Rules for variable naming
Casting
Escape sequence characters
Literals    
Scope of the variable

 

What is a variable?

A variable is a small memory location, where we can store a small amount of data. It can be any data type. Datatype can be alphabetical, numerical,  alphanumerical…etc.

ex:
 

int   a;

 


int  →  is an integer datatype, it can  store only numbers. 

 a  →  is a variable name

 

 

coding-zon-variable-declaraction-demo

 

 
ex:
 
int a,b,c; // variables declration 
 
c = 0;     // initialization
a = 5;
b = 10;
 
 

To display the value of the variable use the following statement.

System.out.println(a);         //  5
System.out.println("a=" + a);  // o/p is a=5
 

 

Comments:

we write our own comments in program. They are 2 types.

1.single line---> with //
2.multi line ----> multiline with  
 
/*    .....
      .....   */

When we comment a line, the compiler ignores it. It doesnt print on the screen. 

 

Datatype and their sizes

There are 8 datatype in java.
 
char     ----  'a';
byte     ----  1 Byte store 0's and 1's.
short    ----  2 bytes
int      ----  4 bytes
long     ----  8 bytes
float    ----  4.16(integral part is 4, decimal values are 16)
double   ----  8.32
boolean  ----  false 
 
 
example: 
 
 
class Calculator{
 
public static void main(String args[])
{
	int a = 10; //declaration + assigning value to a variable
	int b = 20;
	int c = 0;
	c = a + b; // expression
 
	System.out.println("c=" + c); // print statement
}
}
 
 
 

 


Passing arguments to a progra in Java

 
example: 
  
public class ArgsDemo{ //class begin
 
public static void main(String args[]){ //main begin
 
	String name = args[0];
	String address = args[1];
 
       System.out.println("Name: " + name);
	System.out.println("Address: " + address);
 
 } // main end
} // class end
 
example2:  
 
public class Argsdemo2
{
public static void main(String args[])
{
	int n;
	n = Integer.parseInt(args[0]);
	System.out.println("n * n = " + a*a);
}
}
 
 

 

Rules for variable naming


Valid

 
1) variable name starts with letter 
   ex: empName, city, email
 
2) only two special characters are allowed ($, _)
   ex: $street_no, emp_no, emp_salary, customer_name  
 
3) can begin with $(dollar sign)
   ex: $street
   It can have '_', or in between the name
   ex: $street_no
 

Invalid

1) should not start with number. ex:123city;
2) should not allow spaces, ex: emp name
 

Casting

Changing value from one datatype to another.

ex: float a = 56.4;
int b = (int)a;
o/p is only int value, i.e. 56.

Write a program that takes 3 subjects marks and calculates total and prints all the details.

total = sub1 + sub2 + sub3;
avg = total/3;
print all the details.
 
class Marks
{
public static void main(String args[])
{
	int math, sci, eng, total = 0;
	float avg = 0.0f
 
       math = 35;
	sci = 26;
	eng = 34;
 
       total = math + sci + eng; 
       // math+sci+eng/3 ;---wrong
       avg = (math + sci + eng)/3// correct
 
      System.out.println("Total = " + total);
      System.out.println("Average = " + avg);
}
}
 
 

 

 Escape sequence characters

\n ---> prints in newline
\t ---> prints one tab space


Literals

In Java, literals are the constant values that can be visible directly in the program. It can be directly assigned to a variable. Java has different types of literals.

 
 

 

Literals Example


public class LiteralsDemo
{
public static void main(String args[])
{
	int num = 567;
	float floatVal = 5534.99f;
	double price = 18765.567;

      int hexaVal = 0x7e4;
	int binaryVal = 0b11010;
	int octalNum = 067;

       boolean boolVal = true; 

       char ch = 'J';
	char ch1 = '\u0021';
	char ch2 = 1456; 

       String str = "Java";
       String stuName = null;
	

 
       System.out.println(num);
	System.out.println(floatVal);
	System.out.println(price);
	System.out.println(hexaVal);
	System.out.println(binaryVal);
	System.out.println(ch);
	System.out.println(str);
	System.out.println(boolVal);
	System.out.println(octalNum);
	System.out.println(stuName);
	System.out.println(ch1);
	System.out.println("\t" + "backslash literal");
	System.out.println(ch2);
}
}
 

 
Output:
567
5534.99
18765.567
2020
26
J
Java
true
55
null
 
Backslash literal
? 
 

 





Scope of the variable:

Local variables:

A variable that is declared within the method that is called local variables. 

Instance variables 

A non-static variable that is declared within the class but not in the method is called an instance variable. Instance variables are related to a specific object.

Class variables

Class/Static variables A variable that is declared with static keyword inside a class is called static or class variable. They can be accessed by objects.


Example:  
 
class A {
 
int amount = 100; //instance(object) variable
static int pin = 2115; //static(class) variable
 
public static void main(String[] args) {
 
       int age = 35; //local(method) variable
 
	System.out.println(age);
	System.out.println(A.pin);
}
}

 

Review Questions

  1. What data always written in double quotes ( "")
  2. Class name should start with _______
  3. Why to use variables
  4. What  datatype you used to store text data in java
  5. To store numbers, what datatype you use..
  6. What is the  use of ++
  7. What is the default method in java give the name?
  8. What is the difference between char and string?
  9. what is comment in java
  10. How many types of comments are available in java
  11. variable can start with number?
  12. what symbols are allowed to use in variable name?
  13. what is literal?
  14. what are the escapee sequence characters in java ?give one example
  15. how to declare class variable in java?
  16. what is the use of static keyword in java
  17. where we use instance variables
  18. what is instance in java
  19. what is local variable, where it is declared
  20. what is use of args[] in main method
  21. how do you pass arugments to java program?
  22. what is difference between print and println
  23. how many bytes allocated for short datatype
  24. how many bytes taken by string
  25. how do you write float value in java and why?

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP