Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

java programming for beginners, Lecture notes of Java Programming

Hi Friends, this course is specially designed for students who do not have prior coding experience. Course covers all the Core Java Concepts from basic to advanced levels along with practical examples and coding exercises. This course covers Core Java topics in detail from basic to advanced levels. I believe in example-oriented teaching. So, you won’t find any PPTs during the sessions. But, you will find dozens of real time scenarios used to elaborate Java basic and advanced concepts.

Typology: Lecture notes

2022/2023

Available from 06/30/2023

anubhav-10
anubhav-10 🇮🇳

1 document

1 / 83

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Overview
Java is an open-source, class-based, high-level, object-oriented programming language. Java
is platform independent as the java programs are compiled into byte code that are platform
independent.
History:
Java programming language was created by James Gosling in 1995. The original idea was to
design a language for the television industry. Gosling worked along with his team also called
the Green Team and the project they worked on was called Greentalk. This project was later
named as OAK. The name OAK has its roots to the oak tree that stood outside Gosling’s office.
This name had to be dropped later as it was already a trademark by Oak Technologies.
So how was the name Java suggested?
Since the language could no longer be named OAK, Gosling and his team had to come up with
new name. The team considered various names like DNA, RUBY, JAVA, jolt, dynamic,
revolutionary, SILK.
But the name had to unique and quite easy to say. The name JAVA occurred in gosling’s mind
while having a cup of coffee at his office.
Types of Java applications:
A. Web Application:
Web applications are the applications that run on web browser using servlet, JSP, struts
technologies. These technologies create java web applications and deploy them on server.
B. Mobile Application:
These are mobile applications created using java.
C. Standalone Application:
Standalone applications are executed by themselves without the need of other programs and
files. Example of such an application is antivirus.
D. Enterprise Application:
Some applications are designed for corporate organizations with the intent to control major
process in real time. Such applications are called enterprise applications.
Features
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53

Partial preview of the text

Download java programming for beginners and more Lecture notes Java Programming in PDF only on Docsity!

Java Overview

Java is an open-source, class-based, high-level, object-oriented programming language. Java is platform independent as the java programs are compiled into byte code that are platform independent.

History:

Java programming language was created by James Gosling in 1995. The original idea was to design a language for the television industry. Gosling worked along with his team also called the Green Team and the project they worked on was called Greentalk. This project was later named as OAK. The name OAK has its roots to the oak tree that stood outside Gosling’s office. This name had to be dropped later as it was already a trademark by Oak Technologies. So how was the name Java suggested? Since the language could no longer be named OAK, Gosling and his team had to come up with new name. The team considered various names like DNA, RUBY, JAVA, jolt, dynamic, revolutionary, SILK. But the name had to unique and quite easy to say. The name JAVA occurred in gosling’s mind while having a cup of coffee at his office.

Types of Java applications:

A. Web Application: Web applications are the applications that run on web browser using servlet, JSP, struts technologies. These technologies create java web applications and deploy them on server. B. Mobile Application: These are mobile applications created using java. C. Standalone Application: Standalone applications are executed by themselves without the need of other programs and files. Example of such an application is antivirus. D. Enterprise Application: Some applications are designed for corporate organizations with the intent to control major process in real time. Such applications are called enterprise applications.

Features

● Object Oriented: In object oriented programming everything is an object rather that function and logic. ● Simple: Java is simple to understand, easy to learn and implement. ● Secured: It is possible to design secured software systems using Java. ● Platform Independent: Java is write once and run anywhere language, meaning once the code is written, it can be executed on any software and hardware systems. ● Portable: Java is not necessarily fixated to a single hardware machine. Once created, java code can be used on any platform. ● Architecture Neutral: Java is architecture neutral meaning the size of primitive type is fixed and does not vary depending upon the type of architecture. ● Robust: Java emphasizes a lot on error handling, type checking, memory management, etc. This makes it a robust language. ● Interpreted: Java converts high-level program statement into Assembly Level Language, thus making it interpreted.

