Unit I
Unit I
Simple.
Object-oriented.
Distributed.
Interpreted.
Robust.
Secure.
Architecture neutral.
Portable.
1. Primitive data types: The primitive data types include boolean, char,
byte, short, int, long, float and double.
2. Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays.
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
Example:
1. double d1 = 12.3
Example:
Java Variables
A variable is a container which holds the value while the Java program
Types of Variables
There are three types of variables in Java
o local variable
o instance variable
o static variable
1) Local Variable
A variable declared inside the body of the method is called local variable.
You can use this variable only within that method and the other methods
in the class aren't even aware that the variable exists.
A local variable cannot be defined with "static" keyword.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is
called an instance variable. It is not declared as static
3) Static variable
1. public class A
2. {
3. static int m=100;//static variable
4. void method()
5. {
6. int n=90;//local variable
7. }
8. public static void main(String args[])
9. {
10. int data=50;//instance variable
11. }
12.}//end of class
Java Arrays
Normally, an array is a collection of similar type of elements which has
contiguous memory location.
Array in Java is index-based, the first element of the array is stored at the
0th index, 2nd element is stored on 1st index and so on.
Output:
10
20
70
40
50
. The Java for-each loop prints the array elements one by one. It holds an array element in a variable,
then executes the body of the loop.
Output:
123
245
445
Operators in Java
Operator in Java
There are many types of operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Output:
15
5
50
2
0
Output:
1. 40
2. 80
3. 80
4. 240
false
false