Java Basics - Part1-22nd - Sept
Java Basics - Part1-22nd - Sept
Java Basics - Part1-22nd - Sept
CONFIDENTIAL 1
AGENDA
1 Java Basics
2
Java Naming Conventions
3 Java Variables
2
24 Java Data Types
5 Control Statements
CONFIDENTIAL 2
Java Basics
CONFIDENTIAL 3
What is Java
CONFIDENTIAL 4
Features/Benefits of Java Programming Language
CONFIDENTIAL 5
Setting JAVA_HOME path
CONFIDENTIAL 6
Adding JAVA_HOME to Path
CONFIDENTIAL 7
Test
CONFIDENTIAL 8
What happens at compile time?
At compile time, java file is compiled by Java Compiler (It does not interact with OS) and converts the
java code into bytecode.
CONFIDENTIAL 9
Simple Java Examples
CONFIDENTIAL 10
What happens at Run time?
CONFIDENTIAL 11
Sample Java Application
Class name
main method
body
Statement
CONFIDENTIAL 12
Parameters used in Sample Java Application
class keyword is used to declare a class in java.
static is a keyword. If we declare any method as static, it is known as the static method. The core
advantage of the static method is that there is no need to create an object to invoke the static method.
The main method is executed by the JVM, so it doesn't require to create an object to invoke the main
method. So it saves memory.
void is the return type of the method. It means it doesn't return any value.
CONFIDENTIAL 13
Parameters used in Sample Java Application
main represents the starting point of the program.
String[] args is used for command line argument. We will learn it later.
CONFIDENTIAL 14
Class
What is a Class?
CONFIDENTIAL 15
Object
What is an Object ?
CONFIDENTIAL 16
Methods
CONFIDENTIAL 17
Methods passing arguments and returns no value
CONFIDENTIAL 18
Methods passing arguments and returns value
CONFIDENTIAL 19
Java Variables
CONFIDENTIAL 20
Java Variables
Variable is name of reserved area allocated in memory. In other words, it is a name of memory location.
It is a combination of "vary + able" that means its value can be changed.
Reserved
Area
RAM
CONFIDENTIAL 21
Types of Variables
Local variable
A variable declared inside the body of the method is called local variable.
class A{
void method(){
int n=90; //Local variable
}
}//end of class
In Java, the scope of a local variable is the body of the method in which it is declared. In other
words, the variable is visible in the body of the method where its declaration appears, but it is
not visible on the outside the method.
CONFIDENTIAL 22
Types of Variables
Instance variable:
A variable which is declared inside a class and outside all the methods and blocks is an instance variable.
The general scope of an instance variable is throughout the class except in static methods. The lifetime
of an instance variable is until the object stays in memory.
class A{
int data=50;//instance variable
void method()
{
int n=90;//local variable
}
}//end of class
CONFIDENTIAL 23
Static Keyword
• The static keyword in Java is used for memory management mainly.
• Static keyword can be applied for variables, methods, blocks and nested
class.
• The static keyword belongs to the class than an instance of the class.
CONFIDENTIAL 24
Types of Variables
Static variable:
Static variables are stored in the static memory. It is rare to use static variables other than declared
final and used as either public or private constants. Static variables are created when the program
starts and destroyed when the program stops.
Visibility is similar to instance variables. However, most static variables are declared public since they
must be available for users of the class.
Static variables can be accessed by calling with the class name ClassName.VariableName.
class A{
static int data=50;//static variable
void method()
{
int n=90;//local variable
}
}//end of class
CONFIDENTIAL 25
Final
CONFIDENTIAL 26
Java Naming Conventions
CONFIDENTIAL 27
Java Naming conventions
Java naming convention is a rule to follow as you decide what to name your identifiers such as class,
package, variable, constant, method, etc.
The following are the key rules that must be followed by every identifier:
The name must not contain any white spaces.
The name should not start with special characters like & (ampersand), $ (dollar), _ (underscore).
CONFIDENTIAL 28
Java Naming conventions
Class
It should start with the uppercase letter.
Use appropriate words, instead of acronyms.
Example:
public class Employee
{
//code snippet
}
CONFIDENTIAL 29
Java Naming conventions
Method
It should start with lowercase letter.
It should be a verb such as main(), print(), println().
If the name contains multiple words, start it with a lowercase letter followed by an uppercase
letter such as actionPerformed().
Example :
class Employee
{
//method
void draw()
{
//code snippet
}
}
CONFIDENTIAL 30
Java Naming conventions
Variable
It should start with a lowercase letter such as id, name.
It should not start with the special characters like & (ampersand), $ (dollar), _ (underscore).
If the name contains multiple words, start it with the lowercase letter followed by an uppercase letter
such as firstName, lastName.
Avoid using one-character variables such as x, y, z
Example :
class Employee
{
//variable
int id;
//code snippet
}
CONFIDENTIAL 31
Java Data Types
CONFIDENTIAL 32
Data Types
CONFIDENTIAL 33
Data Types
CONFIDENTIAL 34
Data Types
CONFIDENTIAL 35
Data Types
CONFIDENTIAL 36
Control Statements
CONFIDENTIAL 37
Java If-else Statement
The Java if statement is used to test the condition. It checks boolean condition: true or false.
if statement
if-else statement
if-else-if ladder
nested if statement
Java if Statement
The Java if statement tests the condition. It executes the if block if condition is true.
Syntax: if(condition){ Example:
//code to be executed
}
CONFIDENTIAL 38
Java If-else Statement
Java if-else Statement:
The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is
executed.
Syntax: Example:
if(condition){
//code if condition is true
}else{
//code if condition is false
}
CONFIDENTIAL 39
Java if-else-if ladder Statement
CONFIDENTIAL 40
Java Nested if statement
Example:
CONFIDENTIAL 41
Switch Statement
The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder
statement.
Syntax:
Output: 20
CONFIDENTIAL 42
Loops
Loops
CONFIDENTIAL 43
For Loop
Syntax: for(initialization;condition;incr/decr){
//statement or code to be executed
}
Example:
Output:
CONFIDENTIAL 44
While Loop
• The Java while loop is used to iterate a part of the program several
times. If the number of iteration is not fixed, it is recommended to
use while loop.
Syntax: while(condition){
//code to be executed
}
Example:
Output:
CONFIDENTIAL 45
Do While Loop
Example: Output:
CONFIDENTIAL 46
Break
• The Java break is used to break loop or switch statement. It breaks
the current flow of the program at specified condition. In case of
inner loop, it breaks only inner loop.
We can use Java break statement in all types of loops such as for
loop, while loop and do-while loop.
Syntax: Example:
jump-statement;
break;
Output:
CONFIDENTIAL 47
Continue
The Java continue statement is used to continue the loop.
Syntax:
jump-statement;
continue;
Example:
Output:
CONFIDENTIAL 48
References
CONFIDENTIAL 49
Practice Programs
Write a program to add an integer variable having value 5 and a double variable
having value 6?
Write a program to print the following on screen?
*
**
***
****
Take any values of length and breadth of a rectangle and check if it is square or not?
CONFIDENTIAL 50
Practice Programs
*
**
***
2. ****
*
***
*****
***
3. *
1010101
10101
101
1
CONFIDENTIAL 51
Quiz
CONFIDENTIAL 52
Thank You
CONFIDENTIAL 53