HIRANANDANI FOUNDATION SCHOOL, THANE
SUBJECT: COMPUTER STUDIES
STD: VIII -TERM 3 – 2023-24
INTRODUCTION TO JAVA
Java is a high-level programming language
used for
utilities, (An utility is an important service such as water, electricity, or gas that is
provided for everyone, and everyone pays for it.)
games and
business applications.
Advantages
Java is fast (the conversion into machine code is faster), secure (many security related features)
and reliable (Java, can handle huge amounts of data. It has the high probability of failure-free
operation of a computer program. It also provides features such as automatic garbage collection
and dynamic memory allocation.)
Used in
laptops,
datacenters,
game consoles,
scientific supercomputers,
cell phones,
Internet etc.
Popularity of Java is because of:
• Platform Independent:
▫ Possible because of concept of WORA (Write once run anywhere)
Same Java Program can be run anywhere as long as they have
Java Runtime Environment (JRE) is installed and running.
• Simple:
▫ Programs are easy to write and debug.
History of Java
• Was first released by Sun Microsystems in May 1995.
• James Gosling, Patrick Sherida and James Naughton are credited with designing Java
• To create Java Programs, we need
▫ Integrated Development Environment (IDE).
• Well known Java Software:
▫ Java Netbeans
▫ Eclipse
▫ IntelliJ
▫ BlueJ best IDE for beginners
BLUEJ
• It is a Java Integrated Development Environment
▫ Specially designed for teaching
▫ Useful for familiarizing oneself with
🞄 Java Syntax
🞄 Basic software development.
Object Oriented Programming Features
• Modern approach to programming
• Concept implemented using objects
• Objects communicate with each other through functions
• Divides the program into objects
• e.g. Java, C++, Python
Page 1 of 4
Structure of a class
KEYWORDS
⚫ Keywords are the words that convey a special meaning to the language compiler
⚫ These are reserved for special purpose and must not be used as normal identifier names.
⚫ Examples: public, class, void etc.
1. “public class Helloworld” 2. “public void main ( )”
This line has various aspects of java programming. It is a method (function) named main with string array
a. public: This is access modifier keyword as an argument.
which tells compiler access to class. Various a. public: Access Modifier
values of access modifiers can be public, b. void: This keyword declares nothing would
protected, private or default (no value). be returned from the method. The method may
b. class: This keyword used to declare a class. Name return any primitive data type or object.
of class (HelloWorld) followed by this keyword. c. main(): Method content inside curly braces. { }
3. Comments section: 4. System.out.println("Hello") :
We can write comments in java in two ways. a. System: It is the name of Java utility class.
a. Line comments: It starts with two forward b. out: It is an object which belongs to System class.
slashes (//) and continues to the end of the c. println: It is utility method name which is used to
current line. Line comments do not require an send any String to the console (output /terminal
ending symbol. window).
b. Block comments: It starts with a forward slash d. “Hello”: It is String literal set as argument to println
and an asterisk (/*) and end with an asterisk and a method.
forward slash (*/). Block comments can also extend
across as many lines as needed.
In Java, the semicolon is a separator that is used to terminate a statement.
That is each individual statement must be ended with a semicolon.
DATA TYPES
• Data types are means to identify the type of data and associated operations of handling it.
Numeric Datatypes Non-Numeric Datatypes
Type Description Type Description
int whole numbers char Single character
double Precision up to 15 digits String Group of characters
Java treats fractional numbers as of double datatype
e.g. If you write 0.234 it will be treated as a double value.
Boolean datatype
Type Size Description Range Remarks
boolean 1 bit Logical or True or false (these Useful in logic
boolean values are reserved words) test with if
Page 2 of 4
LITERALS
Literals are data items that have fixed data values.
• Kinds of literals:
▫ Integer literal ▫ Floating literal
▫ Character literal ▫ String literal
INTEGER LITERALS FLOATING LITERALS
=> Integer literals are whole numbers without any => Numbers having fractional parts
fractional part. => Also called Real literals
CHARACTER LITERALS STRING LITERALS
=> A character literal is one enclosed in single quotes.
=> Characters that cannot be typed directly from => Multiple character constants are treated as
keyboard are called ‘Nongraphic characters’. e.g. string literals.
backspace, tab, spacebar, carriage return etc. => A string literal is of class type String. e.g. “abc”
=> These can be represented by using escape sequences.
VARIABLES
• Variables represent named storage locations, whose values can be manipulated during program run.
• e.g. student, rollno or student name
Declaration of a variable
◦ Syntax:- datatype variablename;
e.g. int age; double salary; long distance;
When more than one identifier is being defined, a comma-separated list of identifiers may follow the type
specifier. e.g. int month, day, year; double wage, netsal;
Initialization of variables
• A variable with a declared first value is said to be an initialized variable.
e.g. int val = 1001; double price = 214.70, discount = 0.12;
Expressions in Java
An expression is a construct made up of operands and operators, which is constructed according to
the syntax of the language that evaluates to a single value.
Shorthand Operator
It is used to write Java expression into short form provided same variables are used after and before the
assignment sign (=) e.g. n += 4 is equivalent to n=n+4
OPERATORS IN JAVA: -
ARITHMETIC OPERATORS
The operators that are used to perform arithmetical operations on the operands.
They operate on Binary Operators, that means they take 2 operands.
Operator Name Description Example
+ Addition Adds together two int a =10; int b=5; int c = a + b;
operands Adding a and b then assigning the result (10+5= 15) to c.
- Subtraction Subtracts one operands int a =8; int b=2; int c = a - b;
from another Subtracting b from a then assigning the result (8-2=6) to c.
* Multiplication Multiplies two int a =4; int b=3; int c = a * b;
operands Multiplying a and b then assigning the result (4*3=12) to c.
/ Division Divides one operand int a =14; int b=2; int c = a / b;
by another Dividing a by b then assigning the result (14/2=7) to c.
% Modulus Returns the division int a =15; int b=2; int c = a % b; Dividing a by b then
remainder assigning the result i.e. reminder (15/2=1) to c.
Page 3 of 4
RELATIONAL OPERATORS
Relational Operators are used to comparing two variables for equality, non-equality, greater than, less than, etc.
Java relational operator always returns a boolean value i.e. either - true or false
Flow of the Program
• Whenever we execute a program in any programming language, the control reaches the first
line and starts executing in a sequential manner.
• After executing the last line of the program the control gets terminated.
• This sequential flow of control is also called as the normal flow of the program.
• This takes place by default.
Conditional Flow of Control
• When in instances, the user may need to transfer the control to a specified location by skipping some
lines, we use conditional flow of control.
• To apply this, we have control statements like
▫ if
▫ Switch
▫ loops
if - types
1. if
• Used to check a specific condition.
• It considers only the true case of the condition.
• Also referred to as bi-directional branching.
Syntax:
if (condition)
{
yes
Statement 1 Is condition
satisfied?
Statement 2
.....
}
2. if – else
Syntax:
Eg:
if (condition)
{ if A>B
Statement (s) 1 Max = A yes
Is condition
} else satisfied?
else Max = B
{
Statement (s) 2
no
}
Page 4 of 4