Professional Documents
Culture Documents
Fundamentals of
Computer Programming
**The forward slash ( / ) and the backslash ( \ ) characters often are confused, but they are two distinct characters.
You cannot use them interchangeably.
Understanding main method
The keyword static allows main( ) to be
The public keyword is an access called without having to instantiate a The keyword void simply tells
modifier, which allows the particular instance of the class. the compiler that main( ) does
programmer to control the visibility not return a value.
of class members.
The last character on the line is
the {. This signals the start of
main( )’s body. All of the code
System is a that comprises a method will
predefined class occur between the method’s
that provides opening curly brace and its
access to the closing curly brace.
system, and out is
the output stream
that is connected
to the console.
println() is a method. Method
"Hello World!" is a literal string
Dots separate names are always followed by
Closing curly braces of method that is the argument to the
classes, objects, parentheses.
main. println() method.
and methods.
Closing curly braces of class
DemoWeek2.
IDENTIFIERS,
VARIABLES, AND DATA TYPES
IDENTIFIERS
▪ Identifier - In programming languages, identifiers are used for identification purposes.
▪ Case-sensitivity - Java is case sensitive, which implies that the identifier Hi and hi
would have distinctive importance in Java.
▪ Java Identifier- All Java components require names. Names utilized for classes,
variables and strategies are called identifiers. In Java, there are a few focuses to recall
about identifiers.
• Primitive data types store the actual data value directly in the variable.
• Reference data types store a reference (memory address) to where the data
is located in memory. This allows reference types to represent more complex
data structures and share data efficiently, but it adds a layer of indirection
compared to primitive types.
VARIABLE DECLARATION
Variables are containers for storing data values. In a Java program, you must
declare a variable before it can be used. A variable declaration has the
following form:
datatype identifier;
datatype identifier1, identifier2, identifier3;
ASSIGNMENT STATEMENTS
An assignment statement that
has a variable of a primitive
type on the left side of the
equal sign causes the
following action: First, the
expression on the right side of
the equal sign is evaluated,
and then the variable on the
left side of the equal sign is
set to this value.
INITIALIZE VARIABLES
A variable that has been declared, but that has not yet been
given a value by an assignment statement (or in some other
way), is said to be uninitialized. If the variable is a variable of a
class type, it literally has no value. If the variable has a primitive
type, it likely has some default value. However, your program
will be clearer if you explicitly give the variable a value, even if
you are simply reassigning the default value.
Combining a Variable Declaration
and an Assignment
SIMPLE INPUT USING
SCANNER
SIMPLE INPUT
We use the class Scanner, which Java supplies, to accept keyboard input. Our
program must import the definition of the Scanner class from the package
java.util. Thus, we begin the program with the following statement:
import java.util.Scanner;
The following line sets things up so that data can be entered from the keyboard:
Scanner scanner = new Scanner(System.in);
This line must appear before the first statement that takes input from the
keyboard.
int age = scanner.nextInt();
This assignment statement gives a value to the variable age. The expression on
the right side of the equal sign.
SCANNER CLASS METHODS
Method Description
nextDouble() Retrieves input as a double
nextInt() Retrieves input as an int
nextLine() Retrieves the next line of data and returns it as
a String
Next() Retrieves the next complete token as a String
ECHOING INPUT
Repeating as output what a user has entered as input is called echoing the input.
Echoing input is a good programming practice; it helps eliminate
misunderstandings when the user can visually confirm what was entered.
SAMPLE
SCANNER
Pitfall: Using nextLine() Following One of the Other Scanner Input Methods
You can encounter a problem when you use one of the numeric Scanner class retrieval
methods or the next() method before you use the nextLine() method.
Don’t do it:
If you use any other Scanner
input methods prior to string
input that uses nextLine(),
the string input is ignored
unless you take special
action.
Pitfall: Using nextLine() Following One of the Other Scanner Input Methods
The solution to the problem is simple. After any next(), nextInt(), or nextDouble() call,
you can add an extra nextLine() method call that will retrieve the abandoned Enter key
character. Then, no matter what type of input follows, the program will execute
smoothly.
This statement
consumes the Enter key
that follows the integer.
Imprecision in Floating-Point Numbers
• Floating-point numbers are stored with a limited amount of precision and so are, for
all practical purposes, only approximate quantities. For example, the fraction one
third is equal to 0.3333333 . . . where the three dots indicate that the 3s go on
forever. The computer stores numbers in a format somewhat like the decimal
representation on the previously displayed line, but it has room for only a limited
number of digits. If it can store only ten digits after the decimal, then one third is
stored as 0.3333333333 (and no more 3s).
Imprecision in Floating-Point Numbers
ASSIGNMENT COMPATIBILITIES
You can assign a value of any type on the following list to a
variable of any type that appears further down on the list:
• Relational Operators
• Arithmetic Operators
• Logical Operators
• Assignment Operators
RELATIONAL OPERATORS
OPERATION OPERATOR DESCRIPTION
EQUAL TO == Compares the values of two variables for
equality
NOT EQUAL TO != Compares the values of two variables for
equality
GREATER THAN > Checks if one value is greater than the other
value
LESSER THAN < Checks if one value is lesser than the other
value
GREATER THAN OR EQUAL >= Checks if one value is lesser than the other
TO value
LESSER THAN OR EQUAL TO <= Checks if one value is lesser than or equal to
the other value
ARITHMETIC OPERATORS
OPERATION OPERATOR DESCRIPTION
ADDITION + Adds the values of two variables
SUBTRACTION - Subtracts the values of two variables
MULTIPLICATION * Multiplies the values of two variables
DIVISION / Divides the values of two variables
MODULUS % The resultant value is the remainder of
division
INCREMENT ++ Increases the value by 1
DECREMENT -- Decreases the value by 1
LOGICAL OPERATORS
The End