JAVA BASICS
INTERNAL
25 Apr 2023
What is Java?
• Java is a programming language, created in 1995. And It is owned by
Oracle. But it was developed by James Gosling at Sun Microsystems in the
early 1990s.
• It is open-source and free.
• It is secure, fast and powerful.
• Java is an object oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs.
It is used for:
⦁ Mobile applications (specially Android apps)
⦁ Desktop applications
⦁ Web applications
⦁ Web servers and application servers
⦁ Games
⦁ Database connection
Document Name
CONFIDENTIAL
JDK, JVM and JRE
JDK
A software development kit that includes tools for building Java applications, such as
compilers and debuggers. The JDK also includes the Java Class Library, which is a
collection of predefined classes.
JRE
A software package that includes the JVM, class libraries, and other components needed to
run Java applications. The JRE is similar to an operating system, coordinating different
hardware components to run Java applications.
JVM
A platform-independent virtual machine that executes Java programs. The JVM is responsible
for loading, linking, initializing, and compiling programs. It also provides basic Java
functions, such as memory management, security, and garbage collection.
Document Name
CONFIDENTIAL
Differences between JDK, JVM and JRE
JDK JRE JVM
Used to develop Used to run Java Responsible for
Java applications applications running Java code
Platform-dependent Platform-dependent Platform-
Independent
It includes It includes libraries to It runs the java byte
development tools run Java application + code and make java
like (compiler) + JRE JVM application to work
on any platform.
Writing and Running a Java Convert bytecode
compiling Java code application on a system into native machine
code.
Document Name
CONFIDENTIAL
Input And Output
Reading values:
Scanner class for reading values
Scanner sc = new Scanner([Link]);
Displaying values:
[Link](string and numeric)
[Link](2);
[Link](3*5);
[Link]("Hello world!");
[Link]("Rate of Apple is :" + 50 +" per kg.");
Comments in java:
Single line comment: // This is a comment
Multi Line comment:
/*this
is
multi
line
comment. */
Document Name
CONFIDENTIAL
Simple program in Java
import [Link].*;
public class Test {
public static void main(String[] args){
[Link] ("Hello world!");
}
}
Document Name
CONFIDENTIAL
Data Types in Java
• Primitive Data Type: Such as boolean, char, int, short (16
bits), byte(8 bits), long(64 bits), float(upto 7 decimal digits), and
double (upto 16 decimal digits).
• Non-Primitive Data Type or Object Data type: Such as
String, Array, etc.
Document Name
CONFIDENTIAL
Logical Operation in Java
1. AND Operator ( && ) 3. NOT Operator ( ! )
Syntax : if( a && b ) Syntax: !(a<b)
[if true execute else don't] [returns false if a is smaller than b]
2 conditions a and b Returns the opposite of actual
If a is true and b is true return true. value
!(4<5) false
If a is true and b is false: return false or vice
versa
If both is false return false
2. OR Operator ( || )
Syntax: if( a || b)
[if one of them is true to execute else don't]
If a is true and b is false: return true or vice
versa
If both are true return true
If both are false return false
Document Name
CONFIDENTIAL
Conditional Operations
1. if- else statement:
Syntax:
if(condition is true) {do this}
else {do this}
2. if-else-if statement:
Syntax:
if(condition is true) {do this}
else if (second condition is true){do this}
else {do this}
Document Name
CONFIDENTIAL
String Buffer
String Buffer: is a class that allows us to create and modify a flexible,
expandable sequence of characters.
It is thread-safe. This means that it can be safely used when multiple
threads are accessing or modifying the string concurrently. But due to
synchronization, it is slower than StringBuilder.
Because synchronization does not allow String Buffer to share one String to
more than one thread(process).
StringBuffer str = "Happy New year";
Document Name
CONFIDENTIAL
String builder
StringBuilder: is similar to StringBuffer, but it is not synchronized.
This means it is not thread-safe.
But it is much faster than StringBuffer and is suitable for use in scenarios where
thread safety is not required.
StringBuilder str = "Happy New year";
Document Name
CONFIDENTIAL
Iterations using loop
1. for loop:
for(int i=0; i< variable; i++){}
2. while loop:
while(condition is true){}
3. do while loop:
do{ this }
while(condition is true){}
4. for each loop
for(data_type variable_name: data_structure)
Document Name
CONFIDENTIAL
Thank You