Chapter 2 - Java Basics
Chapter 2 - Java Basics
Java Basics
Byte
Default value is 0
Byte data type is used to save space in large arrays, mainly in place of
integers, since a byte is four times smaller than an integer.
Example:
byte employeeId = 121;
Short
Short data type is a 16-bit signed two's complement integer
Short data type can also be used to save memory as byte data type. A
short is 2 times smaller than an integer
Default value is 0.
Example:
short employeeAge = 21;
int
Int data type is a 32-bit signed two's complement integer.
Integer is generally used as the default data type for integral values
unless there is a concern about memory.
The default value is 0
Example:
int customerId = 122;
long
Long data type is a 64-bit signed two's complement integer
This type is used when a wider range than int is needed
Default value is 0L
Example:
long customerContactNumber = 9623639693L;
float
Float data type is a single-precision 32-bit IEEE 754 floating point
Float data type is never used for precise values such as currency
Example:
float productPrice = 5.75f;
double
double data type is a double-precision 64-bit IEEE 754 floating point
This data type is generally used as the default data type for decimal
values, generally the default choice
Double data type should never be used for precise values such as
currency
Default value is 0.0d
Example:
double employeeSalary = 90000.96d;
boolean
boolean data type represents one bit of information
There are only two possible values: true and false
This data type is used for simple flags that track true/false conditions
Default value is false
Example:
boolean isStatusOnline = true
char
char data type is a single 16-bit Unicode character
Char data type is used to store any character
Default value is '\u0000'
Example:
char employeeGrade = 'A';
There are certain rules for defining a valid java identifier. These
rules must be followed, otherwise we get compile-time error. These
rules are also valid for other languages like C,C++.
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.
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.
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.
class Customer{
int customerId=121;//instance variable
static int customerAge=22;//static variable
void show(){
int customerName=”JERRY”;//local variable
}
}//end of class
The keywords const and goto are reserved, even though they
are not currently used. true, false, and null might seem like
keywords, but they are actually literals; you cannot use them
as identifiers in your programs.
3. Documentation comments.
This type of comments are used generally when writing code for
a project/software package, since it helps to generate a
documentation page for reference, which can be used for getting
information about methods present, its parameters, etc.
Syntax:
/**
This
is
documentation
comment
*/
Example:
Package Types-
1. User Defined
These are the packages that are defined by the user.
2. In Built
These packages consist of a large number of classes which
are a part of Java API.Some of the commonly used built-in
packages are:
1) java.lang: Contains language support classes(e.g
classed which defines primitive data types, math
operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output
operations.
3) java.util: Contains utility classes which implement data
structures like Linked List, Dictionary and support ; for
Date / Time operations.
Syntax:
package com.fullstack.hdfc.loan.aggreeloan.emi.advance
package com.fullstack.hdfc.loan.aggreeloan.emi.preclosure
package com.fullstack.hdfc.loan.homeloan.emi.advance
2.7 TypeCasting
Typecasting means convert one primitive datatype into another
type.