Part II
Part II
Part II
Compilation Flow:
When we compile Java program using javac tool, java compiler converts the sourcecode into
byte code.
Parameters used in First Java Program
Java Variables
A variable is a container which holds the value while the Javaprogram
is executed. A variable is assigned with a data type.
Variable is a name of memory location. There are three types of
variables in java: local, instance and static.
There are two types of data types in Java: primitive and non-primitive.
Variable
Types of Variables
local variable
instance variable
static variable
1) Local Variable
A variable declared inside the body of the method is calledlocal
variable.
Local variable can be declared in the method, Constructoror
block Local variables are visible only within the declared
method constructor or block
You can use this variable only within that method and theother
methods in the class aren't even aware that the variable exists.
A local variable cannot be defined with "static" keyword.
Access modifiers cannot be used with local variable
2) Instance Variable
A variable declared inside the class but outside the bodyof the
method, is called instance variable.
It is not declared as static.
It is called instance variable because its value is instance
specific and is not shared among instances.
Instance variable can be accessed object with reference
variable which contains object
import java.io.*;
public class Employee {
// this instance variable is visible for any child class.public
String name;
// salary variable is visible in Employee class only.private
double salary;
// The name variable is assigned in the constructor.public
Employee (String empName)
{
name = empName;
}
// The salary variable is assigned a value.public
void setSalary(double empSal)
{
salary = empSal;
}
// This method prints the employee details.public void
printEmp()
{
System.out.println("name : " + name );System.out.println("salary :"
+ salary);
}
public static void main(String args[])
{
Employee empOne = new Employee("Ransika");
empOne.setSalary(1000);
empOne.printEmp();
}
}
Output
This will produce the following result −name :
Ransika
salary :1000.0
3) Static variable
1. class A{
2. int data=50;//instance variable
3. static int m=100;//static variable
4. void method(){
5. int n=90;//local variable6.
}
7. }//end of class
1. class Simple{
2. public static void main(String[] args){
3. int a=10;
4. int b=10;
5. int c=a+b;
6. System.out.println(c); 7.
}
8. }
Output: 20
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
The Boolean data type is used to store only two possible values:true and
false. This data type is used for simple flags that tracktrue/false
conditions.
The Boolean data type specifies one bit of information, but its"size"
can't be defined precisely.
Example: Boolean one =
falseByte Data Type