JAVA BASICS
JAVA BASICS
JAVA BASICS
KIT-501
VARIABLE IN JAVA
Variable in Java is a data container that stores the data values during Java
program execution. Every variable is assigned data type which designates
the type and quantity of value it can hold. Variable is a memory location
name of the data. The Java variables have mainly three types : Local,
Instance and Static.
2. Variable Initialization
TYPES OF VARIABLES
In Java, there are three types of variables:
1) Local Variables
• Local Variables are a variable that are declared inside the body of a method.
2) Instance Variables
• Instance variables are defined without the STATIC keyword .They are defined Outside a
method declaration. They are Object specific and are known as instance variables.
3) Static Variables
• Static variables are initialized only once, at the start of the program execution. These
variables should be initialized first, before the initialization of any instance variables.
DATA TYPES IN JAVA
Data Types in Java are defined as specifiers that allocate different sizes
and types of values that can be stored in the variable or an identifier.
Java has a rich set of data types. Data types in Java can be divided into
two parts :
• Primitive Data Types :- which include integer, character, boolean, and float
class A {
System.out.println("Hello World");
}
PRIMITIVE DATA TYPES
• Primitive Data Types are predefined and available within the Java language.
Primitive values do not share state with other primitive values.
• There are 8 primitive types: byte, short, int, long, char, float, double, and Boolean
OPERATOR DESCRIPTION
+ ( Addition ) This operator is used to add the value of the operands.
This operator is used to divide the left hand operator with right
/ ( Division )
hand operator.
This operator is used to divide the left hand operator with right
% ( Modulus )
hand operator and returns remainder.
ASSIGNMENT OPERATOR
Assignment operator are used to assign new value to a variable. The left side
operand of the assignment operator is called variable and the right side operand of
the assignment operator is called value.
OPERATOR DESCRIPTION
= This operator is used to assign the value on the right to the operand on the left.
+= This operator is used to add right operand to the left operand and assigns the result to the left operand.
-= This operator subtracts right operand from the left operand and assigns the result to the left operand.
*= This operator multiplies right operand with the left operand and assigns the result to the left operand.
/= This operator divides left operand with the right operand and assigns the result to the left operand.
^= This operator performs exponential calculation on operators and assigns value to the left operand
This operator is used to divide the left-hand operator with right hand operator and assigns the result to left
%=
operand.
LOGICAL OPERATOR
Logical operators are used to combining two or more conditions or complement
the evaluation of the original condition under consideration.
OPERATOR DESCRIPTION
OPERATOR DESCRIPTION
This operator left shifts the bits of the first operand; the second operand
<< (Left Shift)
decides the number of places to shift.
This operator right shifts the bits of the first operand; the second operand
>> (Right Shift)
decides the number of places to shift.
RELATIONAL OPERATOR
Relational operator compares two numbers and returns a boolean value. This operator is
used to define a relation or test between two operands.
OPERATOR DESCRIPTION
This operator returns True, if the value of the left operand is less than the value of the
< (Less than)
right operand, else it returns False.
This operator returns True, if the value of the left operand is greater than the value of the
> (Greater than)
right operand, else it returns False.
This operator returns True, if the value of the left operand is less than or equal to the
<= (Less than or equal to)
value of the right operand, else it returns False.
>= (Greater than or equal This operator returns True, if the value of the left operand is greater than or equal to the
to) value of the right operand, else it returns False.
== (Equal to) This operator returns True, if two operands are equal, else it returns False.
!= (Not equal to) This operator returns True, if two operands are not equal, else it returns False.
JAVA OPERATOR PRECEDENCE AND ASSOCIATIVITY
• OPERATOR PRECEDENCE : Operator precedence determines which operator is performed first in
an expression if there is more than one operators with different precedence.
• OPERATOR ASSOCIATIVITY : Operator associativity is used when an expression have two
operators having same precedence.
OPERATOR PRECEDENCE ASSOCIATIVITY
Unary ++expr —expr +expr –expr ~ ! right to left
Multiplicative */% left to right
Additive +– left to right
Shift << >> >>> left to right
Relational < > <= >= instanceof left to right
Equality == != left to right
bitwise AND & left to right
bitwise exclusive OR ^ left to right
bitwise inclusive OR | left to right
logical AND && left to right
logical OR || left to right
Ternary ?: right to left