0% found this document useful (0 votes)
7 views62 pages

Java Basics and Using Data Compressed

Uploaded by

jericrealista5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views62 pages

Java Basics and Using Data Compressed

Uploaded by

jericrealista5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 62

GROUP 3 BSIT-2B 2ND YEAR ITPF-01

JAVA BASICS
AND
USING DATA
BY : GROUP 3
REPORTER 2
INTRODUCTION
Java is a high-level, class-based, object-oriented
programming language Java is a multi-platform,
object-oriented, and network-centric language that
can be used as a platform in itself. It is a fast, secure,
reliable programming language for coding everything
from mobile apps and enterprise software to big data
applications and server-side technologies.
GROUP 3 BSIT-2B 2ND YEAR ITPF-01

BASIC PROGRAM
STRUCTURE IN
JAVA
A TYPICAL STRUCTURE OF A JAVA PROGRAM CONTAINS the FOLLOWING ELEMENTS :
• Package declaration - Used to specify the package to which the current Java
class or file belongs.
• Import statements - Import statements are used to bring classes, interfaces,
or entire packages into visibility for a particular Java file, allowing you to use
them without needing to fully qualify their names every time.
• Comments - Comments are a way to add notes and explanations to your
code.
• Class definition - A Java program may contain several class definitions,
classes are an essential part of any Java program. It defines the information
about the user-defined classes in a program.
• Main Method Class - The main method is from where the execution actually
starts and follows the order specified for the following statements.
PACKAGE DECLARATION
Structure of a Package Declaration:
The package declaration is typically the first line of code in a Java file,
before any import statements or class definitions. It follows this
structure:

This statement declares that all the classes and interfaces defined in this
source file are a part of the packageName and only one package can be
declared in the source file.
IMPORT STATEMENTS
Structure of an Import Statement:
An import statement typically appears at the beginning of a Java file,
just after the package declaration (if there is one), and before the
class or interface declarations.

In the example above, the “packageName” is a package, while “ClassName”


is a class of the “packageName” package. Using the “Classname” class to
get user input.
MAIN METHOD CLASS
The main method is from where the execution
actually starts and follows the order specified
for the following statements.
MAIN METHOD CLASS
Let’s take a look at a sample program to understand how it is structured:
MAIN METHOD CLASS

LET’S ANALYZE THE ABOVE PROGRAM LINE BY LINE TO UNDERSTAND HOW IT WORKS:
MAIN METHOD CLASS

public class MyFirstProgram


This creates a class called MyFirstProgram. You should make
sure that the class name starts with a capital letter, and the
public word means it is accessible from any other classes.
MAIN METHOD CLASS

Braces { }
It is known as curly braces { }. It is used to delimit compound
statements. Java uses them for surrounding the bodies of
loops, methods and classes.
MAIN METHOD CLASS

public static void main


MAIN METHOD CLASS

public static void main


• When the main method is declared “public”, it means that it can be used outside of this class as well.
MAIN METHOD CLASS

public static void main


• When the main method is declared “public”, it means that it can be used outside of this class as well.
• The word “static” means that we want to access a method without making its objects. As we call the
main method without creating any objects.
MAIN METHOD CLASS

public static void main


• When the main method is declared “public”, it means that it can be used outside of this class as well.
• The word “static” means that we want to access a method without making its objects. As we call the
main method without creating any objects.
• The word “void” indicates that it does not return any value. The main is declared as void because it does
not return any value.
MAIN METHOD CLASS

public static void main


• When the main method is declared “public”, it means that it can be used outside of this class as well.
• The word “static” means that we want to access a method without making its objects. As we call the
main method without creating any objects.
• The word “void” indicates that it does not return any value. The main is declared as void because it does
not return any value.
• “Main” is the method, which is an essential part of any Java program.
MAIN METHOD CLASS

String[]args
It is an array where each element is a string, which is named as args.
If you run the Java code through a console, you can pass the input
parameter. The main() takes it as an input.
MAIN METHOD CLASS

System.out.println();
It is a statement which prints the argument passed to it. The
println() method display results on the monitor.
GROUP 3 BSIT-2B 2ND YEAR ITPF-01