The Java Runtime Environment (JRE) provides java libraries, the JVM and other files and documents that are needed to run java applications.

C. Java Development Kit (JDK):

The Java Development Kit (JDK) is a superset of JRE and is used to create java applications. There are three JDK provided by Oracle; Java Enterprise Edition (Java EE), Java Standard Edition (Java SE), and Java Mobile Edition (Java ME).

Basic Java Syntax

It is particularly important to follow the appropriate syntax while writing java code, as we might get errors for the slightest mistake in our code. The class name should be the same as that of the name of the java file. And each line of code must be written inside a class. Example: program where file name and class name is different. package syntax1; public class DEtails { public static void main(String[] args) { System.out.println("Java program with diff file name and class name"); } } Output: The public type Details must be defined in its own file. Example: program where file name and class name is same. package syntax1; public class Details { public static void main(String[] args) { System.out.println("Java program with same file name and class name"); } } Output: Java program with same file name and class name

As we can see from both the examples that even a slightest change in the name of file and class name still gives us an error. Each block of code is indented within the curly brackets{}. Example: see how each block is indented inside the parent block package syntax1; public class Details { public static void main(String[] args) { System.out.println("Java program with same file name and class name"); } } Each java file must contain a main method that is needed to execute a java file. Example: java code without a main method gives an error package syntax1; public class Details { System.out.println("Java program with same file name and class name"); } Output: Build failed

Java Comments

Comments in any programming language are ignored by the compiler or the interpreter. A comment is a part of the coding file that the programmer does not want to execute, rather the programmer uses it to either explain a block of code or to avoid the execution of a specific part of code while testing. There are two types of coments: ● Single-line comment ● Multi-line comment

A. Single Line Comments:

To write a single-line comment just add a ‘//’ at the start of the line. Example:

● bool: Boolean data type consists of true and false values. ● char: char datatype is used to store characters. ● byte: The main purpose of byte is used to save memory and consists values in the range -128 to 127. ● short: consists values in the range -32768 to 32767. ● int: consists values in the range -2,147,483,648 to 2,147,483,647. ● long: consists values in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775, ● float: can be used to deal with decimal numbers. Always recommended to use float rather than double because float saves memory. ● double: can be used to deal with decimal numbers. Data Type Size Range bool 1 bit true, false char 2 byte a….z, A….Z byte 1 byte -2 7 to 27 - -128 to 128 short 2 byte -2 15 to 215 - -32768 to 32767

int 4 byte -2 31 to

-2,147,483,648 to

long 8 byte -2 63 to

808 to

float 4 byte 6.022f

double 8 byte 3.

Java Variables

Variables are containers that store information that can be manipulated and referenced later by the programmer within the code. Java variables have a data type associated with them which can tell us about the size and layout of the variable’s memory. Syntax: datatype variable = value There are three types of variables in java: ● Local variable ● Instance variable ● Class/Static variable

A. Local Variables:

