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

2_Java Fundamentals_2024

The document provides an overview of Java programming fundamentals, using a cookie recipe analogy to explain variables and tokens. It covers the structure of a Java program, the use of comments, and the different types of tokens including keywords, identifiers, literals, operators, and separators. Additionally, it includes activities for peer review and group learning to reinforce the concepts presented.

Uploaded by

aspotkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

2_Java Fundamentals_2024

The document provides an overview of Java programming fundamentals, using a cookie recipe analogy to explain variables and tokens. It covers the structure of a Java program, the use of comments, and the different types of tokens including keywords, identifiers, literals, operators, and separators. Additionally, it includes activities for peer review and group learning to reinforce the concepts presented.

Uploaded by

aspotkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

JAVA FUNDAMENTALS

RECAP
Connect to prior learning

Quiz time
STIMULUS / STARTER
Stimulus: The Recipe Ingredients
Imagine you're following a recipe to bake cookies. In the recipe, you
have a list of ingredients and their quantities. Now, let's relate this
to programming:
1.Variables: Think of variables as containers for storing information,
much like the ingredients listed in a recipe. Each variable can hold
different types of data, just like each ingredient in a recipe has its
own unique properties (e.g., flour, sugar, eggs, butter).
2.Tokens: In programming, tokens are like the individual steps or
instructions in a recipe. Each token serves a specific purpose and
contributes to the overall process of executing a program.
How do you
store these
ingredients in
Java?
public class CookieRecipe {
public static void main() {
// Declare variables for ingredients and quantities
String flour = "2 cups";
String sugar = "1 cup";
int eggs=2;
double butter = 0.5; // in cups

// Output the ingredients and quantities


System.out.println("Cookie Recipe:");
System.out.println("- Flour: " + flour);
System.out.println("- Sugar: " + sugar);
System.out.println("- Eggs: " + eggs);
System.out.println("- Butter: " + butter + " cups");
}
}
In this program:
We declare variables (flour, sugar and butter) to represent the ingredients and their
quantities.
Each variable holds a specific type of data: String for text (e.g., flour, sugar), int for
whole numbers (e.g., eggs), and double for decimal numbers (e.g., butter).
We output the ingredients and their quantities using System.out.println().
When we run this program, it will display the
ingredients and quantities like a recipe:

Cookie Recipe:
- Flour: 2 cups
- Sugar: 1 cup
- Eggs: 2
- Butter: 0.5 cups

This illustrates how variables (like ingredients)


and tokens (like recipe steps) are used in Java
programming to organize and manipulate data.
READING TIME-INDIVIDUAL ACTIVITY
(10 MINUTES)
Java has been
designed to
resemble some of
the most popular
programming
languages like C &
C++
JAVA Character Set
• Character set is a set of valid characters that
a language can recognize. A character
represents any letter, digit or any other sign.
• Java uses the Unicode character set.
• Unicode is a character coding scheme that
has character codes for numbers, arithmetic
symbols, special characters and for the letters
in almost all the languages in the world.
Structure of a JAVA Program
Typically a java program has one or more instructions enclosed inside a function,
which in turn is enclosed inside a class. Every java instruction has to be terminated by
a semicolon.
Example :
class first // a simple java program
{
public static void main( )
{
System.out.println(“ Hello “);
}
}
The above program has only one instruction : to display hello on the screen.
There is only one statement or instruction that is terminated by a semicolon.
// - indicates a comment line
The body of the program is enclosed in a function called “main”, which in turn is
enclosed in a class called “first” .
The keyword “public” indicates that the main function is to be publically accessible for
other classes.
The keyword “static” defines that the main function is to be independent and not
bound to any object.
The keyword “void” defines that the main function does not return any data.
System.out.println(“ Hello “) is an output statement to print “Hello”.
Comments
Comments are useful in documenting a java program. They provide valuable
information to a programmer and make it easier for the programmer to understand the
program.

Everything between /* and */ is ignored by the compiler, and everything on a single


line after // is also ignored.

// This is the Hello World program in Java


class HelloWorld
{
public static void main ( )
{ /* Now let's print the line Hello World */
System.out.println("Hello World!");
} // main ends here
} // HelloWorld ends here
Different Styles of Comments
/* */ style comments can comment out multiple lines so
they are useful when you want to remove large blocks of
code, perhaps for debugging purposes.
// style comments are better for short notes of no more
than a line.
/** */ style comments are used for documentation
purposes. These type of comments stand out clearly and
are easier to read.
Note: /* */ can be used in the middle of a line whereas
// can only be used at the end. However putting a
comment in the middle of a line makes code harder to
read and is generally considered to be bad.
Tokens
The smallest individual unit in a program
is known as a token.

Java has the following tokens:


• Keywords
• Identifiers
• Literals
• Operators
• Separators
Keywords

• Keywords are reserved words like int, char, if


• Keywords are words that convey a special
meaning to the compiler.
• They are not to be used as normal names.
Identifiers
• Identifiers are names given to different parts
of the program such as variables, classes,
functions, arrays etc.
• Valid identifiers are Myfile, _DS, $1_to_$10,
data_rec, DATA_REC
• Invalid Identifiers are 29CLCT, My.file,
DATA-REC
Identifier forming rules of Java

• Can have alphabets, digits, underscores and


dollar sign.
• Must not be a keyword, Boolean literal (like
true and false or null literal(like null).
• Must not begin with a digit.
• Java is case sensitive i.e., upper-case letters
and lower-case letters are treated differently.
• Must not have spaces
Literals
Literals are data items that have fixed
data values.
• Integer literals - 6700000
• Floating literals - 25.6789f, 34.7685
• Boolean literals - true, false
• Character literals - ‘a’ , ‘g’
• String literal - “good day”
• Null literal - null
Operators
• Operators are tokens which perform specific
operations on data.
• Examples:
• Arithmetic Operators +, -, /, *, %(modulus)
• Relational Operators <, >, ==, != etc.
• Logical operators ! (not),||(or), &&(and)
Separators
The following characters are recognized as
separators or punctuators in Java.
( ) (parenthesis)
{ }(curly braces)
[ ] (square brackets)
; semi-colon
, comma
.dot
LEARNING STATION ACTIVITY
(20 MINUTES)
• Move to the Learning
station assigned to you,
in group
• After spending 5
minutes at a station,
move to the next group
• Repeat this process till
you reach back to your
original station
CHECKS FOR LEARNING

• Peer review & discussion


• This will check your learning and bring to light the
concept that needs to be revisited

You might also like