Java Basics and Using Data Compressed
Java Basics and Using Data Compressed
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.
LET’S ANALYZE THE ABOVE PROGRAM LINE BY LINE TO UNDERSTAND HOW IT WORKS:
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
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
JAVA PROGRAM
COMMENTS
COMMENTS
DATA TYPES,
VARIABLES, AND
CONSTANTS
CONSTANT
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:
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
\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
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.
→The diff is : 2
BUT ERROR!
An operator to multiply two
MULTIPLICATION OPERATOR or more values
- Using '*'.
→The product is : 6
→The product is : 3
→The result is : 2
All operations are done from left to right in the following order:
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
Explicit Type Casting - this type of type casting uses this syntax:
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
SCANNER CLASSES
What is the Scanner Class?
ITPF-01 JAVA BASICS AND USING DATA REPORTED TO: MAAM. AILEEN