Variable and Datatype in Java
- Variable
- Types of Variable
- Data Types in Java
In
this page, we will learn about the variable and java data types. Variable is
a name of memory location. There are three types of variables: local,
instance and static. There are two types of datatypes in java, primitive and
non-primitive.
|
Variable
Variable is name of reserved
area allocated in memory.
1 int data=50;//Here data is variable
Types of
Variable
There
are three types of variables in java
·
local variable
·
instance variable
·
static variable
|
Local Variable
A
variable that is declared inside the method is or constructor or static or
initializer block called local variable.
Local
variable cannot have public, private, protected and default.
Local
variable must initialize before use.
Scope
of local variable only inside the methods or constructors or block where it
is declare.
|
Instance Variable
A
variable that is declared inside the class but outside the method is called
instance variable . It is not declared as static.
|
Static variable
A
variable that is declared as static is called static variable. It cannot be
local.
|
We
will have detailed learning of these variables in next chapters.
|
Example to understand the types of variables
1. class A{
2. int data=50;//instance variable
3. static int m=100;//static variable or
class variable
4. void method(){
5. int n=90;//local variable
6. }
7. }//end of class
Data
Types in Java
Data types defines the type of data.
In
java, there are two types of data types
·
primitive data types
·
non-primitive data types
|
Data Type
|
Default Value
|
Default size
|
boolean
|
false
|
1 bit
|
char
|
'\u0000'
|
2
byte
|
byte
|
0
|
1
byte
|
short
|
0
|
2
byte
|
int
|
0
|
4
byte
|
long
|
0L
|
8
byte
|
float
|
0.0f
|
4
byte
|
double
|
0.0d
|
8
byte
|
Why char
uses 2 byte in java and what is \u0000 ?
because java uses unicode
system rather than ASCII code system. \u0000 is the lowest range of unicode
system.To get detail about Unicode see below.
Unicode System
Unicode
is a universal international standard character encoding that is capable of
representing most of the world's written languages.
|
Why java
uses Unicode System?
Before
Unicode, there were many language standards:
|
·
ASCII (American Standard Code for Information
Interchange) for the United States.
·
ISO 8859-1 for Western European Language.
·
KOI-8 for Russian.
·
GB18030 and BIG-5 for chinese, and so on.
|
This caused two
problems:
1. A
particular code value corresponds to different letters in the various
language standards.
2. The
encodings for languages with large character sets have variable length.Some
common characters are encoded as single bytes, other require two or more
byte.
|
To
solve these problems, a new language standard was developed i.e. Unicode
System.
|
In
unicode, character holds 2 byte, so java also uses 2 byte for characters.
|
lowest value:\u0000
|
highest value:\uFFFF
|
Operators in java
Operator in java is a symbol that is used to perform
operations. There are many types of operators in java such as unary operator,
arithmetic operator, relational operator, shift operator, bitwise operator,
ternary operator and assignment operator.
Operators
|
Precedence
|
postfix
|
expr
++ expr-- |
unary
|
++ expr -- expr + expr - expr ~ ! |
multiplicative
|
* / % |
additive
|
+ - |
shift
|
<< >> >>> |
relational
|
< > <= >= instanceof |
equality
|
== != |
bitwise
AND
|
& |
bitwise
exclusive OR
|
^ |
bitwise
inclusive OR
|
| |
logical
AND
|
&& |
logical
OR
|
|| |
ternary
|
? : |
assignment
|
= += -= *= /= %= &= ^= |= <<= >>=
>>>= |
No comments:
Post a Comment