JAVA
IDENTIFIERS
IDENTIFIERS

A Java identifier is a name given to a package, class,


interface, method, or variable. It allows a programmer to
refer to the item from other places in the program. To make
the most out of the identifiers you choose, make them
meaningful and follow the standard Java naming
conventions.
RULES OF IDENTIFIERS IN JAVA

1.Valid identifiers- characters (A-Z & a-z ),


Numbers ( 0-9 ), Underscore ( _ ), Dollars ( $ ).
2. We can’t state a variable with space.

3. We can’t begin an identifier with a number.


RULES OF IDENTIFIERS IN JAVA
NA
There are types of
Name casing
convensions that are
commonly used such
as camelCase,
snake_case and
PascalCase.
GROUP 3 BSIT-2B 2ND YEAR ITPF-01

JAVA PROGRAM
COMMENTS
COMMENTS

In programming, comments are a way to add notes and


explanations to your code. Comments are readable to
people and ignored by the compiler, which is a program
that converts code, making it readable to a computer.
There are three ways to write comments in Java. Single line
comments begin with two forward slashes.
2 TYPES OF COMMENT
Single-Line Comments: Start
with “//” and extend to the end
of the line :

Multi-Line Comments: Start


with /* and end with */. They
can span multiple lines :
GROUP 3 BSIT-2B 2ND YEAR ITPF-01

DATA TYPES,
VARIABLES, AND
CONSTANTS
CONSTANT

Cannot be redeclared and reassigned


VARIABLES
Variable when its value might change. Java allows us to
refer to the data in a program by defining variables –
temporary storage location in computer memory. It can
hold one value or data at a time, and the value stores in it
might change.
When we use identifiers (name given to a variable),
whether data is variable or constant, we used to tell the
compiler which kind of data we will store in an identifier.
Different types of variable
• 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
• 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 VARIABLES

Variable declaration is giving a name or identifier that will


hold and store data and it should be declared after data type as
shown below:
Syntax:
dataType identifier;
dataType identifier1, identifier2, identifier3;
Variables are named using the same naming rules as you do
for legal class identifiers. Conventionally, identifiers for variable
name starts with the lower case to identify and distinguish them
from class names.
DATA TYPES

Integer Data Types


Integer data type is a type of data that holds numbers specifically
whole numbers, positive or negative. Java provides four integer data
types: int, short, long, and byte (they are different in the number of
bytes of memory.)

Example:
Floating Data Type
Floating-point is a type of data that holds fractional values. Java
supports two floating point: single-precision float and the double-
precision double.

Example:
Character Data Type
You use the char to store and hold any single character or single
Unicode character. If you place constant character values you must
enclose the character with single quotes (‘ ‘).

Example:
boolean Data Type
Boolean data type stores only two values. Logically, based on the
true-or-false comparisons. It is used for decision making and control
structures

Example:
GROUP 3 BSIT-2B 2ND YEAR ITPF-01

ASSIGNMENT
OPERATOR,
INITIALS AND
LITERALS
assignment Operator
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to
assign the value 10 to a variable called x:
assignment Operator
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to
assign the value 10 to a variable called x:

You can also assign values based on the result of comparisons


to Boolean variables.
comparison Operator
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
Relational or Comparison Operators
Operators Description Example (True) Example (False)

< Less Than 100 < 1000 1000 < 100

> Greater Than 5>1 10 > 20

== Equal to 100 == 100 100 == 99

Less than or Equal


<= to
3 <= 4 4 <= 2

Greater than or
>= Equal to
50 >= 30 50 >= 60

!= Not Equal to 50 != 10 50 != 50
String literal and escape sequences
String Literal* is a sequence of characters enclosed by double
quotation marks.
String literal and escape sequences
String Literal* is a sequence of characters enclosed by double
quotation marks.

Java’s String literals can’t extend over two lines. Java solves
these problems by providing a set of escape sequences that can
be used to include within String literals.
common escape sequences
Operator Description

\b Backspace; moves the cursor one space to the left

\t Tab; moves the cursor to the next tab stop

