Unit 1 Basic Language Elements
Unit 1 Basic Language Elements
CORE JAVA
- Raggavendhra C
LeadPro InfoTech
Unit 1 : Basic Language Elements
Notes by Raggavendhra
▪ Keywords
▪ Constants
▪ Operators and Expressions
▪ Control Statements
What is Java?
Notes by Raggavendhra
History Of Java
Notes by Raggavendhra
▪ Firstly, it was called ‘Green Talk’ and later as ‘Oak’
▪ In 1995, ‘Oak’ was renamed as ‘JAVA’
Advantages of Java
▪ Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
▪ It is one of the most popular programming languages in the world
▪ It has a large demand in the current job market
▪ It is easy to learn and simple to use
Notes by Raggavendhra
▪ It is open-source and free
▪ It is secure, fast and powerful
▪ It has huge community support (tens of millions of developers)
▪ Java is an object oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs
Features of JAVA
Notes by Raggavendhra
Why JAVA is platform independent?
Notes by Raggavendhra
▪ Compilation: Java source code is compiled into bytecode, which is
platform-independent.
▪ Execution: The bytecode can be executed on any platform that has a Java
Virtual Machine (JVM) compatible with that bytecode.
▪ JVM: The JVM recognizes the platform and converts the bytecode into
machine code, which can then be read and executed.
JVM (vs) JRE (vs) JDK
Notes by Raggavendhra
Java Bytecode
▪ Java bytecode is the instruction set of the Java virtual machine (JVM),
the language to which Java and other JVM-compatible source code is
compiled.
▪ Each instruction is represented by a single byte, hence the name
bytecode, making it a compact form of data.
Notes by Raggavendhra
▪ Due to the nature of bytecode, a Java bytecode program is runnable
on any machine with a compatible JVM; without the lengthy process
of compiling from source code.
▪ In general, a Java programmer does not need to understand Java
bytecode or even be aware of it.
Downloading Java
Notes by Raggavendhra
▪ Note : For our practice, we are going to install Eclipse Software
▪ Eclipse is an IDE (Integrated Development Environment) that runs on
a JAVA Virtual Machine. Thus Java need not be installed separately.
Installing Eclipse
Notes by Raggavendhra
▪ Once installation is complete, choose your workspace location where
all your works will be saved
Syntax and Basic Flow of the program
Notes by Raggavendhra
▪ Any code inside the main() method will be executed.
▪ System.out.print() is used to print statements in output
First Program
Notes by Raggavendhra
First Program - Output
Notes by Raggavendhra
Eclipse – Layout Explanation
Notes by Raggavendhra
Print (vs) Println
▪ When using the System class, there are two different ways that we
can print output: println and print . The big difference between these
two is that println will display the message on a new line, while the
print statement will print the message on the same line.
▪ If you want to print some text -> then use them inside double quotes.
Notes by Raggavendhra
Eg : print(“Text”)
▪ If you want to print some numbers -> then double quotes is not
needed. Eg : print(1);
Print (vs) Println
Notes by Raggavendhra
Print (vs) Println
Notes by Raggavendhra
Print (vs) Println
Notes by Raggavendhra
Java Comments
Notes by Raggavendhra
executed).
▪ Multi-line comments start with /* and ends with */. Any text between
/* and */ will be ignored by Java.
Java Comments
Notes by Raggavendhra
Java Variables
Notes by Raggavendhra
– float - stores floating point numbers, with decimals, such as 19.99 or -19.99
– char - stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes
– boolean - stores values with two states: true or false
Declaring (Creating) Variables
▪ To create a variable, you must specify the type and assign it a value:
▪ type variableName = value;
▪ Examples
– String name = "John";
Notes by Raggavendhra
– int myNum = 15;
▪ You can also declare a variable without assigning the value, and
assign the value later:
– int myNum;
– myNum = 15;
Declaring (Creating) Variables
Notes by Raggavendhra
Final Variables – Java constant
Notes by Raggavendhra
– final int myNum = 15;
– myNum = 20; // will generate an error: cannot assign a value to a final variable
Displaying text and variables
Notes by Raggavendhra
Displaying text and variables
Notes by Raggavendhra
Identifiers
Notes by Raggavendhra
▪ Note: It is recommended to use descriptive names in order to create
understandable and maintainable code:
General Rules for naming variables
Notes by Raggavendhra
▪ Names are case-sensitive ("myVar" and "myvar" are different
variables)
▪ Reserved words (like Java keywords, such as int or boolean) cannot
be used as names
Data Types
Notes by Raggavendhra
Primitive Data Types
Notes by Raggavendhra
Primitive Data Types
Notes by Raggavendhra
Java Keywords
▪ Java has a set of keywords that are reserved words that cannot be
used as variables, methods, classes, or any other identifiers
▪ Example : class, boolean, byte, int, import, abstract, default, final,
long, package, public, private etc.,
▪ true, false, and null are not keywords, but they are literals and
Notes by Raggavendhra
reserved words that cannot be used as identifiers.
Constants in Java
Notes by Raggavendhra
Java Operators
Notes by Raggavendhra
– Logical operators
Arithmetic Operators
▪ Addition : x+y
▪ Subtraction x-y
▪ Muultiplication x*y
▪ Division x/y
Notes by Raggavendhra
▪ Modulus x%y
▪ Increment x++
▪ Decrement x--
Arithmetic Operators
Notes by Raggavendhra
Assignment Operators
Notes by Raggavendhra
Comparison Operators
Notes by Raggavendhra
Comparison Operators
Notes by Raggavendhra
Logical Operators
▪ You can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables
or values
Notes by Raggavendhra
Logical Operators
Notes by Raggavendhra
Control Statements
▪ Java compiler executes the code from top to bottom. The statements
in the code are executed according to the order in which they appear.
▪ However, Java provides statements that can be used to control the
flow of Java code. Such statements are called control flow
statements. It is one of the fundamental features of Java, which
Notes by Raggavendhra
provides a smooth flow of program.
Types of Control Statements
Notes by Raggavendhra
– while loop
– for loop
– for-each loop
▪ Jump statements
– break statement
– continue statement
Decision Making statements
Notes by Raggavendhra
▪ There are two types of decision-making statements in Java
– If statement
– switch statement.
IF statement
Notes by Raggavendhra
▪ There are four types of if-statements given below.
– Simple if statement
– if-else statement
– if-else-if ladder
– Nested if-statement
Simple If statement
if(condition)
{
statements; // Executes when the condition is true
}
Notes by Raggavendhra
▪ If the condition is true, then the control goes inside the body of the if
conditions and the statements are executed
▪ If the condition is false, then the control will not be going inside the
body of the if condition
Simple If statement
Notes by Raggavendhra
If-else statement
if(condition)
{
statements; // Executes when the condition is true
}
Notes by Raggavendhra
Else
{
statements; // Executes when the condition is false
}
If-else statement
Notes by Raggavendhra
if-else-if ladder
if(condition 1)
else if(condition 2)
Notes by Raggavendhra
{
else {
}
if-else-if ladder
Notes by Raggavendhra
if-else-if ladder
Notes by Raggavendhra
Nested If
Notes by Raggavendhra
Nested If
Notes by Raggavendhra
Nested If
Notes by Raggavendhra
Nested If
Notes by Raggavendhra
Switch Statement
Notes by Raggavendhra
– The case variables can be int, short, byte, char, or enumeration. String type is also
supported since version 7 of Java
– Cases cannot be duplicate
– Default statement is executed when any of the case doesn't match the value of
expression. It is optional.
– Break statement terminates the switch block when the condition is satisfied.
It is optional, if not used, next case is executed.
– While using switch statements, we must notice that the case expression will be of
the same type as the variable. However, it will also be a constant value.
Switch Statement
switch (expression){
case value1:
statement1;
break;
case valueN:
Notes by Raggavendhra
statementN;
break;
default:
default statement;
}
Switch Statement
Notes by Raggavendhra
Switch Statement
Notes by Raggavendhra
Loop Statement
Notes by Raggavendhra
condition.
▪ We have three types of loops that execute similarly. However, there
are differences in their syntax and condition checking time.
– for loop
– while loop
– do-while loop
For Loop
Notes by Raggavendhra
{
//block of statements
}
For Loop
Notes by Raggavendhra
For Loop
Notes by Raggavendhra
For Loop
Notes by Raggavendhra
While Loop
▪ The while loop also used to iterate over the number of statements
multiple times. However, if we don't know the number of iterations in
advance, it is recommended to use a while loop. Unlike for loop, the
initialization and increment/decrement doesn't take place inside the
loop statement in while loop.
Notes by Raggavendhra
▪ It is also known as the entry-controlled loop since the condition is
checked at the start of the loop. If the condition is true, then the loop
body will be executed; otherwise, the statements after the loop will
be executed.
While Loop
While(condition)
{
statements
}
Notes by Raggavendhra
While Loop
Notes by Raggavendhra
Do While Loop
▪ The do-while loop checks the condition at the end of the loop after
executing the loop statements. When the number of iteration is not
known and we have to execute the loop at least once, we can use do-
while loop.
▪ It is also known as the exit-controlled loop since the condition is not
Notes by Raggavendhra
checked in advance.
Do While Loop
Do
{
statements
}
Notes by Raggavendhra
While(condition)
Do While Loop
Notes by Raggavendhra
End of Unit 1
Notes by Raggavendhra
▪ Keywords
▪ Constants
▪ Operators and Expressions
▪ Control Statements