2. Table of Contents
Java Introduction
Features of Java
Applications of Java
Data types
Conditional Control Structures
Loops in Java
Arrays in Java
3. Introduction
Java is a general-purpose, high-level programming language.It is
owned by Oracle, and more than 3 billion devices run Java.
Java is a programming language that James Gosling developed at
Sun Microsystems_Inc in the year 1995.
Java Virtual Machine interprets the bytecode and converts it to
platform specific machine code. Hence, Java is also called a
platform-independent programming language.
Java is a class-based object-oriented programming language that
implements the principle of write once code anywhere.
4. JDK- Java Development kit
Java development kit that includes everything including compiler, Java
Runtime Environment (JRE), java debuggers, java docs, etc. For the
program to execute in java, we need to install JDK on our computer in
order to create, compile and run the java program.
JRE- Java Runtime Environment
The Java Runtime Environment is a software layer that runs on top of a
computer’s operating system software and provides the class libraries and
other resources that a specific Java program needs to run.
JVM- Java Virtual Machine
JVM is an abstract machine. It is a specification that provides runtime
environment in which java bytecode can be executed.
JAVA
5.
6. Features of Java
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10.Multithreaded
11.Distributed
12.Dynamic
7. Applications of Java
o Mobile App Development
o Desktop GUI Applications
o Web-based Applications
o Gaming Applications
o Big Data Technologies
o Distributed Applications
o Cloud-based Applications
o IoT Applications
o Enterprise Applications
o Scientific Applications
8. Data Types
There are two types of data types in Java:
Primitive data types : The primitive data types include:
o boolean data type
o byte data type
o char data type
o short data type
o int data type
o long data type
o float data type
o double data type
Non-primitive data types : The non-primitive data types include :
o Classes
o Interfaces
o Arrays.
9.
10. Example
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99f; // Floating point number
char myLetter = 'D’; // Character
boolean myBool = true; // Boolean
double d2 = 1.234e2 // Double
String myText = "Hello"; // String
int arr[] = new int[100]; // Array
11. If-else Statement
The if Statement:
If(condition) {
//block of code to be executed if the condition is true
}
Example :
int x = 20;
int y = 18;
if (x > y)
{
System.out.println("x is greater than y");
}
12. The else Statement:
If(condition) {
//block of code to be executed if the condition is true
}
else {
// block of code to be executed if the condition is false
}
Example :
int x = 20;
if (x > 18){
System.out.println("x is greater than 18");
}
else {
System.out.println("x is less than 18");
}
13. The else if Statement:
If(condition 1) {
//block of code to be executed if the condition 1 is true
} else if (condition 2) {
// block of code to be executed if the condition 1 is false and condition 2 is true
} else {
// block of code to be executed if the condition 1 is false and condition 2 is false
}
Example : int x = 0;
if (x > 0){
System.out.println("x is greater than 0");
} else if (x < 0) {
System.out.println("x is less than 0");
} else {
System.out.println("x is 0");
}
14. Loops in Java
for Loop:
for(initializing statement;testing condition;increment/decrement)
{
//code to be iterated
}
Example :
for (i = 0; i <= 10; i++)
{
System.out.println("Value of i = " + i);
}
16. do while Loop:
do {
statement(s);
} while(test-expression) ;
Example :
char ch = 'A' ;
do
{
System.out.println( ch + " " );
ch++;
} while(ch <= 'Z');
17. Problem:
Given the number of terms of series N. Write a Java program to
print the Fibonacci series of N.
Code: Output:
18. Arrays in Java
• In Java, all arrays are dynamically allocated.
• Arrays are stored in contagious memory.
• Since arrays are objects in Java, we can find their length using the
object property length.
• A Java array variable can also be declared like other variables with
[] after the data type.
• Java array can also be used as a static field, a local variable, or a
method parameter.
• The size of an array must be specified by int or short value and not
long.
19. Instantiating array:
var-name = new type [size];
int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to array
int[] intArray = new int[20]; // combining both statements in one
Array Literal:
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
// Declaring array literal
20. Problem:
Given N numbers,write a java program to find the sum.
Constraints :
1 <= N <= 106
Input Format :
First line contains N numbers separated by comma
Output Format :
Sum
22. Strings:
Syntax of Strings(Creation):
char[]ch={‘h’,’e’,’l’,’l’,’o’};
String s=new String(ch);
(or)
String s="hello";
String method:
Length() –returns the length of the string
concat() – used to concat two or more strings
toCharArray()– converts string to a character array
charAt(int i) – returns character at index i in given string
compareTo() – compares two strings based on dictionary order