\n Newline or linefeed; moves the cursor to the beginning of the next line

\r Carriage return; moves the cursor to the beginning of the current line

\” Double quotation mark; displays a double quotation mark

\’ Single quotation mark; displays a single quotation mark

\\ Backslash; displays a backslash character


GROUP 3 BSIT-2B 2ND YEAR ITPF-01

ARITHMETIC
OPERATORS AND
OPERATOR
PRECEDENCE
An operator to add two or more
values.
ADDITION OPERATOR Using ‘+’.
We can store the sum in a variable,
and then display it.

We can display the sum directly.

→The sum is: 4

A REMEMBER →The sum is: 13

So to display the expression correctly, we use ().


An operator to subtract
two or more values
SUBTRACTION OPERATOR
- Using '-'.

→The diff is : 2

BUT ERROR!
An operator to multiply two
MULTIPLICATION OPERATOR or more values
- Using '*'.

→The product is : 6

AND →The product is : 6

Works with or without ().


An operator to divide two or
DIVISION OPERATOR more values
- Using '/'.

→The product is : 3

AND →The product is : 3

Works with or without ().


DIVISION OPERATOR

We will talk about this operator in much more


details later
The result of the division of two integers is an
integer.
→ We can use casting to get a double result
- The result of the division of two doubles is a
double.
The result of the division of an integer and a
double is a double (implicit casting).
Remainder of division
Using '%'.
MODULO OPERATOR - This is a very important operator as
you will see later on.
example: 4% 2 = 0 → 4 is even
5%2=1-5 is odd

→The result is : 2

AND →The result is : 2

Works with or without ().


OPERATOR PRECIDENCE

All operations are done from left to right in the following order:

1. The operations between the parenthesis


2. The multiplication and the division
3. The addition and the subtraction
2 * (1 + 5) 12 / (4 + 2)
2 * 6-12 / 6
12-2
10
GROUP 3 BSIT-2B 2ND YEAR ITPF-01

MIXED TYPE
ARITHMETIC AND
TYPE CASTING
Operand Promotion in Java refers to the
automatic conversion of data types when
performing operations between different types
of operands. Java follows specific rules for
operand promotion to ensure compatibility
and accuracy during arithmetic and other
operations. These rules are mostly applied
when dealing with arithmetic expressions that
involve different data types.
Rules of Operand Promotion
Data Type of One
Data Type of other Operand Promotion of Other Operand Data Type of Result
Operand

double char, byte, short, int, long, float Double double

float char, byte, short, int, long Float float

long char, byte, short, int Long long

int char, byte, short Int int

Both operands are promoted to


short char, byte int
int

Both operands are promoted to


byte Char int
int
Two Types of Type Casting

Implicit Type Casting - the arithmetic promotion of operands is


called implicit type casting because the compiler performs the
promotions automatically.
Implicit casting only works when going from a smaller type to a
larger one. Java will not implicitly convert a larger type to a
smaller one (like double to int) because of the potential for data
loss. This requires explicit type casting.

Explicit Type Casting - this type of type casting uses this syntax:

targetType variableName = (targetType) Variablename;


GROUP 3 BSIT-2B 2ND YEAR ITPF-01

SHORTCUT
OPERATORS
Java provides shortcut operators (++ and --). We use these
operators to perform operations such as adding (incrementing)
and subtracting (decrementing) one value to a variable. Note
that there are no spaces between a ++ and - - in a variable. source

count++ //adds 1 to the value of count variable


count-- // subtracts 1 to the value of count variable
Thus,
count++ is equivalent to count = count + 1 and count+=1; and
count-- is equivalent to count = count - 1 and count-=1.
GROUP 3 BSIT-2B 2ND YEAR ITPF-01

SCANNER CLASSES
What is the Scanner Class?

- A utility class in Java for reading input from various sources

- Part of `java.util` package.


Importing Scanner
• Example:

reading different types of input


example code reading user input
• Example:
thank you
GROUP 3

ITPF-01 JAVA BASICS AND USING DATA REPORTED TO: MAAM. AILEEN

You might also like