Java
DataTypes
Java
Rules for variables
1. Case Sensitive
Ex :
int amount;
int Amount;
• These both are di erent variables
2. Contains Alphabets , Numbers _ or $
Ex :
int room1;
int cabin51;
int room_number
oat total$amount
fl
ff
3. Starts with Alphabet _ or $
Ex :
byte x1;
byte _x;
4. Should not be keyword
Ex : These are the keywords which we cannot use while declaring a
variable
5. Should not be class name , if class is also in use
Ex :
• If string class is used then the variable cannot have the
same name as the class
string
int string
6. No limit on length on names
• You can declare a variable name as small as possible or
as large as possible
7. Follows camel casing
The rst letter of a word must be capital except for the rst
word
Ex :
byte rollNumber;
fi
fi
Java
Literals
• Literals are the instant values that are used in a program
Ex :
Z=5*x+7+y
• Here 5 and 7 are literals
int value = 25;
• Here 25 is literal
• As variables have type so does literals too
• The examples mentions above are integer literals
• The example mentioned below is double literal . Any
number with decimal value is a double literal it’s not oat.
double price = 153.75;
fl
• Character literals are enclosed in ‘ ’ , if the character is
stored in. “ ” then its a string.
Ex :
String str = ‘java’
• The following Is the table which represents the type of literals for
di erent data types
ff
Java
Integral DataType
• The reason for java having short type of data is for smaller
number for e cient memory utilisation , therefore byte is also
used for the same purpose
• They are all signed datatype as all of them support both
positive and negative datatype
ffi
• In bytes one bit is reserved for sign value ( + , - )
• Number can be stored only in 7 bits
• The minimum number that can be stored in this 7 bits is 0
and max is 127
• The negative numbers are stored in two’s compliment form
Java
Float and Character Datatype
• A decimal number is not stored inside the memory but they
represent decimal number . so, in order to avoid it we move
the decimal point that means we oat that point hence we
call it as oat type
• The oat number is stored in binary form and its conversion
is done as follows the standard used for conversion is IEEE
754 format
Float
163.52
163.52 x 100 / 100
16352 x 1 / 100
-2
16352 x 10
• When you want to precise value in decimal point then we can
go for double because in some cases detail decimal value is
needed for accurate result.
• It supports upto 14 numbers under
fl
fl
fl
Char :
• The computer doesn’t support alphabets so we use numeric
code for theses characters . So, those codes are called ASCII
codes
• This is supported by other programming language
• Not only for English but they are codes available for every
natural language in the world as internet supports various
languages so must the machines too theses codes are called
unicodes
• Therefore ASCII codes comes under Unicode
ASCII Table :
DATA TYPES -VARIABLES AND
LITERALS
Data Types:
➢ Data is an important part of a program which is said as the ingredient of a program
➢ Variables are meant to store data.
➢ Data is stored only a8er the variable is declared.
➢ Data can be both signed and unsigned.
➢ Nega<ve numbers are stored in the form of 2’s complement
There are Basic built-in datatypes in the compiler of java.
There are four categories of primi<ve datatypes:
• Integral – they can have any numerical value without decimal point.
• Float – Have numerical value with decimal point.
• Char – for storing characters.
• Boolean – for storing true or false.
• Integral data type is divided depending on the size
o byte
o short
o int
o long.
o
➔ Floa<ng point is divided depending on the precision size –
• float -- decimal point is known as float as the decimal point is floated away.
Example: 163.25 is wriRen in the form of man<ssa and exponent as
16325*10^2 i.e 16325E-2.
IEEE standard number 754.
• double – for more precise value a8er decimal.
➔ Char values have 2 bytes (*Java uses Unicode).
• ASCII codes are subset of UNICODE
For A-Z = 65-90
a-z = 97-122
0-9 = 48-57
➔ Boolean – True is represented using ‘true’.
False is represented using ‘false’.
Variables
➢ Variables are names given to store data in memory alloca<on.
➢ Variables are to be declared and ini<alised.
➢Rules to name variables:
• variable names differ in upper and lower cases.
• contains alphabets, numbers and _, $.
• variables may start with an alphabet or _, $.
• it should not contain built-in words like int, float, if, while etc.
• it should not contain the built-in class names.
• name can be as long as possible.
• follow camel cases example- Roll Number, Average Marks.
Literals
➢ constant values(numbers) used in a program.
➢ there are different types of literals:
• int literals- represen<ng integer type.
• float and double literals- represen<ng decimal type.
• char literals-represen<ng characters in single quotes.
• string literals-represen<ng characters in double quotes.
➢ Literals are wriRen based upon the data types:
➔ int literal – int, short and byte.
• int literals can be represented in different number systems:
Decimal.
Binary.
Octal.
Hexadecimal.
• L or l – long.
• F or f – float.
• D or d – double.
• ’’ – char.
• true/false – Boolean.
➢ www.unicode.org
DATA TYPES -VARIABLES AND
LITERALS
Data Types:
➢ Data is an important part of a program which is said as the ingredient of a program
➢ Variables are meant to store data.
➢ Data is stored only a8er the variable is declared.
➢ Data can be both signed and unsigned.
➢ Nega<ve numbers are stored in the form of 2’s complement
There are Basic built-in datatypes in the compiler of java.
There are four categories of primi<ve datatypes:
• Integral – they can have any numerical value without decimal point.
• Float – Have numerical value with decimal point.
• Char – for storing characters.
• Boolean – for storing true or false.
• Integral data type is divided depending on the size
o byte
o short
o int
o long.
o
➔ Floa<ng point is divided depending on the precision size –
• float -- decimal point is known as float as the decimal point is floated away.
Example: 163.25 is wriRen in the form of man<ssa and exponent as
16325*10^2 i.e 16325E-2.
IEEE standard number 754.
• double – for more precise value a8er decimal.
➔ Char values have 2 bytes (*Java uses Unicode).
• ASCII codes are subset of UNICODE
For A-Z = 65-90
a-z = 97-122
0-9 = 48-57
➔ Boolean – True is represented using ‘true’.
False is represented using ‘false’.
Variables
➢ Variables are names given to store data in memory alloca<on.
➢ Variables are to be declared and ini<alised.
➢Rules to name variables:
• variable names differ in upper and lower cases.
• contains alphabets, numbers and _, $.
• variables may start with an alphabet or _, $.
• it should not contain built-in words like int, float, if, while etc.
• it should not contain the built-in class names.
• name can be as long as possible.
• follow camel cases example- Roll Number, Average Marks.
Literals
➢ constant values(numbers) used in a program.
➢ there are different types of literals:
• int literals- represen<ng integer type.
• float and double literals- represen<ng decimal type.
• char literals-represen<ng characters in single quotes.
• string literals-represen<ng characters in double quotes.
➢ Literals are wriRen based upon the data types:
➔ int literal – int, short and byte.
• int literals can be represented in different number systems:
Decimal.
Binary.
Octal.
Hexadecimal.
• L or l – long.
• F or f – float.
• D or d – double.
• ’’ – char.
• true/false – Boolean.
➢ www.unicode.org