Professional Documents
Culture Documents
JAVA IS AN OBJECT-ORIENTED
CLASS-BASED, CONCURRENT, SECURED AND GENERAL-PURPOSE
COMPUTER-PROGRAMMING LANGUAGE. IT IS A WIDELY USED ROBUST
TECHNOLOGY.
INTRODUCTI
ON
TO CORE
WHAT IS JAVA?
There are mainly 4 types of applications that can be created using Java
programming:
1) Standalone Application
Standalone applications are also known as desktop applications or
window-based applications. These are traditional software that we need to
install on every machine. Examples of standalone application are Media
player, antivirus, etc. AWT and Swing are used in Java for creating
standalone applications.
2) Web Application
An application that runs on the server side and creates a dynamic page is
called a web application. Currently, Servlet
, JSP
, Struts
, Spring
, Hibernate
, JSF
, etc. technologies are used for creating web applications in Java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications,
etc. is called an enterprise application. It has advantages like high-level
security, load balancing, and clustering. In Java, EJB
is used for creating enterprise applications.
4) Mobile Application
An application which is created for mobile devices is called a mobile
application. Currently, Android and Java ME are used for creating mobile
applications.
JAVA PLATFORMS / EDITIONS
The history of Java is very interesting. Java was originally designed for
interactive television, but it was too advanced technology for the digital
cable television industry at the time. The history of Java starts with the
Green Team. Java team members (also known as Green Team), initiated
this project to develop a language for digital devices such as set-top
boxes, televisions, etc. However, it was best suited for internet
programming. Later, Java technology was incorporated by Netscape.
The principles for creating Java programming were "Simple, Robust,
Portable, Platform-independent, Secured, High Performance,
Multithreaded, Architecture Neutral, Object-Oriented, Interpreted, and
Dynamic". Java was developed by James Gosling, who is known as the
father of Java, in 1995. James Gosling and his team members started the
project in the early '90s.
Currently, Java is used in internet programming, mobile devices, games,
e-business solutions, etc. Following are given significant points that
describe the history of Java.
1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the
Java language project in June 1991. The small team of sun engineers
called Green Team.
2) Initially it was designed for small, embedded systems in electronic
appliances like set-top boxes.
3) Firstly, it was called "Greentalk" by James Gosling, and the file
extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green
project.
WHY JAVA PROGRAMMING NAMED
"JAVA"?
Why had they chose the name Java for Java language? The team gathered
to choose a new name. The suggested words were "dynamic",
"revolutionary", "Silk", "jolt", "DNA", etc. They wanted something that
reflected the essence of the technology: revolutionary, dynamic, lively,
cool, unique, and easy to spell, and fun to say.
According to James Gosling, "Java was one of the top choices along
with Silk". Since Java was so unique, most of the team members
preferred Java than other names.
8) Java is an island in Indonesia where the first coffee was produced
(called Java coffee). It is a kind of espresso bean. Java name was chosen
by James Gosling while having a cup of coffee nearby his office.
JAVA VERSION HISTORY
CONCEPT OF
that works on the principles of abstraction,
encapsulation, inheritance, and
polymorphism. it allows users to create
Object means a real-world entity such as a pen, chair, table, computer, watch, etc.
Object-Oriented Programming is a methodology or paradigm to design a program
using classes and objects. It simplifies software development and maintenance by
providing some concepts:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
Object
Any entity that has state and behavior is known as an object. For example, a chair,
pen, table, keyboard, bike, etc. It can be physical or logical.
Example: A dog is an object because it has states like color, name, breed, etc. as well as
behaviors like wagging the tail, barking, eating, etc.
Class
When one object acquires all the properties and behaviors of a parent object, it is
known as inheritance. It provides code reusability. It is used to achieve runtime
polymorphism.
Polymorphism
Hiding internal details and showing functionality is known as abstraction. For example
phone call, we don't know the internal processing.
Binding (or wrapping) code and data together into a single unit are known as
encapsulation. For example, a capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class
because all the data members are private here.
Advantages of OOPs
OOPs provides the ability to simulate real-world event much more effectively. We can
provide the solution of real word problem if we are using the Object-Oriented
Programming language.
Difference between an object-oriented programming
language and object-based programming language?
S HOLD DATA.
Java Variables
A variable is a container which holds the value while the Java program is executed. A variable
is assigned with a data type.
A variable is the name of a reserved area allocated in memory. In other words, it is a name of
the memory location. It is a combination of "vary + able" which means its value can be
changed.
There are two types of data types in Java: primitive and non-primitive.
1. Primitive data types: The primitive data types include; Boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
Types of Variables
There are three types of variables;
1) Local Variable
A variable declared inside the body of the method is called local variable. You can use this
variable only within that method. A local variable cannot be defined with "static" keyword.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an instance
variable. It is not declared as static. It is called an instance variable because its value is
instance-specific and is not shared among instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You can
create a single copy of the static variable and share it among all the instances of the class.
Memory allocation for static variables happens only once when the class is loaded in the
memory.
Understanding the types of variables in java
1. public class A
2. {
3. static int m=100;//static variable
4. void method()
5. {
6. int n=90;//local variable
7. }
8. public static void main(String args[])
9. {
10. int data=50;//instance variable
11. }
12. }//end of class
A VARIABLE IS A
OPERATOR LOCATION IN MEMORY
(STORAGE AREA) TO
S HOLD DATA.
Operators in Java
Operators are symbols that perform operations on variables and values.
For example: +, -, *, / etc.
• Unary Operator,
• Arithmetic Operator,
• Shift Operator,
• Relational Operator,
• Logical Operator,
• Bitwise Operator,
• Ternary Operator and
• Assignment Operator
Unary operators are used with only one operand.
[+] Unary plus: not necessary to use since numbers are positive without using it.
[-] Unary minus: inverts the sign of an expression.
[++] Increment operator: increments value by 1.
[--] Decrement operator: decrements value by 1.
[!] Logical complement operator: inverts the value of a Boolean.
Assignment operators are used in Java to assign values to variables.
They are; =, +=, -=, /=, %=
Shift Operators shifts the bits of its first operand right or left, a shift operator performs bit
manipulation on data.
They are; Signed Left Shift <<, Signed Left Shift >>, Unsigned Right Shift >>>
Relational operators are used to check the relationship between two operands.
They are; <, >, ==, !=, <=, >=
Logical operators are used to check whether an expression is True or False.
They are; &&, ||, !
Bitwise operators in Java are used to perform operations on individual bits.
They are; ~, <<, >>, >>>, &, ^
The ternary operator (conditional operator) is shorthand for the if-then-else statement.
That is: Variable = Expression ? True : False
Arithmetic operators are used to perform arithmetic operations on variables and data.
They are; +, -, /, *, %
WRITING A
PROGRAM IN JAVA
First Java Program | Hello World Example
To create a simple Java program, you need to create a class that contains the main
method. Let's understand the requirement first.
For executing any Java program, the following software or application must be
properly installed.
• Install the JDK if you don't have installed it, download the JDK and install it.
• Set path of the jdk/bin directory. http://www.javatpoint.com/how-to-set-path-in-java
• Create the Java program
• Compile and run the Java program
Creating Hello World Example
1. class Simple{
2. public static void main(String args[]){
3. System.out.println("Hello Java");
4. }
5. }
In computer programming, comments
COMMENTS IN are a portion of the program that are
completely ignored by java compilers.
//This is single line comment
/* Multi line
comment
*/
3) Java Documentation Comment
1. /**
2. *
3. *We can use various tags to depict the parameter
4. *or heading or author name
5. *We can also use HTML tags
6. *
7. */
A programming language uses
control statements to control the
flow of execution of a program
FLOW based on certain conditions. These
are used to cause the flow of
CONTROL execution to advance and branch
based on changes to the state of a
IN JAVA program.
Control Statements | Control Flow
1. if(condition){
2. //code to be executed
3. }
JAVA IF-ELSE STATEMENT
if(condition){
//code if condition is true
}else{
//code if condition is false
}
JAVA IF-ELSE-IF LADDER
STATEMENT
1. If (condition1){
2. //code to be executed if condition1 is true
3. } else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if (condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. Else {
11. //code to be executed if all the conditions are false
12. }
JAVA NESTED IF STATEMENT
1. if(condition){
2. //code to be executed
3. if(condition){
4. //code to be executed
5. }
6. }
JAVA SWITCH STATEMENT
Switch (expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
LOOP STATEMENTS IN
JAVA
JAVA FOR LOOP
for(initialization; condition; increment/decrement){
//code to be executed
}
JAVA WHILE LOOP
1. while (condition){
2. //code to be executed
3. Increment / decrement statement
4. }
JAVA DO-WHILE LOOP
do{
//code to be executed / loop body
//update statement
}while (condition);
JUMP STATEMENTS IN
JAVA
JAVA BREAK STATEMENT
1. public class BreakExample {
2. public static void main(String[] args) {
3. //using for loop
4. for (int i=1; i <=10; i++){
5. if (i==5){
6. //breaking the loop
7. break;
8. }
9. System.out.println(i);
10. }
11. }
12. }
JAVA CONTINUE STATEMENT
1. public class ContinueExample {
2. public static void main(String[] args) {
3. //for loop
4. for (int i=1; i<=10;I ++){
5. if (i==5){
6. //using continue statement
7. continue;//it will skip the rest statement
8. }
9. System.out.println(i);
10. }
11. }
12. }
AN ARRAY IS A COLLECTION
OF SIMILAR TYPES OF DATA.
ARRAYS IN JAVA
JAVA ARRAYS
int a[]={33,3,4,5};//declaration, instantiation and initialization
JAVA MULTIDIMENSIONAL
ARRAYS
A multidimensional array is an array of arrays. Each element of a multidimensional
array is an array itself. For example,
int [][] number = new int[3][4];
A diagram that explains the above 2-dimensional array is shown on the next slide.
METHODS IN AN ARRAY IS A COLLECTION
OF SIMILAR TYPES OF DATA.
JAVA
METHODS
We do not require to write code again and again. It also provides the easy
modification and readability of code, just by adding or removing a chunk of code. The
method is executed only when we call or invoke it.
TYPES OF METHODS
Predefined Method:
predefined methods are the method that is already defined in the Java class libraries is known as
predefined methods. It is also known as the standard library method or built-in method.
User-defined Method:
The method written by the user or programmer is known as a user-defined method. These methods
are modified according to the requirement.
METHOD DECLARATION
METHOD DECLARATION WELL-DEFINED
The method declaration provides information about method attributes, such as visibility, return-type,
name, and arguments.
Access Specifier: Access specifier or modifier is the access type of the method. It specifies the
visibility of the method. Java provides four types of access specifier:
Public: The method is accessible by all classes when we use public specifier in our application.
Private: When we use a private access specifier, the method is accessible only in the classes in
which it is defined.
Protected: When we use protected access specifier, the method is accessible within
the same package or subclasses in a different package.
Default: When we do not use any access specifier in the method declaration, Java
uses default access specifier by default. It is visible only from the same package only.
NAMING A METHOD
While defining a method, remember that the method name must be a verb and start with
a lowercase letter. If the method name has more than two words, the first name must be a
verb followed by adjective or noun. In the multi-word method name, the first letter of each
word must be in uppercase except the first word.
For example:
1. public class Demo
2. {
3. public static void main(String[] args)
4. {
5. // using the max() method of Math class
6. System.out.print("The maximum number is: " + Math.max(9,7));
7. }
8. }
USER-DEFINED METHOD
1. //user defined method
2. public static void findEvenOdd(int num)
3. {
4. //method body
5. if(num%2==0)
6. System.out.println(num+" is even");
7. else
8. System.out.println(num+" is odd");
9. }
SAMPLE OF JAVA PROGRAM
1. public class PrimeExample{
2. public static void main(String args[]){
3. int i, m=0,flag=0;
4. int n=3;//it is the number to be checked
5. m=n/2;
6. if(n==0||n==1){
7. System.out.println(n+" is not prime number");
8. }else{
9. for(i=2; I <= m; i++){
Continue..
1. If ( n % I == 0 ){
2. System.out.println(n+" is not prime number");
3. flag=1;
4. break;
5. }
6. }
7. if(flag==0) { System.out.println(n+" is prime number"); }
8. }//end of else
9. }
10. }
REFERENCES
https://www.javatpoint.com/java-tutorial
https://www.programiz.com/java-programming
https://www.geeksforgeeks.org/java-tutorial/?ref=lbp
https://www.guru99.com/java-tutorial.html
Learn More.. JAVA IS FUN!