03 Fundementals - Numeric Data
03 Fundementals - Numeric Data
03 Fundementals - Numeric Data
Programs
(Part 1)
Reference
• Readings
• Chapter 2: Introduction to Java Applications; Input/Output and Operators
• Chapter 3: Control Statements: Part I; Assignment, ++ and -- Operators
Introduction
Java application programming
Java application: a computer program that executes when you use the java
command to launch the Java Virtual Machine (JVM).
JVM
First Program
Comments
Class Name
main Method
Statement
First Program
Commenting Your Programs
// Fig. 2.1: Welcome1.java
• // indicates that the line is a comment.
• Used to document programs and improve their readability.
• Compiler ignores comments.
• A comment that begins with // is an end-of-line comment—it terminates at the end of the line
on which it appears.
• Traditional comment, can be spread over several lines as in
/* This is a traditional comment. It
can be split over multiple lines */
• This type of comment begins with /* and ends with */.
• All text between the delimiters is ignored by the compiler.
• Blank lines, space characters and tabs are known as white space (or whitespace).
• They are ignored by the compiler. But make programs easier to read.
https://trinket.io/java/8d74ae0d59
First Program
• Displaying strings
• Phrases that appear in quotation marks are called strings, because they contain a
sequence of “characters” strung together. Characters can be letters, numbers,
punctuation marks, symbols, spaces, tabs, etc.
First Program
https://trinket.io/java/56a156837e
First Program
• Escape Sequence
• it is possible to display multiple lines of output in just one line of code. You just
must tell Java where to put the line breaks.
• The backslash (\) is called an escape character.
• Indicates a “special character”
• Backslash is combined with the next character to form an escape sequence—\n
represents the newline character.
Declaring variables
• Programs remember numbers and other data in the computer’s memory and access
that data through program elements called variables.
• Every variable has a name, a type, a size (in bytes) and a value.
• A variable is a named location that stores a value. Values may be numbers, text,
images, sounds, and other types of data. To store a value, you first must declare a
variable.
String message;
• For declaring multiple variables with the same type on one line: hour and minute are both
integers. Note that each declaration statement ends with a semicolon.
int hour, minute;
• You can use any name you want for a variable. But there are about 50 reserved words,
called keywords, that you are not allowed to use as variable names. These words
include public, class, static, void, and int, which are used by the compiler to analyze the
structure of the program.
Built-in Data Type
• int: hold integer values [whole numbers such as 72, –1127 and 0].
• float and double: hold real number [contain decimal points, such as 3.4, 0.0 and –11.19].
• char : hold character data between single quotation mark [such as an uppercase letter
(e.g., A), a digit (e.g., 7), a special character (e.g., *or %) or an escape sequence (e.g., the
newline character, \n)].
• string : hold sequence of character between double quotation mark [such as “Hello
World”, “Hi”, “3.14”, “true”, “2020”].
• Variables must be initialized (assigned for the first time) before they can be used.
You can declare a variable and then assign a value later, as in the previous
example. You can also declare and initialize on the same line:
• The left side of an assignment statement has to be a variable name (storage location).
• In Java, an assignment statement can make two variables equal, but they don’t
have to stay that way.
• This import statement tells the compiler that when you say
Scanner, you mean the one defined in java.util. Import statements
can’t be inside a class definition. By convention, they are usually
at the beginning of the file.
• Run-time error, so-called because it does not appear until after the program has
started running.
• Ex: divide by zero
• logic error. If your program has a logic error, it will compile and run without
generating error messages, but it will not do the right thing.
• https://trinket.io/java/35ddb7b272
Integer Division
• The arithmetic operators are binary operators because they each operate on two operands.
• Any fractional part in integer division is simply truncated (i.e., discarded)—no rounding
occurs.
• In many cases Java automatically converts from one type to another if promotion
is possible:
double y = 1; // legal, but bad style
• The expression on the right divides two integers, so Java does integer division,
which yields the int value 0. Converted to double, the value assigned to y is 0.0.
double y = 1 / 3; // common mistake (logic error)
Conversion
• The simplest way to convert a floating-point value to an integer is to use a type
cast, so called because it “casts” a value from one type to another. The syntax for
type casting is to put the name of the type in parentheses and use it as an operator.
• The casting used to convert from double to int to avoid compilation error
“incompatible types: possible lossy conversion from double to int”
• The value of x and y in the following code did not change they still contain a
double value in the memory.
average=total/(double)counter;
System.out.println("Average with cast right operand="+ average);
average=(double)total/(double)counter;
System.out.println("Average with cast both operand="+ average);
}
Conversion
• The previous code use casting from int to double to avoid losing data “when
performing arithmetic operation such as division.
• As we can see from the first operation when we did not use casting the result lost
its fraction part.
• The value of total and counter in the following code did not change they still
contain an integer value in the memory.
Rules of Operator Precedence
• Parentheses are used to group terms in expressions in the same manner as in algebraic expressions.
• If an expression contains nested parentheses, the expression in the innermost set of parentheses is
evaluated first.
• As in algebra, it’s acceptable to place unnecessary parentheses in an expression to make the
expression clearer
Increment and Decrement Operators
• Unary increment operator, ++, adds one to its operand