SlideShare a Scribd company logo
1 of 11
What is Java?
Java is one of widely used and popular programming language. The flexibility of writing a java
code on one machine and running it on several other machine makes it a popular choice in
programming world.
Java is the #1 programming language and development platform with millions of developers
running more than 51 billion Java Virtual Machines worldwide.
Features of Java
Following are some of the useful and advanced features of java:
1. Simple
Java is a very simple programming language, it is easy to learn, read and write in Java. The
syntax of Java is clean and easy to understand.
2. Platform Independent
Java is a platform independent language. Compiler (javac) converts source code (.java file) to
the byte code(.class file). JVM executes the bytecode produced by compiler. This byte code
can run on any platform such as Windows, Linux, Mac OS etc. Which means a program that is
compiled on windows can run on Linux and vice-versa.
Each operating system has different JVM, however the output they produce after execution
of bytecode is same across all operating systems. That is why we call java as platform
independent language.
3. Secure
Java is more secure than C/C++ as does not allow developers to create pointers, thus it
becomes impossible to access a variable from outside if it’s not been initialized.
4. Object-Oriented Programming language
Java is Object-Oriented Programming language.
5. Robust
Robust means reliable. Java programming language is developed in a way that puts a lot of
emphasis on early checking for possible errors.
6. Distributed
Using java programming language we can create distributed applications.
7. Multithreading
Java supports multithreading. Multithreading is a Java feature that allows concurrent
execution of two or more parts of a program for maximum utilisation of CPU.
8. Portable
Java code that is written on one machine can run on another machine
9. Dynamic
Java is a dynamic programming language.
10. Performance
Java is significantly faster than other traditional interpreted programming languages.
Compiled java code which is known as byte code is like a machine code, that allows a faster
execution.
JDK (Java Development Kit)
The JDK comes with a complete Java Runtime Environment (JRE), that is different from the
regular JRE thats the reason it is usually called a private runtime so we can say that it includes
all the features that JRE has. It has all the java development tools such as compiler, JVM,
JRE, debugger etc. We need jdk in order to write and run java program.
JRE (Java Runtime Environment):JRE is the environment within which the java virtual machine
runs. JRE contains Java virtual Machine(JVM), class libraries, and other files excluding
development tools such as compiler and debugger.
JVM (Java Virtual Machine):
Java is a high level programming language. A program written in high level language cannot
be run on any machine directly. First, it needs to be translated into that particular machine
language. The javac compiler does this thing, it takes java program (.java file containing source
code) and translates it into machine code (referred as byte code or .class file).
Java Virtual Machine (JVM) is a virtual machine that resides in the real machine (your
computer) and the machine language for JVM is byte code. JVM executes the byte code
generated by compiler and produce output. JVM is the one that makes java platform
independent.
Compiler/Debugger: The javac compiler takes java program (.java file containing source code)
and translates it into machine code (referred as byte code or .class file).
Explain the following line:
public static void main (String [] args) {}
A java file can have any number of classes but it can have only one public class and the file
name should be same as public class name.
public: This makes the main method public that means that we can call the method from
outside the class.
static: We do not need to create object for static methods to run. They can run itself.
void: It does not return anything.
main: It is the method name. This is the entry point method from which the JVM can run your
program.
(String[] args): Used for command line arguments that are passed as strings. We will
cover that in a separate post.
OOP:
Object-oriented programming System(OOPs) is a programming paradigm based on the
concept of “objects” that contain data and methods. The primary purpose of object-oriented
programming is to increase the flexibility and maintainability of programs.
What is an Object
Object: is a bundle of data and its behaviour(often known as methods).
Objects have two characteristics: They have states and behaviors.
Examples of states and behaviors
Example 1:
Object: House
State: Address, Color, Area
Behavior: Open door, close door
Object: Car
State: Color, Brand, Weight, Model
Behavior: Break, Accelerate, Slow Down, Gear change.
What is a Class in OOPs Concepts
A class can be considered as a blueprint using which you can create as many objects as you
like. For example, here we have a class Website that has two data members (also known as
fields, instance variables and object states). This is just a blueprint, it does not represent any
website, however using this we can create Website objects (or instances) that represents the
websites.
Types of Java Comments
There are three types of comments in Java.
1. Single Line Comment
2. Multi Line Comment
3. Documentation Comment
1) Java Single Line Comment
The single-line comment is used to comment only one line of the code. Single line comments
starts with two forward slashes (//). Any text in front of // is not executed by Java.
//This is single line comment
2) Java Multi Line Comment
The multi-line comment is used to comment multiple lines of code. It can be used to explain a
complex code snippet . Multi-line comments are placed between /* and */.
/*
This
is
multi line
comment
*/
3) Java Documentation Comment
Documentation comments are usually used to write large programs for a project or software
application as it helps to create documentation API.
/**
*
*We can use various tags to depict the parameter
*or heading or author name
*We can also use HTML tags
*
*/
Chap 3
Primitive data types
In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and
double.
byte, short, int and long data types are used for storing whole numbers.
float and double are used for fractional numbers.
char is used for storing characters(letters).
boolean data type is used for variables that holds either true or false.
byte:
This can hold whole number between -128 and 127. Mostly used to save memory .Default
size of this data type: 1 byte.
Example:
byte num;
num = 113;
short:
This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767.
Default size of this data type: 2 byte
short num = 45678;
int: Used when short is not large enough to hold the number, it has a wider range: -
2,147,483,648 to 2,147,483,647
Default size: 4 byte
int num;
num = 150;
long:
Used when int is not large enough to hold the value, it has wider range than int data type,
ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
size: 8 bytes
Example:
long num = -12332252626L;
double: Sufficient for holding 15 decimal digits
size: 8 bytes
double num = -42937737.9d;
float: Sufficient for holding 6 to 7 decimal digits
size: 4 bytes
float num = 19.98f;
boolean: holds either true of false.
boolean b = false;
char: holds characters.
size: 2 bytes
char ch = 'Z';
Sum of two numbers
public class JavaExample {
public static void main(String[] args) {
int num1 = 5, num2 = 15,sum;
sum = num1+num2;
System.out.println("Sum of "+num1+" and "+num2+" is: "+sum);
}
}
Output:
Sum of 5 and 15 is: 20
Program to read two integer and print product/Multiply
of them
import java.util.Scanner;
public class multiplynum {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scan.nextInt();
System.out.print("Enter second number: ");
int num2 = scan.nextInt();
scan.close();
int product = num1*num2;
System.out.println("Output: "+product);
}
}
Output:
Enter first number: 15
Enter second number: 6
Output: 90
Program to read the number entered by user
import java.util.Scanner;
public class scaninput {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter any number: ");
int num = scan.nextInt();
scan.close();
System.out.println("The number entered by user: "+num);
}
}
Output:
Enter any number: 101
The number entered by user: 101
Java program to calculate area of Triangle
import java.util.Scanner;
class AreaTriangleDemo {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the width of the Triangle:");
double base = scanner.nextDouble();
System.out.println("Enter the height of the Triangle:");
double height = scanner.nextDouble();
//comment : Area = (width*height)/2
double area = (base* height)/2;
System.out.println("Area of Triangle is: " + area);
}
}
Output:
Enter the width of the Triangle: 2
Enter the height of the Triangle: 2
Area of Triangle is: 2.0
Variables in Java
A variable is a name which is associated with a value that can be changed. For example when
I write int i=10; here variable name is i which is associated with value 10, int is a data type
that represents that this variable can hold integer values.
To declare a variable we follow this syntax:
data_type variable_name = value;
There are three types of variables in Java.
1) Local variable (These variables are declared inside method of the class. Their scope is
limited to the method which means that You can’t change their values and access them
outside of the method.) 2) Static (or class) variable (If I create three objects of a class and
access this static variable, it would be common for all, the changes made to the variable using
one of the object would reflect when you access it through other objects.) 3) Instance
variable (Each instance(objects) of class has its own copy of instance variable.)
Variables naming convention in java
1) Variables naming cannot contain white spaces,
2) Variable name can begin with special characters such as $ and _
3) The variable name should begin with a lower case letter,
4) Variable names are case sensitive in Java.
Dynamic initialization of object refers to initializing the objects at run
time i.e. the initial value of an object is to be provided during run time.
Dynamic initialization can be achieved using constructors and passing
parameters values to the constructors.
Example Code of Dynamic initialization
#include <iostream>
using namespace std;
class simple_interest {
float principle , time, rate ,interest;
public:
simple_interest (float a, float b, float c) {
principle = a;
time =b;
rate = c;
}
void display ( ) {
interest =(principle* rate* time)/100;
cout<<"interest ="<<interest ;
}
};
int main() {
float p,r,t;
cout<<"principle amount, time and rate"<<endl;
cout<<"2000 7.5 2"<<endl;
simple_interest s1(2000,7.5,2);//dynamic initialization
s1.display();
return 1;
}
Output
Enter principle amount ,rate and time
2000 7.5 2
Interest =300

