Java Basics: Basic
Concepts
Programming Fundamentals in Java
Basic Concepts
Object – has states and behaviors. It is an instance of a class.
It is created everytime you instantiate a class with a new
keyword.
Class – provides a template for defining objects. It does not
necessarily have to represent real object, it can also be a
representation of a concept, process, etc.
Method – is basically a behavior of an object. This is where the
logics are written, data is manipulated and all actions are
executed.
Attributes – instance variables of a class. These are properties
of an object. Each object has its own unique set of these. An
object’s state is created by the values assigned to these.
Basic Concepts
Statement – refers to one or more lines of code
terminated by a semicolon.
Block - is one or more statements surrounded by an
opening and closing curly braces that groups the
statement as one unit.
Package – refers to the grouping of classes and/or
subpackages. Its structure is similar to that of a
directory.
Java Identifier
Name used for classes, variables, and methods. Pointers for
identifiers:
Pointers for identifiers:
▪ All identifiers should begin with a letter (A to Z or a to z), currency
character ($) or an underscore (_).
▪ After the first character, identifiers can have any combination of
characters.
▪ A keyword cannot be used as an identifier.
▪ Most importantly, identifiers are case sensitive
▪ Examples of legal identifiers: age, $salary, _value, __1_value.
▪ Examples of illegal identifiers: 123abc, -salary.
Java Modifiers
Modifiers are keywords that are added to change
meaning of a definition. In Java, modifiers are
categorized into two types:
▪ Access Modifiers – modifiers to control
accessibility of a class, attribute, and method
▪ Non-Access Modifiers – unlike the access control
modifiers, this does not change the accessibility of
class, attribute and method, but it provides them
special properties.
Access Modifiers
▪ default: has scope only inside the package
▪ public: has scope that is visible everywhere
▪ protected: has scope within the package and all
sub classes
▪ private: has scope only within the classes
Non-Access Modifiers
▪ final: it can be used with class variable, a method, or a class. It is
used to prevent modification of the class variable, method, and
class.
▪ static: used to create class variables and class methods which
can be accessed without instance of a class.
▪ transient: used with instance variable. The value of the instance
variable does not persist when an object is serialized.
▪ synchronized: when a method is synchronized it can be accessed
by only one thread at a time.
▪ volatile: it can only be used with a variable. It tells the compiler
that the volatile variable can be changed unexpectedly by
other parts of your program. Volatile variables are used in case
of multithreading program
Java Data Types
▪ Primitive Data Type
There are eight primitive datatypes supported by Java.
Primitive datatypes are predefined by the language and
named by a keyword
▪ Reference/Object Data Type
Reference variables are created using defined constructors of
the classes. They are used to access objects. These variables
are declared to be of a specific type that cannot be changed
Class objects and various type of array variables come under
reference datatype
Default value of any reference variable is null
Primitive Data Types
DATA TYPE MIN – MAX DEFAULT REMARKS EXAMPL
E
byte -128 to 127 0 A byte is four times byte a =
smaller than an 100;
integer. byte b = -
50;
short -32,768 to 0 A short is 2 times short a =
32,767 smaller than an integer 100;
short b =
-2000;
int -2,147,483,648 0 Integer is generally int a =
to 2,147,483,647 used as the default 100;
data type for integral int b = -
values 100
long - 0L; This type is used when long a =
9,223,372,036,85 a wider range than int 1000L;
4,775,808 to - is needed long b =
Primitive Data Types
DATA TYPE MIN – MAX DEFAULT REMARKS EXAMPL
E
float -single-precision 0.0f mainly used to save float a =
32-bit IEEE 754 memory in large arrays 234.7f;
floating point of floating point
numbers.
double double-precision 0.0d generally used as the double a
64-bit IEEE 754 default data type for = 123.7;
floating point decimal values
boolean true or false true boolean a
= true;
char \u0000 to \uffff 0L; Char data type is used long a =
to store any character 1000L;
This type is used when long b =
a wider range than int -2000L;
is needed
Java Variables
▪ Instance Variables: Instance variables are
variables within a class but outside any method.
These variables are initialized when the class is
instantiated. Instance variables can be accessed
from inside any method, constructor or blocks of
that particular class.
▪ Class Variables: are variables declared within a
class, outside any method, with the static keyword
Java Variables
▪ Local Variables: variables defined inside
methods, constructors or blocks are called local
variables. The variable will be declared and
initialized within the method and the variable will be
destroyed when the method has completed
Java Methods
A collection of statements that are grouped together
to perform an operation.
Java Constructor
special type of method used for creating and
initializing a new object.