A variable that is declared inside the body of the method or constructor is called a local variable. It is called so because the extent of a local variable lies only within the method or constructor within which it is created and other methods in the class cannot access it. Inside the method body, local variable is declared using the static keyword. Example: public class variableType { public void localVariable() { String name = "Ben"; int marks = 95; System.out.println(name + " Scored " + marks + "%."); } public static void main(String[] args) { variableType vt = new variableType();

public void instanceVariable() { System.out.println(name + " Scored " + marks + "%."); } public static void main(String[] args) { variableType vt = new variableType(); vt.instanceVariable(); } } Output: Ben Scored 95%.

C. Class/Static Variables:

An static variable is declared inside the class but outside a method or a constructor. It is similar to a instance variable except that it is declared using the keyword static. These variables are accessible by all methods or constructors that are inside the class. Example: public class variableType { public static String name; public static int marks; public static void main(String[] args) { name = "Ben"; marks = 95; System.out.println(name + " Scored " + marks + "%."); } }

Java Operators

Java has different types of operators for different

operations. They are as follows:

Arithmetic operators:

Arithmetic operators are used to perform arithmetic/mathematical operations. Name Operator Example Addition + a+b Subtraction - a-b Multiplication * a*b Division / a/b Modulus % a%b Increment ++ a++ or b++ Decrement -- a-- or b--

Assignment operators:

These operators are used to assign values to variables. Name Operator Evaluated As Assignment = a=b Addition assignment += a+=b or a=a+b Subtraction assignment -= a-=b or a=a-b Multiplication assignment = a=b or a=a*b Division assignment /= a//=b or a=a//b Modulus assignment %= a%=b or a=a%b Bitwise AND assignment &= a&=b or a=a&b Bitwise inclusive OR assignment |= a|=b or a=a|b Bitwise exclusive OR assignment ^= a^=b or a=a^b Right Shift assignment >>= a>>=b or a=a>>b

Name Operator Example

AND && a && b

OR || a || b

NOT!! (a=2 or b=3)

Other operators:

A. instanceof operator: This operator checks if an object is an instance of class. Example: class Main { public static void main(String[] args) { Integer number = 5; boolean res; if (res = number instanceof Integer){ System.out.println("number is an object of Integer. Hence: " + res); } else { System.out.println("number is not an object of Integer.Hence: " + res); } } } Output: number is an object of Integer. Hence: true B. Conditional operator: It is used in a single line if-else statement. Example: class Main { public static void main(String[] args) { Integer number = 3; String res;

res = (number > 5)? "number greater than five" : "number lesser than five"; System.out.println(res); } } Output: number lesser than five

User Input/Output

Taking Input:

To use the Scanner class, we need to import the java.util.Scanner package. import java.util.Scanner; Now the Scanner class has been imported. We create an object of the Scanner class to use the methods within it. Scanner sc = new Scanner(System.in) The System.in is passed as a parameter and is used to take input. Note: Your object can be named anything, there is no specific convention to follow. But we normally name our object sc for easy use and implementation.

There are four ways to create an object of the scanner class. Let us see

how we can create Scanner objects:

A. Reading keyboard input: Scanner sc = new Scanner(System.in) B. Reading String input: Scanner sc = new Scanner(String str)

nextFloat() Accepts float value nextDouble() Accepts double value nextLong() Accepts long value nextShort() Accepts short value nextBoolean() Accepts Boolean value nextByte() Accepts Byte value Example: import java.util.Scanner; public class ScannerExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter Name, RollNo, Marks, Grade"); String name = sc.nextLine(); //used to read line int RollNo = sc.nextInt(); //used to read int double Marks = sc.nextDouble(); //used to read double char Grade = sc.next().charAt(0); //used to read till space System.out.println("Name: "+name); System.out.println("Gender: "+RollNo); System.out.println("Marks: "+Marks); System.out.println("Grade: "+Grade);

sc.close(); } } Output: Enter Name, RollNo, Marks, Grade Mohit 19

A Name: Mohit Gender: 19 Marks: 87. Grade: A Entering the wrong type of input, or just messing up with the order of inputs in the above example will give an error. For same example when we change the order of inputs it gives the following output: Enter Name, RollNo, Marks, Grade Rajesh Joshi Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594)

In a simple if statement, a block of code inside the if statement is only executed if the condition evaluates to true. Syntax: if (condition) { //block of code } e.g. public class JavaIf {

public static void main(String[] args) { String name = "Mohan"; int Roll = 25; if (name == "Mohan" && Roll == 25) { System.out.println("Details of Mohan."); } } } Output: Details of Mohan. Example: public class JavaIf { public static void main(String[] args) { String name = "Mohan"; int Roll = 25; if (name == "Mohan" && Roll == 26) { System.out.println("Details of Mohan."); } } } Output Above code won’t give an output.

if…..else Statement

In an if…..else statement we have two blocks of code, wherein the former block of code (code inside if statement) is executed if condition evaluates to true and the later block of code (code inside else statement) is executed if the condition is false. Syntax: if (condition) { //block of code } else {