GRADE 9 - SPICT
MR. JAY-R L. DE LEON
Teacher I
JAVA
PROGRAMMING
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
Single-line comments
start with two forward
slashes (//).
Example
// This is a comment
[Link]("Hello
World");
Example
[Link]("Hello World"); // This is a comment
Java Multi-line comments
Multi-line comments start
with /* and ends with */.
Example
/* The code below will print
the words Hello World to the
screen, and it is amazing */
[Link]("Hello
World");
Java Variables
Variables are containers
for storing data values.
Java Variables
•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
•float - stores floating point numbers, with
decimals, such as 19.99 or -19.99
Java Variables
•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
Syntax
type variableName =
value;
Rules for naming variables:
The general rules for naming variables are:
• Names can contain letters, digits, underscores, and dollar signs
• Names must begin with a letter
• Names should start with a lowercase letter and it cannot contain
whitespace
• Names can also begin with $ and _
• 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
String
Example
Create a variable called name of
type String and assign it the value
"John":
String name = "John";
[Link](name);
Example
String name = "John";
[Link]("Hello " +
name);
Example
String firstName = "John ";
String lastName = "Doe";
String fullName = firstName +
lastName;
[Link](fullName);
Primitive Data Types
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to
2,147,483,647
long 8 bytes Stores whole numbers from -
9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to
7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15
decimal digits
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values
char
char myGrade = 'B';
[Link](myGrade
);
Alternatively, if you are familiar with ASCII values,
you can use those to display certain characters:
char myVar1 = 65, myVar2 = 66,
myVar3 = 67;
[Link](myVar1);
[Link](myVar2);
[Link](myVar3);
Integer
int myNum = 15;
[Link](myNum);
Example
int x = 5;
int y = 6;
[Link](x + y); // Print the value of x
+ y
Example
int x = 5, y = 6, z = 50;
[Link](x + y +
z);
Example
int x, y, z; x = y = z =
50; [Link](x +
y + z);
Long
long myNum =
15000000000L;
[Link](myNum
);
Float and Double
float f1 = 35e3f;
double d1 = 12E4d;
[Link](f1);
[Link](d1);
Boolean
boolean isJavaFun = true;
boolean isFishTasty = false;
[Link](isJavaFun); // Outputs
true [Link](isFishTasty); //
Outputs false
THANK YOU