More Related Content

Similar to OOP-Chap2.docx

Java programing language unit 1 introduction
Java programing language unit 1 introductionJava programing language unit 1 introduction
Java programing language unit 1 introductionchnrketan
 
Java for Mainframers
Java for MainframersJava for Mainframers
Java for MainframersRich Helton
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionGanesh Samarthyam
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECSreedhar Chowdam
 
Sybsc cs sem 3 core java
Sybsc cs sem 3 core javaSybsc cs sem 3 core java
Sybsc cs sem 3 core javaWE-IT TUTORIALS
 
Java-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oopsJava-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oopsbuvanabala
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction AKR Education
 
Introduction to Java Programming.pdf
Introduction to Java Programming.pdfIntroduction to Java Programming.pdf
Introduction to Java Programming.pdfAdiseshaK
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software DevelopmentZeeshan MIrza
 

Similar to OOP-Chap2.docx (20)

Srgoc java
Srgoc javaSrgoc java
Srgoc java
 
Java programing language unit 1 introduction
Java programing language unit 1 introductionJava programing language unit 1 introduction
Java programing language unit 1 introduction
 
OOPS JAVA.pdf
OOPS JAVA.pdfOOPS JAVA.pdf
OOPS JAVA.pdf
 
basic_java.ppt
basic_java.pptbasic_java.ppt
basic_java.ppt
 
