Java For Beginners
Java For Beginners
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Table of Content
● Java
● History
● Our First Program
● Features
● JDK, JRE and JVM
● Variables
● Operators
● Data types
● Conditional Statements
● Loops
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Java
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
What is Java?
● Java is a high level object oriented programming language that was originally
developed by Sun Microsystems
● It follows the principle of WORA(Write Once Run Anywhere) - you can run a java
program as many times as you want on a java supported platform after it is
compiled
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
History
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
History of Java
● 1991: Java was first designed for digital cable television industry
● It was initially known as Green Team
● Later it got renamed to Oak based on the Oak tree outside James Gosling's Office
● 1995: Oak got changed to Java based on the island in Indonesia from which the team
used to have coffee
● Java was included in the time magazine's "Top Ten Best Product of 1995"
● 1996: JDK 1.0 got released
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Our First Program
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Hello World Program
public class MyFirstCode {
public static void main (String[] args) {
System.out.println("Hello World!");
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Explanation of Code
● Everything bold is keyword which are words with some fixed meaning
● class is used to declare class in Java
● public is a access modifier which allows visibility to all
● static doesn't need object to get called and main gets called by JVM
● void means that function will not return anything
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Features
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Features of Java
● Simple: It is very easy to learn as syntax is similar to previously existing language like C and
C++
● Object oriented: Java is based on objects which imitates real world problems. It supports 4
pillars of object oriented language i,e., Inheritance, Polymorphism, Abstraction and
Encapsulation
● Platform Independent: It has its own virtual machine and converts the code into bytecode
which can run in a java supported platform
● Secured: Java is way more secured because it has its own box responsible for handling its
execution. It does not support pointers and multiple inheritance which can cause ambiguity
or lead programs to crash
● Efficient: It is much more efficient because it uses a JIT(Just In Time) compiler that basically
utilizes the best of 2 worlds i.e., compiler and interpreter
● Multi-threaded: Java supports thread, that basically allows multiple tasks to execute
concurrently without occupying much memory
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
JDK, JRE & JVM
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
JDK, JRE and JVM
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
JDK, JRE and JVM - Explanation
● JVM: Java Virtual Machine
○ This is a virtual(abstract) machine that holds all java executions
○ It doesn't exist in reality
○ It also manages heap memory, garbage collector, etc
● JRE: Java Runtime Environment
○ It is responsible for running pre compiled java programs
○ It contains all files and libraries to facilitate running of java code
● JDK: Java Development Toolkit
○ This is a s/w that helps in development of Java applications
○ It is basically: JRE+Development tools
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Variables
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
What is a Variable?
● Variable is a name of a memory location where the data is stored
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Variable - Example
public class VariableDemo {
public static void main (String[] args) {
//var declaration and initialization
int roll_num=23; Output:
String first_name="adam"; Hi I am adam rollno 23 and I scored 89
//just declaration
int marks;
//later initialization
marks=89;
System.out.println("Hi I am "+first_name+"
roll_no "+rollno+" and I scored "+marks);
}
} Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Naming Convention
● Variable names should begin with a letter, $ or an underscore(_).
● First character can be then followed by any combination of letters or digits.
● A variable name cannot be the same as any keyword as they are reserved for special
purposes.
● Variable names case sensitive.
○ Eg: valid- apple, _name, $address1
○ Eg: invalid- 123apple, *roll, @email
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Types of Variables
● Instance variable:
○ Variable declared outside of any methods but within the class
○ Can be accessed by the all the methods of class irrespective of its access specifier
○ Every object of class gets its own original copy of variables.
● Static variable:
○ Also known as class variables
○ Variables declared as static, maintains single copy in memory and is shared by all
the objects of the class.
○ They can be directly called using class name as classname.var_name
○ Non-static methods can't access or manipulate static variables.
● Local variable:
○ Variables defined inside methods fall into this category.
○ They are accessible in the scope where it is declared.
○ They remain hidden outside of their methods.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Example
class VariableDemo {
void method(){
int k=300;//local variable
}
}
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Operators
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Unary Operator
● It needs one operand
● We have 2 major operators here i.e, increment and decrement and each one have 2
variations of postfix and prefix
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Arithmetic Operator
● It comprises all binary arithmetic operations:
○ Addition
○ Subtraction
○ Multiplication
○ Division
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Shift Operator
● Two variations - left and right shift
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Relational Operator
● It comprises operators for comparisons
● There are operators to check inequality i.e., < ,>, <=, >=
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Bitwise Operator
● It computes by going bit by bit
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Logical Operator
● Used for conditions comparison that basically checks for the validity of them
● If the first condition is enough to give the final verdict, it will not evaluate the second
one
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Assignment and Ternary Operator
● Assignment operator(=)
It is used to assign right hand side value to left hand side variable
● Ternary operator(?:)
An alternative of if else. Condition is placed before ? and if evaluates to true
then LHS of colon gets executed else RHS of colon will
● Example:
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Data Types
● To process them we need different types of containers or data types as storage.
● In Java, there are 8 primitive data types
○ byte - size is 1 byte and by default it holds 0
○ int - size is 4 byte and by default it holds 0
○ short - size is 2 byte and by default it holds 0
○ long - size is 8 byte and by default it holds 0L
○ char - size is 2 byte and by default it holds '\u0000'
○ boolean - size is 1 bit and by default it holds false
○ float - size is 4 byte and by default it holds 0.0f
○ double - size is 8 byte and by default it holds 0.0d
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Conditional Statements
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
If-Else
public class IfElseDemo { Output:
public static void main (String[] args) { num are equal
int val1=5,val2=6;
if(val1 + val2 ==11)
System.out.println("num are equal");
else
System.out.println("num are not equal");
}
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Loops
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
For Loop
● It is best to use when we know the specified number of times the code should
execute
● Syntax:
for( initialization; termination; updation){
}
○ initialization - is used to initialize the variable, which is being used for
iteration.
○ termination - comprises conditions which determine till when iteration will
continue.
○ updation - how our variable will get updated.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
While Loop
● It is optimal when we know the specified expression whose value dictates the times
the code should execute.
● Syntax:
while( expression ){ }
expression- is used to dictate the condition who is responsible for loop continuation.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Do-While Loop
● It is best to use when we know that at least code must execute once irrespective of
the condition.
● Syntax:
do{
}while( expression );
expression- is used to dictate the condition who is responsible for loop continuation.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Thank You
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited