·
A variable is a container which holds the value
while the Java program 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 is name of reserved area allocated in
memory. In other words, it is a name of memory location. It is a combination of
"vary + able" that means its value can be changed.
How to Declare a variable in Java
To declare a variable follow this syntax:
data_type variable_name = value;
here value is optional because in java, you can declare the
variable first and then later assign the value to it.
Eg:
char ch = 'A';
int number = 100;
Variables naming convention in java
1) Variables naming cannot contain white spaces, for
example: int num ber = 100; is invalid because the variable name has space in
it.
2) Variable name can begin with special characters such as $
and _
3) As per the java coding standards the variable name should
begin with a lower case letter, for example int number; For lengthy variables
names that has more than one words do it like this: int smallNumber; int
bigNumber;
4) Variable names are case sensitive in Java.
5) Reserved words (like Java keywords, such as int or
boolean) cannot be used as names
Types:
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 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.
3) Static variable
A variable which is declared as static is called static
variable. It cannot be local. You can create a single copy of static variable
and share among all the instances of the class. Memory allocation for static
variable happens only once when the class is loaded in the memory.
Eg:
class A{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
}
}//end of class
Data Types in Java
Data types specify the different sizes and values that can
be stored in the variable. There are two types of data types in Java:
Primitive data types: The primitive data types include
boolean, char, byte, short, int, long, float and double.
Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays.
Data Type &
size
|
Default Value
|
Description
|
byte 1 byte
|
0
|
Stores whole
numbers from -128 to 127
|
short 2 bytes
|
0
|
Stores whole
numbers from -32,768 to 32,767
|
int 4 bytes
|
0
|
Stores whole
numbers from -2,147,483,648 to 2,147,483,647
|
long 8 bytes
|
0
|
Stores whole
numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
|
float 4 bytes
|
0.0f
|
Stores
fractional numbers. Sufficient for storing 6 to 7 decimal digits
|
double 8 bytes
|
0.0d
|
Stores
fractional numbers. Sufficient for storing 15 decimal digits
|
Boolean 1 bit
|
False
|
Stores true
or false values
|
char 2 bytes
|
'\u0000’
|
Stores a
single character/letter or ASCII values
|
Point to note:
why char uses 2 byte in java and what is \u0000 ?
It is because java uses Unicode system not ASCII code
system. The \u0000 is the lowest range of Unicode system.
Primitive number types are divided into two groups:
Integer types stores whole numbers, positive or negative
(such as 123 or -456), without decimals. Valid types are byte, short, int and
long. Which type you should use, depends on the numeric value.
Floating point types represents numbers with a fractional
part, containing one or more decimals. There are two types: float and double.
Non-Primitive Data Types
Non-primitive data types are called reference types because
they refer to objects.
The main difference between primitive and non-primitive data
types are:
Primitive types are predefined (already defined) in Java.
Non-primitive types are created by the programmer and is not defined by Java
(except for String).
Non-primitive types can be used to call methods to perform
certain operations, while primitive types cannot.
A primitive type has always a value, while non-primitive
types can be null.
A primitive type starts with a lowercase letter, while
non-primitive types starts with an uppercase letter.
The size of a primitive type depends on the data type, while
non-primitive types have all the same size.
Examples of non-primitive types are Strings, Arrays, Classes,
Interface, etc
Type Casting
Type casting is when you assign a value of one primitive
data type to another type.
In Java, there are two types of casting:
Widening Casting (automatically) - converting a smaller type
to a larger type size
byte -> short -> char -> int -> long -> float
-> double
Narrowing Casting (manually) - converting a larger type to a
smaller size type
double -> float -> long -> int -> char ->
short -> byte
public class Main {
public static void
main(String[] args) {
int myInt = 9;
double myDouble =
myInt; // Automatic casting: int to double
System.out.println(myInt); //
Outputs 9
System.out.println(myDouble); //
Outputs 9.0
}
}
public class Main {
public static void
main(String[] args) {
double myDouble =
9.78;
int myInt = (int)
myDouble; // Manual casting: double to int
System.out.println(myDouble); //
Outputs 9.78
System.out.println(myInt); //
Outputs 9
}
}
Operators
Operator Type
|
Category
|
Precedence
|
Unary
|
postfix
|
expr++ expr--
|
prefix
|
++expr --expr +expr -expr ~
!
|
Arithmetic
|
multiplicative
|
* / %
|
additive
|
+ -
|
Shift
|
shift
|
<< >> >>>
|
Relational
|
comparison
|
< > <= >= instanceof
|
equality
|
== !=
|
Bitwise
|
bitwise AND
|
&
|
bitwise exclusive OR
|
^
|
bitwise inclusive OR
|
|
|
Logical
|
logical AND
|
&&
|
logical OR
|
||
|
Ternary
|
ternary
|
? :
|
Assignment
|
assignment
|
= += -= *= /= %= &= ^= |= <<= >>= >>>=
|
class Unary OperatorExample{
public static void main(String args[]){
int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21
}}
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(~a);//-11 (minus of total positive value
which starts from 0)
System.out.println(~b);//9 (positive of total minus,
positive starts from 0)
System.out.println(!c);//false (opposite of boolean
value)
System.out.println(!d);//true
Arithmetic
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
Shift Operator
The Java left shift operator << is used to shift all
of the bits in a value to the left side of a specified number of times.
class LeftShiftOperatorExample{
public static void main(String args[]){
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(15<<4);//15*2^4=15*16=240
}}
The Java right shift operator >> is used to move left
operands value to right by the number of bits specified by the right operand.
class RightShiftOperatorExample{
public static void main(String args[]){
System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
}}
//For positive number, >> and >>> works
same
System.out.println(20>>2);
System.out.println(20>>>2);
//For negative
number, >>> changes parity bit (MSB) to 0
System.out.println(-20>>2);
System.out.println(-20>>>2);
5
5
-5
1073741819
Logical && vs Bitwise &
The logical && operator doesn't check second
condition if first condition is false. It checks second condition only if first
one is true.
The bitwise & operator always checks both conditions
whether first condition is true or false.
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a++<c);//false
&& true = false
System.out.println(a);//10 because second condition is not
checked
System.out.println(a<b&a++<c);//false &&
true = false
System.out.println(a);//11 because second condition is
checked
Logical || and Bitwise |
int a=10;
int b=5;
int c=20;
System.out.println(a>b||a<c);//true || true =
true
System.out.println(a>b|a<c);//true | true = true
//|| vs |
System.out.println(a>b||a++<c);//true || true =
true
System.out.println(a);//10 because second condition is not
checked
System.out.println(a>b|a++<c);//true | true =
true
System.out.println(a);//11 because second condition is
checked
Java Ternary Operator
Java Ternary operator is used as one liner replacement for
if-then-else statement and used a lot in Java programming. it is the only
conditional operator which takes three operands.
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
Java Assignment Operator
Java assignment operator is one of the most common operator.
It is used to assign the value on its right to the operand on its left.
Example
int a=10;
int b=20;
a+=4;//a=a+4 (a=10+4)
b-=4;//b=b-4 (b=20-4)
System.out.println(a);
System.out.println(b);
int a=10;
a+=3;//10+3
System.out.println(a);
a-=4;//13-4
System.out.println(a);
a*=2;//9*2
System.out.println(a);
a/=2;//18/2
System.out.println(a);
13
9
18
9
short a=10;
short b=10;
//a+=b;//a=a+b internally so fine
a=a+b;//Compile time error because 10+10=20 now int
System.out.println(a);
so,
a=(short)(a+b);//20 which is int now converted to short