01_3_Data_Types
01_3_Data_Types
Keywords
Datatypes
Identifiers
Variables
Access Specifier
Type Casting
int (two's complement integer) 0 4 byte -214 748 3648 to 214 748 3647 (inclusive)
float (7decimal places) 0.0f 4 byte single-precision 32-bit IEEE 754 floating
point.
1.4e-45f to 3.4e+38f
double (15decimal places) 0.0d 8 byte double-precision 64-bit IEEE 754 floating
point
4.9e-324d to 1.7e+308d
1 bit
Unicode System
Universal international standard character encoding that is capable of
representing most of the world's written languages
lowest value:\u0000
highest value:\uFFFF
Wrapper classes: These classes provide a way to use primitive data types as objects.
For example, Integer for int, Double for double, and so on.
Identifiers
All Java variables must be identified with unique names.
Identifiers must start with a letter, $ ,underscore ( _ ) but cannot start with a
number.
Can't use a Java keyword as an identifier.
Are Case sensitive (Sab Chalega)
characters [A-Z] or [a-z] or numbers [0-9], and underscore(_) or a dollar sign ($).
There should not be any space in an identifier.
An identifier should not be any query language keywords such as SELECT, FROM,
COUNT, DELETE, etc.
Java Variables
A variable is the name of a reserved area allocated in memory. In other words,
it is a name of the memory location.
Memory is associated to data type and can assign value.
There are three types of variables in java: local, instance and static.
Should not start with the special characters like & (ampersand), $ (dollar), _
(underscore).
1) Local Variable
A variable declared inside the body of the method is called local variable.
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.
It is called an instance variable because its value is instance-specific and is not shared
among instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You
can create a single copy of the static variable and share it among all the instances of
the class. Memory allocation for static variables happens only once when the class is
loaded in the memory.
Initialization of a variable with a
primary value
int n1; //declare
n1 =21 ; // assignment
int i2 = 18; // initialization
char ch = ‘S’; // initialization
double d = 21.8; // initialization
d = n1; // assignment
float f1 = 16.13F; //initialization
access modifiers:
scope of a field, method, constructor, or class.
We can change the access level of fields, constructors, methods, and class by
applying the access modifier on it.
Private:
The access level of a private modifier is only within the class.
It cannot be accessed from outside the class.
Default:
The access level of a default modifier is only within the package.
It cannot be accessed from outside the package.
If you do not specify any access level, it will be the default.
Protected:
The access level of a protected modifier is within the package and outside the
package through child class.
If you do not make the child class, it cannot be accessed from outside the package.
Public: The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.
non-access modifiers, such as static, abstract, synchronized, native, volatile,
transient, etc.
Here, we are going to learn the access modifiers only.
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
Error
4) Public
The public access modifier is accessible everywhere. It has the widest scope among all other
modifiers.
(within class, within package, outside package by subclass, outside package)
Type Casting(Narrowing)
Output:
10.5
10
Output:
130
-126
Converting a higher data type into a lower one is called narrowing type casting.
It is also known as explicit conversion or casting up.
double -> float -> long -> int -> char -> short -> byte
Assignment
int a = 1, b = 2, c = 5;
a=b=c;
a = b = c;
1) b=c //b=5
System.out.print("a= " + a + " b= " + b + " c= " + c);
2) a=b //a=5
so
o/p: a=5 b=5 c=5 a=5,b=5,c=5