Basics of JAVA language
Program: A set of instructions written using a computer programming language to
perform a particular task is called a program.
The process of creating these programs is called programming or program coding.
Programming languages: The languages used to write computer programs are called
programming languages e.g. BASIC, COBOL, JAVA, Python etc.
Programming languages are mainly of two types:
1. POP – Procedure Oriented Programming languages
2. OOP – Object Oriented Programming languages
1. POP languages focus upon procedures or routines for solving any problem in a
step by step manner. Examples: BASIC, C language etc.
2. OOP languages focus upon data and set of operations that work upon the data.
These languages work with objects and classes and allow to design programs using a
modular approach. Examples: Java, Python, C++ etc.
JAVA : Java is an object oriented high level programming language created by James
Gosling at Sun Microsystems in 1995. It was later taken over by Oracle.
Features of Java:
• Java programs are both compiled and interpreted.
• Java is platform independent means Java programs can run on any operating
System.
• Java is case sensitive which means it treats upper and lowercase letters
differently.
• Java is Object Oriented, organizes codes into objects making it modular and
easy to maintain large programs.
• Java programs are robust because of strong error checking mechanism
(Compile time as well as Runtime).
• Java is Secured due to the property of encapsulation.
• Java is simple and easy to learn.
Object Oriented Programming
Class
Objects
Attributes Behaviour
Components of a Java Program:
1. TOKENS: The smallest elements of a Java program. Every statement written in Java
is made up of tokens.
• Keywords: Reserved words like class, int, if, for etc.
• Identifiers: Names given to variables, methods and classes.
• Literals: Fixed values like numbers, characters, strings etc.
• Operators: Symbols like +, -, *, /, == etc.
• Separators or Special Symbols: Characters like { , } , [ , ] , ( , ) , ; etc.
Example:
int age = 25;
Above statement in Java is assigning a fixed value 25 to an integer type variable age.
Here ‘int’ is the keyword, ‘age’ is an identifier, ‘=’ is an operator, ‘25’ is a literal and
‘;’ is a special symbol or separator.
2. Keywords are reserved words in Java which have some predefined meaning to
the Java compiler. They can not be used as identifiers. Example: int, float,
double, Boolean, class, void, public, static etc.
3. Identifiers and their naming rules:
Identifiers are the names given to variables, methods, classes or objects. They
follow specific rules:
• Java keywords can not be used as identifiers.
• They may contain alphabet A-Z or a-z, 0-9 numerals, an underscore ( _ ) or a
dollar sign ( $ ).
• Spaces or any other special characters can not be used.
• Identifier names can not begin with a number.
• Identifiers are Case sensitive (means main and Main are considered different).
• Valid Identifiers: age, a1, a$, even_odd etc.
• Invalid Identifiers: int, even odd, 1a, 123 etc.
4. Literals: These are fixed values directly used in Java programs.
a. Integer Literals: whole numbers, without any decimal part e.g. 100, -50.
b. Floating literals: Decimal or real numbers e.g. 25.50, -2.5.
c. Character literals: A single character within a single quote e.g. ‘A’, ‘9’, ‘z’.
d. Boolean literals: true or false.
e. String literals: Text enclosed in double quotes e.g. “St. John’s, BLW”.
Examples:
int num = 100;
float price = 49.99f;
char ch = ‘S’;
String name = “Karishma”;
Boolean b = true;
5. Data Types: Data types specify the type of data a variable can hold. It also allots
memory for efficient processing.
A. Primitive data types:
a. Integers or whole numbers (100, -50) can be stored in:
byte, short, int or long types of variables.
Examples:
byte age = 14;
short salary = 25500;
int gross = 1024 * 30;
long mb = 1024 * 1024;
b. Floating or real numbers (50.5, -22.69) can be stored in:
float or double type of variables.
Examples:
float percentage = 98.66f;
double Amount = 2500.50;
c. single character constants are stored in char type of variables.
Example: char choice = ‘A’;
d. Boolean values are stored as true or false.
boolean b = true;
B. Non-primitive Data types:
They include arrays, Strings and classes.
Size chart of primitive data types:
Data Data Type Bit Size
Integer byte 8 bits (1 byte)
short 16 bits (2 bytes)
int 32 bits (4 bytes)
long 64 bits (8 bytes)
Real or decimal float 4 bytes
double 8 bytes
Characters char 2 bytes
6. Operators:
Operators are special symbols which perform operations on operands(values). They
are mainly of two types: 1. Unary operators- operate upon single operand.
Examples: unary plus ’+’, unary minus ‘-‘, increment ‘++’, decrement ‘—', logical not !
2. Binary operators- operate upon two operands. Examples: +, -, *, /, <, >, == etc.
Operators Type
+, -, *, /, % Arithmetic operators
>, <, >=, <=, ==, != Relational operators
&& (AND), || (OR), ! (NOT) Logical Operators
=, +=, -=, *=, /=, %= Assignment operators