Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Objectives
Become familiar with the basic components of a
expressions.
Explore how mixed expressions are evaluated.
Objectives
Learn about type casting.
it does.
Discover how to input data into memory by using
input statements.
Become familiar with the use of increment and
decrement operators.
Objectives
Examine ways to output results using output
statements.
Learn how to import packages and why they are
necessary.
Discover how to create a Java application
program.
Explore how to properly structure a program,
implemented by emulating software on a real machine. The JVM provides the hardware platform specifications to which you compile all Java technology code. This specification enables the Java software to be platform-independent because the compilation is done for a generic machine known as the JVM. Bytecode is a special machine language that can be understood by the Java Virtual Machine (JVM). The bytecode is independent of any particular computer hardware, so any computer with a Java interpreter can execute the compiled Java program, no matter what type of computer the program was compiled on.
Cont.
Garbage Collection - In Java, the programmer is freed from the
burden of having to deallocate that memory themselves by having what we call the garbage collection thread. The garbage collection thread is responsible for freeing any memory that can be freed. This happens automatically during the lifetime of the Java program.
Code security is attained in Java through the implementation of
its Java Runtime Environment (JRE). The JRE runs code compiled for a JVM and performs class loading (through the class loader), code verification (through the bytecode verifier) and finally code execution.
Cont.
Class Loader - is responsible for loading all classes needed for
the Java program. It adds security by separating the namespaces for the classes of the local file system from those that are imported from network sources.
bytecode verifier - then tests the format of the code fragments
and checks the code fragments for illegal code that can violate access rights to objects.
program.
Token: The smallest individual unit of a program.
Special Symbols
Word Symbols
void public static throws return
int
float
double char
Java Identifiers
Names of things.
Consists of:
Letters Digits
sign.
Illegal Identifiers
Data Types
A set of values together with a set of operations.
Boolean:
True False
+ addition
- subtraction * multiplication
/ division
% mod (modulus) Unary operator: An operator that has one operand.
Order of Precedence
* +
/ -
Operators in 1 have a higher precedence than operators in 2. When operators have the same level of precedence, operations are performed from left to right.
Expressions
Integral expressions
Integral Expressions
All operands are integers.
Examples:
2 + 3 * 5 3 + x y / 7 x + 2 * (y z) + 18
Floating-Point Expressions
All operands are floating-point numbers.
Examples:
Mixed Expressions
Operands of different types.
Examples:
2 + 3.5 6 / 4 + 3.9 Integer operands yield an integer result; floatingpoint numbers yield floating-point results. If both types of operands are present, the result is a floating-point number. Precedence rules are followed.
Syntax:
(dataTypeName) expression Expression evaluated first, then type converted to: dataTypeName Examples:
(int)(7.9 + 6.7) = 14 (int)(7.9) + (int)(6.7) = 13
String:
Sequence of zero or more characters. Enclosed in double quotation marks. Null or empty strings have no characters. Numeric strings consist of integers or decimal
Input
Named constant
Cannot be changed during program execution. Declared by using the reserved word final. Initialized when it is declared.
Example 2-11 final double CENTIMETERS_PER_INCH = 2.54; final int NO_OF_STUDENTS = 20; final char BLANK = ' '; final double PAY_RATE = 15.75;
Input
Variable (name, value, data type, size)
Content may change during program execution. Must be declared before it can be used. May not be automatically initialized. If new value is assigned, old one is destroyed. Value can only be changed by an assignment statement or an input (read) statement.
Input
The Assignment Statement
Example 2-13
int i, j; double sale; char first; String str; i = 4; j = 4 * 5 - 11; sale = 0.02 * 1000; first = 'D'; str = "It is a sunny day.";
variable = expression;
Input
To read data:
1.
Create an input stream object of the class Scanner. Use the methods such as next, nextLine, nextInt, and nextDouble.
2.
Input
static Scanner console = new Scanner(System.in);
Example 2-16
static Scanner console = new Scanner(System.in); int feet; int inches; Suppose the input is 23 7 feet = console.nextInt(); inches = console.nextInt(); //Line 1 //Line 2
Pre-increment: ++variable
Post-increment: variable++
Pre-decrement: --variable Post-decrement: variable--
String str; int num1, num2; num1 = 12; num2 = 26; str = "The sum = " + num1 + num2; After this statement executes, the string assigned to str is: "The sum = 1226";
str = "The sum = " + (num1 + num2); In this statement, because of the parentheses, you first evaluate num1 + num2. Because num1 and num2 are both int variables, num1 + num2 = 12 + 26 = 38. After this statement executes, the string assigned to str is: "The sum = 38";
Output
Standard output object is System.out. Methods:
print println
Syntax:
import Statement
Used to import the components of a package into
a program. Reserved word. import java.io.*; Imports the (components of the) package java.io into the program. Primitive data types and the class String:
Part of the Java language. Dont need to be imported.
naming identifiers.
is equivalent to:
variable *= expression; Similarly, variable = variable + (expression); is equivalent to:
variable += expression;
Programming Examples
Convert Length program:
Input: Length in feet and inches.
Output: Equivalent length in centimeters.
Chapter Summary
Basic elements of a Java program include:
The main method
Reserved words Special symbols Identifiers Data types Expressions Input
Output
Statements
Summary
To create a Java application, it is important to
understand:
Syntax rules. Semantic rules.
Objectives
In this section, we will be discussing on how to write,
compile and run Java programs. There are two ways of doing this, the first one is by using a console and a text editor. The second one is by using Netbeans which is an Integrated Development Environment or IDE. At the end of the lesson, the student should be able to: Create a Java program using text editor and console in the Linux environment Differentiate between syntax-errors and runtime errors Create a Java program using Netbeans
Introduction
What is IDE? a programming environment integrated into a software application that provides a GUI builder, a text or code editor, a compiler and/or interpreter and a debugger.
{ /** * My first java program */ public static void main(String[] args) { //prints the string "Hello world" on screen System.out.println("Hello world!"); } }