Java_notes_cp
JAVA
A computer Program is a set of instructions that performs a specific task.
A collections of computerPrograms, libraries and related data are referred to as software.
A programming Language is a language used to write programs or it is a vocabulary and set of
grammatical rules for instructing a computer to perform specific tasks.
A programming language is a language used to write programs. or
A programming language is a vocabulary and set of grammatical rules for instructing a computer to
perform specific tasks.
There are two types of software's.
System software.
Application software.
System Software Application Software
These are the essential software These are the external software
for the system hardware to work. which provides additional
System software's are installed on functionality to user.
the computer when operating Application software's are installed
system is installed. according to user's requirements.
Some examples are operating Some examples are MS Office, web
system (OS), compiler, assembler, browser, media player, games,
interpreter, drivers, etc. facebook etc.
A computer programmer or coder, is a person who creates computer
software.
The term computer programmer can refer to a specialist in one area of
computers, who writes code for many kinds of software.
A web programmer or coder, is a person who creates web applications.
A range of occupations that involve programming are, for example:
software developer, web developer, mobile applications developer,
embedded firmware developer, software engineer, computer scientist,
game programmer, game developer and software analyst.
Computer understands only binary or low level or machine level language
i.e.(o's and 1's
High level languages are also known as Human readable language because
it contains words.
Programming languages are those which use compiler.
Scripting languages are those which use Interpreter.
1|Page
Java_notes_cp
Compiler Interpreter
Compiler is used to translate Interpreter is used to
complete High level translate complete High level
language program to low language program to low
level at Once. level line by line.
We get Multiple errors at a We get one error at a time.
time. Error debugging is easy.
Error debugging is Difficult. Interpreted code run slower.
Compiled program run Python, Javascript PHP, Perl,
faster. Ruby uses an interpreter.
C, C++,C#, Scala, Java all
use complier.
Java Programming Features
1. Java Supports both Procedure-oriented and object oriented
programming concepts.
2. High-level programming language.
3. Java is Case Sensitive.
4. Java is Platform Independent Language.
5. Java Supports Multithreading and Exception Handling.
Importat:
2|Page
Java_notes_cp
Java: Java is a high-level, object-oriented programming language.
Developed by Sun Microsystems (now owned by Oracle
Corporation).
Java is used for a wide range of applications, including:
1. Web development (e.g., servlets, JavaServer Pages)
2. Mobile app development (e.g., Android apps)
3. Desktop applications (e.g., NetBeans, Eclipse)
4. Enterprise software development
5. Games development
6. Internet of Things (IoT) development
Java is known for its:
1. Platform independence
2. Object-oriented design
3. Robust security features
4. Large community of developers and resources
5. Extensive libraries and frameworks
Java has many benefits, including:
1. Easy to learn
2. High-level language (abstracts away low-level details)
3. Platform independent
4. Secure
5. Large community and resources
Overall, Java is a versatile and widely used language that has become
a standard in many industries and applications.
3|Page
Java_notes_cp
Functions/Methods
In Java,All functions defined within a class.
Class hello
public static void main(String args[])
Statements
❖ Shortcut:main+ctrl+space
Process Involved
4|Page
Java_notes_cp
Buit in Classes:[Tittle Case]
System Scanner String ArrayList
Math LinkedList
Buit in Function: [Camel Case]
next() nextInt() nextFloat() nextDouble()
nextCharArray() toString() toLowerCase()
F11:Run Program
Ctrl+shift++++:Zoom in
Ctrl+shift---:Zoom out
Variable:
Variables must be initiallised before using in Program. Used to store data . It can
be changed or reduced many times.
Syntax
Datatype Variablename=value;
Data Types in Java
A data type decides the type of data stored by variable.
It also decides how much memory should be allocated to a variable.
Basic Data Types Memory
byte 1
short 2
int 4
float 4
char 1
double 8
5|Page
Java_notes_cp
Keywords in Java:
A keyword is a reserved word. You cannot use it as a variable name, function
name, constant name, array name etc.
Final double break case char class continue else enum
static float For public if int long private
return short protected try static switch throws throw
finally void catch while do default
Identifiers:
The name given to variable, array, string, class etc..
Rules for declaring variables/Identifiers
A variable can have alphabets, digits, and underscore.
A variable name can start with the alphabet, and underscore only. It can't start with a digit.
No whitespace is allowed within the variable name.
No Special characters is allowed within the variable name.
A variable name must not be any reserved word or keyword, e.g. int, float, etc.
OutPut Statement/printing Statement:
System.out.println(“statement”);
Key notes to remember:
1. All Java program must have main() function and it's mandatory.
2. Java is case-sensitive; the use of uppercase letter and lowercase letter have
different meanings.
3. The statement in a Java program ends with a semicolon.
6|Page
Java_notes_cp
Reading in java
After Input Statement you have to Insert a line”import java.util.Scanner;”
In main Function( S must be in capital):
”Scanner Scannername=new Scanner(system.in)”.
example
Int: variablename=scannername.nextInt();
Float: variablename= scannername.nextFloat();
Char: variablename= scannername.next().char At(0);
String: scannername.next();
The Final keyword is used to define constant in JavaProgram,But in C it is Const.
7|Page
Java_notes_cp
Comments in Java
Comments in Java language are the non-executable statements, used to provide information
about lines of code. It is widely used for documenting code.
There are 2 types of comments in the Java language.
1) Single Line Comments(//).
2) Multi-Line Comments( /* statements*/ ).
Create object for Class
Classname objname=new classname();
Scanner v=new Scanner(System.in);
Step 1: We need to import Scanner class from java.util package. (before class declaration)
import java.util.Scanner;
Step 2: We need to create object of Scanner class. (within main function)
Scanner sc=new Scanner(System.in);
8|Page
Java_notes_cp
Operators
An operator is a symbol perform various operations.
Java Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical
Values(constants and variables).
9|Page
Java_notes_cp
Java Increment and Decrement Operators
Java programming has two operators increment ++ and decrement --
to change the value of an operand (variable) by 1.
Increment ++ increases the value by 1 whereas decrement --
decreases the value by 1. These two operators are unary operators,
meaning they only operate on a single operand.
There are two types :
Pre Increment&Decrement.
Post
Pre-Increment&Decrement:
10 | P a g e
Java_notes_cp
Post-Increment&Decrement:
Java Relational Operator:
11 | P a g e
Java_notes_cp
Java Conditional Operator/ Ternary Operator
Conditional operators return one value if condition is true and
returns another value if condition is false.
Variable (condition)? true value: false value;
Syntax:
class hello
{
public static void main(String ar[])
{
int a, b;
a=(10<20)740:80;
b=(10>20)750-90;
System.out.println("a="+a);
System.out.println("b="+b);
}
}
12 | P a g e
Java_notes_cp
Java Assignment Operators
An assignment operator is used for assigning a value to a variable.
The most common assignment operator is =.
13 | P a g e
Java_notes_cp
Java Logical Operators
Logical Operators are used to compare expressions and returns true or false. Logical operators are
commonly used in decision making in Java programming.
14 | P a g e
Java_notes_cp
Control Statements
If Statements:
If else Statements:
To Find Vowel/consonants
15 | P a g e
Java_notes_cp
To Find odd/Even
To Find LeapYear
16 | P a g e
Java_notes_cp
Using Condition
To get Pass/Fail
17 | P a g e
Java_notes_cp
To Find Distinction
To get Pass/Fail
18 | P a g e
Java_notes_cp
Write a program using if else to show people have Discont or Not.
19 | P a g e
Java_notes_cp
If else if Statements:
Write a program using if else if to show
UpperCase/LowerCase/Digits/Speical Characters.
20 | P a g e
Java_notes_cp
Write a program using if else if to show Distinction,First class,Second
class.
21 | P a g e
Java_notes_cp
To get Mathematical Operations
22 | P a g e
Java_notes_cp
To Check whether Divisible by Two Number
23 | P a g e
Java_notes_cp
Java Switch Program
The switch statement in java allows us to execute multiple operations for the
different possible values of a single variable called switch variable.
Here, We can define various statements in the multiple cases for the different
values of a single variable.
The syntax of switch statement in java language is given below:
switch(value)
{
case value1: //code to be executed;
break;
case value2: //code to be executed;
break;
case value3: //code to be executed;
break;
………… ……………………
default: // code to be executed if all cases are not matched;
}
Rules for switch statement in Java language:
The case value must be an integer or character constant.
The break statement in switch case is not must. It is optional. If there is no break
statement found in the case, all the cases will be executed present after the
matched case.
Default case in switch is optional.
The statements corresponding to the matching case will be executed.
If the value doesn't matches with any of the cases, then the statements
corresponding to default case will be executed
24 | P a g e
Java_notes_cp
25 | P a g e
Java_notes_cp
Looping Statements
A looping statement allows us to execute a statement or group of statements
multiple times.
while, do While and for loop repeatedly executes a statement or group of
statements as long as a given condition is true.
There are 2 types:
Pre tested Loop:(condition checks first).
{While loop, For loop} .
Post tested Loop:(condition checks later).
{Do while}.
Using Do While Printing Different Pattren of Numbers
26 | P a g e
Java_notes_cp
27 | P a g e
Java_notes_cp
Write a Program to Print Square&Cube in Range 1 to 20
To Find Area of Triangle
28 | P a g e
Java_notes_cp
Write a Program to Find Odd/Even
Write a Program to print (50,45,40…..30)in this Order
29 | P a g e
Java_notes_cp
Dowhile Loop
30 | P a g e
Java_notes_cp
ForLoop
Using forloop finding prime number
31 | P a g e
Java_notes_cp
Continue with Array
32 | P a g e
Java_notes_cp
C Jumping Statements
break:( used within switch and loops).
The break statement stops the execution of the loop or switch when
it is encountered.
continue: used only within loops.
The continue statement skips the current iteration of the loop and continues
with the next iteration. (or) It takes control to the beginning of the loop.
33 | P a g e
Java_notes_cp
34 | P a g e
Java_notes_cp
35 | P a g e
Java_notes_cp
36 | P a g e
Java_notes_cp
37 | P a g e
Java_notes_cp
38 | P a g e
Java_notes_cp
39 | P a g e
Java_notes_cp
40 | P a g e
Java_notes_cp
41 | P a g e
Java_notes_cp
42 | P a g e
Java_notes_cp
43 | P a g e
Java_notes_cp
44 | P a g e
Java_notes_cp
45 | P a g e
Java_notes_cp
46 | P a g e