Lecture-04-JAVA-UPDATED
Lecture-04-JAVA-UPDATED
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
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,...;
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.
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.