What is Java? Presentation On Introduction To Core Java By PSK Technologies
What is Java? Presentation On Introduction To Core Java By PSK TechnologiesWhat is Java? Presentation On Introduction To Core Java By PSK Technologies
What is Java? Presentation On Introduction To Core Java By PSK Technologies
 
Java for Mainframers
Java for MainframersJava for Mainframers
Java for Mainframers
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Java Notes
Java Notes Java Notes
Java Notes
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
 
Java 3 rd sem. 2012 aug.ASSIGNMENT
Java 3 rd sem. 2012 aug.ASSIGNMENTJava 3 rd sem. 2012 aug.ASSIGNMENT
Java 3 rd sem. 2012 aug.ASSIGNMENT
 
JAVA for Every one
JAVA for Every oneJAVA for Every one
JAVA for Every one
 
Sybsc cs sem 3 core java
Sybsc cs sem 3 core javaSybsc cs sem 3 core java
Sybsc cs sem 3 core java
 
Java-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oopsJava-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oops
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction
 
Basic java part_ii
Basic java part_iiBasic java part_ii
Basic java part_ii
 
Introduction to Java Programming.pdf
Introduction to Java Programming.pdfIntroduction to Java Programming.pdf
Introduction to Java Programming.pdf
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
 
CORE JAVA
CORE JAVACORE JAVA
CORE JAVA
 

Recently uploaded

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 

Recently uploaded (20)

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 

