0% found this document useful (0 votes)
8 views

Lecture-04-JAVA-UPDATED

The document provides an overview of Java comments, data types, and variables. It explains three types of comments (single line, multi line, and documentation), details primitive and non-primitive data types, and describes various variable types including local, instance, static, and final variables. Additionally, it discusses the scope and lifetime of variables in Java, emphasizing the differences between instance variables, local variables, and argument variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture-04-JAVA-UPDATED

The document provides an overview of Java comments, data types, and variables. It explains three types of comments (single line, multi line, and documentation), details primitive and non-primitive data types, and describes various variable types including local, instance, static, and final variables. Additionally, it discusses the scope and lifetime of variables in Java, emphasizing the differences between instance variables, local variables, and argument variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

LECTURE-04

Java Comments
The java comments are statements that are not executed by the compiler and interpreter.
The comments can be used to provide information or explanation about the variable,
method, class or any statement. It can also be used to hide program code for specific time.
Types of Java Comments
There are 3 types of comments in java.
1. Single Line Comment
2. Multi Line Comment
3. Documentation Comment

1) Java Single Line Comment


The single line comment is used to comment only one line.
Syntax:
1. //This is single line comment

Example:
1. public class Sample{
2. public static void main(String[] args) {
3. int j=10;//Here, i is a variable
4. System.out.println(j);
5. }
6. }
2) Java Multi Line Comment
The multi line comment is used to comment multiple lines of code.
Syntax:
1. /*
2. This
3. is
4. multi line
5. comment
6. */

Example:
1. public class CommentExample2 {
2. public static void main(String[] args) {
3. /* Let's declare and
4. print variable in java. */
5. int j=10;
6. System.out.println(j);
7. }
8. }
3) Java Documentation Comment
The documentation comment is used to create documentation API. To create documentation
API, you need to use javadoc tool.
Syntax:
1. /**
2. This
3. is
4. documentation
5. comment

6. */
Java Data Types
Java programming language has a rich set of data types. The data type is a category of data stored in variables. In java, data
types are classified into two types and they are as follows.
 Primitive Data Types
 Non-primitive Data Types
Primitive Data Types
The primitive data types are built-in data types and they specify the type of value stored in a variable and the memory size.
The primitive data types do not have any additional methods.
In java, primitive data types includes byte, short, int, long, float, double, char, and boolean.
The following table provides more description of each primitive data type.
Non-primitive Data Types
In java, non-primitive data types are the reference data types or user-created data types. All non-primitive data types are
implemented using object concepts. Every variable of the non-primitive data type is an object. The non-primitive data types
may use additional methods to perform certain operations. The default value of non-primitive data type variable is null.
In java, examples of non-primitive data types are String, Array, List, Queue, Stack, Class, Interface, etc.
Java Variables
A variable is a named memory location used to store a data value. A variable can be defined as a container that holds a data
value.
In java, we use the following syntax to create variables.

Syntax
data_type variable_name;
(or)
data_type variable_name_1, variable_name_2,...;
(or)
data_type variable_name = value;
(or)
data_type variable_name_1 = value, variable_name_2 = value,...;

In java programming language variables are classified as follows.

 Local variables
 Instance variables or Member variables or Global variables
 Static variables or Class variables
 Final variables
Local variables
The variables declared inside a method or a block are known as local variables. A local variable is visible within the method
in which it is declared. The local variable is created when execution control enters into the method or block and destroyed
after the method or block execution completed.
Let's look at the following example java program to illustrate local variable in java.

Example : public class LocalVariables


{
public void show() {
int a = 10;
//static int x = 100;
System.out.println("Inside show method, a = " + a);
}
public void display() {
int b = 20;
System.out.println("Inside display method, b = " + b);
// trying to access variable 'a' - generates an ERROR
System.out.println("Inside display method, a = " + a);
}
public static void main(String args[]) {
LocalVariables obj = new LocalVariables();
obj.show();
obj.display();
}
}
Example
Instance variables or member
public class ClassVariables {
variables or global variables int x = 100;
The variables declared inside a class and outside any
method, constructor or block are known as instance
public void show() {
variables or member variables. These variables are System.out.println("Inside show method, x = " + x);
visible to all the methods of the class. The changes x = x + 100;
made to these variables by method affects all the }
methods in the class. These variables are created public void display() {
separate copy for every object of that class. System.out.println("Inside display method, x = " +
Let's look at the following example java program to x);
illustrate instance variable in java. }
public static void main(String[] args) {
ClassVariables obj = new ClassVariables();
obj.show();
obj.display();
}
}
Static variables or Class variables Example
A static variable is a variable that declared using static
keyword. The instance variables can be static variables but public class StaticVariablesExample {
local variables can not. Static variables are initialized only int x, y; // Instance variables
once, at the start of the program execution. The static static int z; // Static variable
variable only has one copy per class irrespective of how StaticVariablesExample(int x, int y){
many objects we create. The static variable is access by this.x = x;
using class name. Let's look at the following example java this.y = y;
program to illustrate static variable in java. }
public void show() {
int a; // Local variables
System.out.println("Inside show method,");
System.out.println("x = " + x + ", y = " + y + ", z = " + z);
}
public static void main(String[] args) {
StaticVariablesExample obj_1 = new
StaticVariablesExample(10, 20);
StaticVariablesExample obj_2 = new
StaticVariablesExample(100, 200);
obj_1.show();
StaticVariablesExample.z = 1000;
obj_2.show();
}
}
Final variables
A final variable is a variable that declared using
final keyword. The final variable is initialized only
once, and does not allow any method to change it's
value again. The variable created using final
keyword acts as constant. All variables like local,
instance, and static variables can be final variables.
Let's look at the following example java program to
illustrate final variable in java. Example

public class FinalVariableExample {


final int a = 10;
void show() {
System.out.println("a = " + a);
a = 20; //Error due to final variable cann't be modified
}
public static void main(String[] args) {
FinalVariableExample obj = new FinalVariableExample();
obj.show();
}
}
Scope and lifetime of variables in Java
Scope and Life Time of Variables
The scope of a variable defines the section of the code in which the variable is visible. As a general rule, variables that are defined within a block
are not accessible outside that block. The lifetime of a variable refers to how long the variable exists before it is destroyed. Destroying variables
refers to deallocating the memory that was allotted to the variables when declaring it. We have written a few classes till now. You might have
observed that not all variables are the same. The ones declared in the body of a method were different from those that were declared in the
class itself. There are three types of variables: instance variables, formal parameters or local variables and local variables.
Instance variables
Instance variables are those that are defined within a class itself and not in any method or constructor of the class. They are known as instance
variables because every instance of the class (object) contains a copy of these variables. The scope of instance variables is determined by the
access specifier that is applied to these variables. We have already seen about it earlier. The lifetime of these variables is the same as the lifetime
of the object to which it belongs. Object once created do not exist for ever. They are destroyed by the garbage collector of Java when there are
no more reference to that object. We shall see about Java's automatic garbage collector later on

Local variables
A local variable is the one that is declared within a method or a constructor (not in the header). The scope and lifetime are limited to the
method itself. One important distinction between these three types of variables is that access specifiers can be applied to instance variables
only and not to argument or local variables. In addition to the local variables defined in a method, we also have variables that are defined in
bocks life an if block and an else block. The scope and is the same as that of the block itself

Argument variables
These are the variables that are defined in the header oaf constructor or a method. The scope of these variables is the method or constructor
in which they are defined. The lifetime is limited to the time for which the method keeps executing. Once the method finishes execution, these
variables are destroyed.

You might also like