0% found this document useful (0 votes)
9 views

Unit 1 Basic Language Elements

The document provides a comprehensive overview of Core Java, covering its features, history, advantages, and fundamental concepts such as data types, variables, operators, and control statements. It explains Java's platform independence, the Java Virtual Machine (JVM), and the process of downloading and installing Java and Eclipse IDE. Additionally, it details syntax rules, comments, and various programming constructs like loops and decision-making statements.

Uploaded by

2403717662221016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit 1 Basic Language Elements

The document provides a comprehensive overview of Core Java, covering its features, history, advantages, and fundamental concepts such as data types, variables, operators, and control statements. It explains Java's platform independence, the Java Virtual Machine (JVM), and the process of downloading and installing Java and Eclipse IDE. Additionally, it details syntax rules, comments, and various programming constructs like loops and decision-making statements.

Uploaded by

2403717662221016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

Notes by Raggavendhra

CORE JAVA
- Raggavendhra C
LeadPro InfoTech
Unit 1 : Basic Language Elements

▪ Features of Java Language


▪ JVM and Bytecode
▪ Comments
▪ Data Types
▪ Variables

Notes by Raggavendhra
▪ Keywords
▪ Constants
▪ Operators and Expressions
▪ Control Statements
What is Java?

▪ Java is a popular programming language, created in 1995.


▪ It is owned by Oracle, and more than 3 billion devices run Java.
▪ It is used for: Web Applications, Mobile Appliations, Desktop
Application and much more

Notes by Raggavendhra
History Of Java

▪ James Gosling, Mike Sheridan and Patrick Naughton initiated the


Java language project in June 1991. The small team of sun engineers
called Green Team.
▪ Initially it was designed for small , embedded systems in electronic
appliances like set-top boxes.

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?

▪ Java is considered platform-independent because it uses a "Write Once,


Run Anywhere" approach
▪ This means that Java source code is compiled into a platform-neutral
bytecode that can be executed on any device with a Java Virtual Machine
(JVM)
Here's how Java's platform independence works:

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

▪ Java installation is very simple like installing other softwares


▪ https://www.java.com/download/ie_manual.jsp
▪ Once Java is installed, you can view the Java version using the
following command in your cmd : java –version

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

▪ Download the Eclipse from the below link


https://www.eclipse.org/downloads/packages/installer
▪ While installing , choose the package as “Eclipse IDE for Java
Developers”

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

▪ Every line of code that runs in Java must be inside a CLASS


▪ We can give any name to the class
▪ Note: Java is case-sensitive: "MyClass" and "myclass" has different
meaning.

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

▪ Observe the output for print statement

Notes by Raggavendhra
Print (vs) Println

▪ Observe the output for println statement

Notes by Raggavendhra
Print (vs) Println

▪ Observe the output

Notes by Raggavendhra
Java Comments

▪ Comments can be used to explain Java code, and to make it more


readable. It can also be used to prevent execution when testing
alternative code.
▪ Single-line comments start with two forward slashes (//). Any text
between // and the end of the line is ignored by Java (will not be

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

▪ Variables are containers for storing data values.


▪ In Java, there are different types of variables, for example:
– String - stores text, such as "Hello". String values are surrounded by double
quotes
– int - stores integers (whole numbers), without decimals, such as 123 or -123

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

▪ A constant is a variable whose value cannot change once it has been


assigned.
▪ To define a variable as a constant, we just need to add the keyword
"final" in front of the variable declaration.
▪ "constant“ means unchangeable and read-only):

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

▪ All Java variables must be identified with unique names.


▪ These unique names are called identifiers.
▪ Identifiers can be short names (like x and y) or more descriptive
names (age, sum, totalVolume).

Notes by Raggavendhra
▪ Note: It is recommended to use descriptive names in order to create
understandable and maintainable code:
General Rules for naming variables

▪ Names can contain letters, digits, underscores, and dollar signs


▪ Names must begin with a letter
▪ Names should start with a lowercase letter, and cannot contain
whitespace

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

▪ Data types are divided into two groups:


– Primitive data types - includes byte, short, int, long, float, double, boolean and
char
– Non-primitive data types - such as String, Arrays and Classes (you will learn more
about these in a later chapter)

▪ There are eight primitive data types in Java:

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

▪ A constant is a variable whose value cannot change once it has been


assigned.

Notes by Raggavendhra
Java Operators

▪ Operators are used to perform operations on variables and values.


▪ Java divides the operators into the following groups:
– Arithmetic operators
– Assignment operators
– Comparison 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

▪ Assignment operators are used to assign values to variables.


▪ = is an assignment operator. Eg : int a=10;
▪ The addition assignment operator (+=) adds a value to a variable:
▪ X+= 3 is the same as x = x + 3

Notes by Raggavendhra
Comparison Operators

▪ Comparison operators are used to compare two values (or variables).


This is important in programming, because it helps us to find answers
and make decisions.
▪ The return value of a comparison is either true or false. These values
are known as Boolean values

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

▪ Java provides three types of control flow statements.


▪ Decision Making statements ( Conditional Statements)
– if statements
– switch statement
▪ Loop statements
– do while loop

Notes by Raggavendhra
– while loop
– for loop
– for-each loop
▪ Jump statements
– break statement
– continue statement
Decision Making statements

▪ As the name suggests, decision-making statements decide which


statement to execute and when.
▪ Decision-making statements evaluate the Boolean expression and
control the program flow depending upon the result of the condition
provided.

Notes by Raggavendhra
▪ There are two types of decision-making statements in Java
– If statement
– switch statement.
IF statement

▪ In Java, the "if" statement is used to evaluate a condition.


▪ The control of the program is diverted depending upon the specific
condition.
▪ The condition of the If statement gives a Boolean value, either true or
false.

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)

statement 1; //executes when condition 1 is true

else if(condition 2)

Notes by Raggavendhra
{

statement 2; //executes when condition 2 is true

else {

statement 3; //executes when all the conditions are false

}
if-else-if ladder

Notes by Raggavendhra
if-else-if ladder

Notes by Raggavendhra
Nested If

▪ This is used when one if statement is present inside another if


statement
▪ There can be any no of if else ladder inside another if

Notes by Raggavendhra
Nested If

Notes by Raggavendhra
Nested If

Notes by Raggavendhra
Nested If

Notes by Raggavendhra
Switch Statement

▪ Switch statements are similar to if-else-if statements. The switch


statement contains multiple blocks of code called cases and a single case is
executed based on the variable which is being switched.
▪ The switch statement is easier to use instead of if-else-if statements. It also
enhances the readability of the program.
▪ Points to be noted about 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

▪ In programming, sometimes we need to execute the block of code


repeatedly while some condition evaluates to true.
▪ However, loop statements are used to execute the set of instructions
in a repeated order.
▪ The execution of the set of instructions depends upon a particular

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

▪ It enables us to initialize the loop variable, check the condition, and


increment/decrement in a single line of code. We use the for loop
only when we exactly know the number of times, we want to execute
the block of code.
for(initialization, condition, increment/decrement)

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

▪ Features of Java Language


▪ JVM and Bytecode
▪ Comments
▪ Data Types
▪ Variables

Notes by Raggavendhra
▪ Keywords
▪ Constants
▪ Operators and Expressions
▪ Control Statements

You might also like