OOP-Chap2.docx

  • 1. What is Java? Java is one of widely used and popular programming language. The flexibility of writing a java code on one machine and running it on several other machine makes it a popular choice in programming world. Java is the #1 programming language and development platform with millions of developers running more than 51 billion Java Virtual Machines worldwide. Features of Java Following are some of the useful and advanced features of java: 1. Simple Java is a very simple programming language, it is easy to learn, read and write in Java. The syntax of Java is clean and easy to understand. 2. Platform Independent Java is a platform independent language. Compiler (javac) converts source code (.java file) to the byte code(.class file). JVM executes the bytecode produced by compiler. This byte code can run on any platform such as Windows, Linux, Mac OS etc. Which means a program that is compiled on windows can run on Linux and vice-versa. Each operating system has different JVM, however the output they produce after execution of bytecode is same across all operating systems. That is why we call java as platform independent language. 3. Secure Java is more secure than C/C++ as does not allow developers to create pointers, thus it becomes impossible to access a variable from outside if it’s not been initialized. 4. Object-Oriented Programming language Java is Object-Oriented Programming language. 5. Robust Robust means reliable. Java programming language is developed in a way that puts a lot of emphasis on early checking for possible errors. 6. Distributed Using java programming language we can create distributed applications. 7. Multithreading Java supports multithreading. Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilisation of CPU.
  • 2. 8. Portable Java code that is written on one machine can run on another machine 9. Dynamic Java is a dynamic programming language. 10. Performance Java is significantly faster than other traditional interpreted programming languages. Compiled java code which is known as byte code is like a machine code, that allows a faster execution. JDK (Java Development Kit) The JDK comes with a complete Java Runtime Environment (JRE), that is different from the regular JRE thats the reason it is usually called a private runtime so we can say that it includes all the features that JRE has. It has all the java development tools such as compiler, JVM, JRE, debugger etc. We need jdk in order to write and run java program. JRE (Java Runtime Environment):JRE is the environment within which the java virtual machine runs. JRE contains Java virtual Machine(JVM), class libraries, and other files excluding development tools such as compiler and debugger. JVM (Java Virtual Machine): Java is a high level programming language. A program written in high level language cannot be run on any machine directly. First, it needs to be translated into that particular machine
  • 3. language. The javac compiler does this thing, it takes java program (.java file containing source code) and translates it into machine code (referred as byte code or .class file). Java Virtual Machine (JVM) is a virtual machine that resides in the real machine (your computer) and the machine language for JVM is byte code. JVM executes the byte code generated by compiler and produce output. JVM is the one that makes java platform independent. Compiler/Debugger: The javac compiler takes java program (.java file containing source code) and translates it into machine code (referred as byte code or .class file). Explain the following line: public static void main (String [] args) {} A java file can have any number of classes but it can have only one public class and the file name should be same as public class name. public: This makes the main method public that means that we can call the method from outside the class. static: We do not need to create object for static methods to run. They can run itself. void: It does not return anything. main: It is the method name. This is the entry point method from which the JVM can run your program. (String[] args): Used for command line arguments that are passed as strings. We will cover that in a separate post. OOP: Object-oriented programming System(OOPs) is a programming paradigm based on the concept of “objects” that contain data and methods. The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs. What is an Object Object: is a bundle of data and its behaviour(often known as methods). Objects have two characteristics: They have states and behaviors. Examples of states and behaviors
  • 4. Example 1: Object: House State: Address, Color, Area Behavior: Open door, close door Object: Car State: Color, Brand, Weight, Model Behavior: Break, Accelerate, Slow Down, Gear change. What is a Class in OOPs Concepts A class can be considered as a blueprint using which you can create as many objects as you like. For example, here we have a class Website that has two data members (also known as fields, instance variables and object states). This is just a blueprint, it does not represent any website, however using this we can create Website objects (or instances) that represents the websites. Types of Java Comments There are three types of comments in Java. 1. Single Line Comment 2. Multi Line Comment 3. Documentation Comment 1) Java Single Line Comment The single-line comment is used to comment only one line of the code. Single line comments starts with two forward slashes (//). Any text in front of // is not executed by Java. //This is single line comment 2) Java Multi Line Comment The multi-line comment is used to comment multiple lines of code. It can be used to explain a complex code snippet . Multi-line comments are placed between /* and */. /* This is multi line comment */ 3) Java Documentation Comment Documentation comments are usually used to write large programs for a project or software application as it helps to create documentation API.
  • 5. /** * *We can use various tags to depict the parameter *or heading or author name *We can also use HTML tags * */
  • 6. Chap 3 Primitive data types In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and double. byte, short, int and long data types are used for storing whole numbers. float and double are used for fractional numbers. char is used for storing characters(letters). boolean data type is used for variables that holds either true or false. byte: This can hold whole number between -128 and 127. Mostly used to save memory .Default size of this data type: 1 byte. Example: byte num; num = 113; short: This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767. Default size of this data type: 2 byte short num = 45678; int: Used when short is not large enough to hold the number, it has a wider range: - 2,147,483,648 to 2,147,483,647 Default size: 4 byte int num; num = 150; long: Used when int is not large enough to hold the value, it has wider range than int data type, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. size: 8 bytes Example: long num = -12332252626L;
  • 7. double: Sufficient for holding 15 decimal digits size: 8 bytes double num = -42937737.9d; float: Sufficient for holding 6 to 7 decimal digits size: 4 bytes float num = 19.98f; boolean: holds either true of false. boolean b = false; char: holds characters. size: 2 bytes char ch = 'Z'; Sum of two numbers public class JavaExample { public static void main(String[] args) { int num1 = 5, num2 = 15,sum; sum = num1+num2; System.out.println("Sum of "+num1+" and "+num2+" is: "+sum); } } Output: Sum of 5 and 15 is: 20 Program to read two integer and print product/Multiply of them import java.util.Scanner; public class multiplynum { public static void main(String[] args) {
  • 8. Scanner scan = new Scanner(System.in); System.out.print("Enter first number: "); int num1 = scan.nextInt(); System.out.print("Enter second number: "); int num2 = scan.nextInt(); scan.close(); int product = num1*num2; System.out.println("Output: "+product); } } Output: Enter first number: 15 Enter second number: 6 Output: 90 Program to read the number entered by user import java.util.Scanner; public class scaninput { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter any number: "); int num = scan.nextInt(); scan.close(); System.out.println("The number entered by user: "+num); } } Output: Enter any number: 101 The number entered by user: 101
  • 9. Java program to calculate area of Triangle import java.util.Scanner; class AreaTriangleDemo { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the width of the Triangle:"); double base = scanner.nextDouble(); System.out.println("Enter the height of the Triangle:"); double height = scanner.nextDouble(); //comment : Area = (width*height)/2 double area = (base* height)/2; System.out.println("Area of Triangle is: " + area); } } Output: Enter the width of the Triangle: 2 Enter the height of the Triangle: 2 Area of Triangle is: 2.0
  • 10. Variables in Java A variable is a name which is associated with a value that can be changed. For example when I write int i=10; here variable name is i which is associated with value 10, int is a data type that represents that this variable can hold integer values. To declare a variable we follow this syntax: data_type variable_name = value; There are three types of variables in Java. 1) Local variable (These variables are declared inside method of the class. Their scope is limited to the method which means that You can’t change their values and access them outside of the method.) 2) Static (or class) variable (If I create three objects of a class and access this static variable, it would be common for all, the changes made to the variable using one of the object would reflect when you access it through other objects.) 3) Instance variable (Each instance(objects) of class has its own copy of instance variable.) Variables naming convention in java 1) Variables naming cannot contain white spaces, 2) Variable name can begin with special characters such as $ and _ 3) The variable name should begin with a lower case letter, 4) Variable names are case sensitive in Java. Dynamic initialization of object refers to initializing the objects at run time i.e. the initial value of an object is to be provided during run time. Dynamic initialization can be achieved using constructors and passing parameters values to the constructors. Example Code of Dynamic initialization #include <iostream> using namespace std; class simple_interest { float principle , time, rate ,interest; public: simple_interest (float a, float b, float c) { principle = a; time =b; rate = c; } void display ( ) { interest =(principle* rate* time)/100; cout<<"interest ="<<interest ; } }; int main() {
  • 11. float p,r,t; cout<<"principle amount, time and rate"<<endl; cout<<"2000 7.5 2"<<endl; simple_interest s1(2000,7.5,2);//dynamic initialization s1.display(); return 1; } Output Enter principle amount ,rate and time 2000 7.5 2 Interest =300